mirror of
https://github.com/love2d/love.git
synced 2026-08-16 16:20:42 +02:00
Render pass API. Replaces love.graphics.setCanvas and friends.
- Removed love.graphics.set/getCanvas, Canvas:renderTo, Canvas:newImageData, love.graphics.clear, love.graphics.discard, and love.graphics.newScreenshot.
- Added love.graphics.beginPass and love.graphics.endPass. All rendering must happen within a pass. love.draw is now called while a render pass to the main screen is active.
- Added love.graphics.renderPass, which is a wrapper for begin/endPass which calls the supplied function argument in between a begin/end. This is similar to the old Canvas:renderTo.
- Added love.graphics.isPassActive, getPassCanvases, getPassWidth, getPassHeight, and getPassDimensions.
- Added love.graphics.captureScreenshot.
- Added a new love.run callback love.drawpasses, which is called after love.update and before love.draw. It is the place to render to Canvases using beginPass or renderPass, since love.draw now always draws to the main screen.
- Renamed the ‘canvasswitches’ field in the table returned by love.graphics.getStats to ‘renderpasses’.
love.graphics.beginPass has the following variants (and renderPass mirrors them with an additional function argument at the end):
- love.graphics.beginPass(), begins rendering to the main screen without clearing it.
- love.graphics.beginPass(r, g, b [, a]), begins rendering to the main screen and clears the screen to the specified color.
- love.graphics.beginPass(canvas [, willstencil]), begins rendering to the specified Canvas without clearing it. love.graphics.stencil can only be used within the Canvas if ‘willstencil’ is true.
- love.graphics.beginPass(canvas, r, g, b [, a] [, willstencil]), beings rendering to the specified Canvas and clears it to the specified color. love.graphics.stencil can only be used within the Canvas if ‘willstencil’ is true.
- love.graphics.beginPass(info), where ‘info’ is a table in the following form:
{
{canvas [, r, g, b, a]}, — A canvas and an optional color to clear the Canvas to when the pass begins.
{canvas2, [r, g, b, a]}, — Additional Canvases and optional clear colors for multi-canvas rendering.
…,
stencil = false, — Optional boolean field to specify whether love.graphics.stencil is allowed within this pass. False by default.
}
The pass info table can be created once up-front and used for multiple beginPass/renderPass calls.
love.graphics.endPass can take an optional callback function argument, which causes endPass to capture the contents of the Canvas drawn to in the render pass to a new ImageData and passes it to the supplied function (this replaces Canvas:newImageData). This does not work on the main screen.
love.graphics.captureScreenshot replaces love.graphics.newScreenshot. It takes a callback function argument which causes love.graphics.present to capture the contents of the screen to a new ImageData and passes it to the supplied function. captureScreenshot can only be called before drawing to the main screen begins in the current frame (e.g. in love.keypressed, love.update, etc.)
--HG--
branch : minor
This commit is contained in:
@@ -34,6 +34,8 @@ namespace love
|
||||
namespace graphics
|
||||
{
|
||||
|
||||
const int MAX_COLOR_RENDER_TARGETS = 16;
|
||||
|
||||
/**
|
||||
* Globally sets whether gamma correction is enabled. Ideally this should be set
|
||||
* prior to using any Graphics module function.
|
||||
@@ -59,6 +61,14 @@ void gammaCorrectColor(Colorf &c);
|
||||
**/
|
||||
void unGammaCorrectColor(Colorf &c);
|
||||
|
||||
class RenderOutsidePassException : public love::Exception
|
||||
{
|
||||
public:
|
||||
RenderOutsidePassException()
|
||||
: Exception("Cannot draw outside of a render pass!")
|
||||
{}
|
||||
};
|
||||
|
||||
class Graphics : public Module
|
||||
{
|
||||
public:
|
||||
@@ -178,7 +188,7 @@ public:
|
||||
struct Stats
|
||||
{
|
||||
int drawCalls;
|
||||
int canvasSwitches;
|
||||
int renderPasses;
|
||||
int shaderSwitches;
|
||||
int canvases;
|
||||
int images;
|
||||
@@ -257,6 +267,8 @@ public:
|
||||
**/
|
||||
virtual bool isActive() const = 0;
|
||||
|
||||
virtual bool isPassActive() const = 0;
|
||||
|
||||
static bool getConstant(const char *in, DrawMode &out);
|
||||
static bool getConstant(DrawMode in, const char *&out);
|
||||
|
||||
|
||||
@@ -36,11 +36,10 @@ namespace opengl
|
||||
static GLenum createFBO(GLuint &framebuffer, GLuint texture)
|
||||
{
|
||||
// get currently bound fbo to reset to it later
|
||||
GLint current_fbo;
|
||||
glGetIntegerv(GL_DRAW_FRAMEBUFFER_BINDING, ¤t_fbo);
|
||||
GLuint current_fbo = gl.getFramebuffer(OpenGL::FRAMEBUFFER_ALL);
|
||||
|
||||
glGenFramebuffers(1, &framebuffer);
|
||||
gl.bindFramebuffer(GL_FRAMEBUFFER, framebuffer);
|
||||
gl.bindFramebuffer(OpenGL::FRAMEBUFFER_ALL, framebuffer);
|
||||
|
||||
if (texture != 0)
|
||||
{
|
||||
@@ -53,14 +52,19 @@ static GLenum createFBO(GLuint &framebuffer, GLuint texture)
|
||||
|
||||
GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
|
||||
|
||||
// unbind framebuffer
|
||||
gl.bindFramebuffer(GL_FRAMEBUFFER, (GLuint) current_fbo);
|
||||
|
||||
gl.bindFramebuffer(OpenGL::FRAMEBUFFER_ALL, current_fbo);
|
||||
return status;
|
||||
}
|
||||
|
||||
static GLenum createMSAABuffer(int width, int height, int &samples, GLenum iformat, GLuint &buffer)
|
||||
static bool createMSAABuffer(int width, int height, int &samples, GLenum iformat, GLuint &buffer)
|
||||
{
|
||||
GLuint current_fbo = gl.getFramebuffer(OpenGL::FRAMEBUFFER_ALL);
|
||||
|
||||
// Temporary FBO used to clear the renderbuffer.
|
||||
GLuint fbo = 0;
|
||||
glGenFramebuffers(1, &fbo);
|
||||
gl.bindFramebuffer(OpenGL::FRAMEBUFFER_ALL, fbo);
|
||||
|
||||
glGenRenderbuffers(1, &buffer);
|
||||
glBindRenderbuffer(GL_RENDERBUFFER, buffer);
|
||||
|
||||
@@ -73,7 +77,7 @@ static GLenum createMSAABuffer(int width, int height, int &samples, GLenum iform
|
||||
|
||||
GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
|
||||
|
||||
if (status == GL_FRAMEBUFFER_COMPLETE)
|
||||
if (status == GL_FRAMEBUFFER_COMPLETE && samples > 1)
|
||||
{
|
||||
// Initialize the buffer to transparent black.
|
||||
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
|
||||
@@ -83,22 +87,21 @@ static GLenum createMSAABuffer(int width, int height, int &samples, GLenum iform
|
||||
{
|
||||
glDeleteRenderbuffers(1, &buffer);
|
||||
buffer = 0;
|
||||
samples = 0;
|
||||
}
|
||||
|
||||
return status;
|
||||
gl.bindFramebuffer(OpenGL::FRAMEBUFFER_ALL, current_fbo);
|
||||
gl.deleteFramebuffer(fbo);
|
||||
|
||||
return status == GL_FRAMEBUFFER_COMPLETE && samples > 1;
|
||||
}
|
||||
|
||||
Canvas *Canvas::current = nullptr;
|
||||
OpenGL::Viewport Canvas::systemViewport = OpenGL::Viewport();
|
||||
bool Canvas::screenHasSRGB = false;
|
||||
int Canvas::canvasCount = 0;
|
||||
|
||||
Canvas::Canvas(int width, int height, Format format, int msaa)
|
||||
: fbo(0)
|
||||
, resolve_fbo(0)
|
||||
, texture(0)
|
||||
, msaa_buffer(0)
|
||||
, depth_stencil(0)
|
||||
, format(format)
|
||||
, requested_samples(msaa)
|
||||
, actual_samples(0)
|
||||
@@ -143,65 +146,15 @@ Canvas::Canvas(int width, int height, Format format, int msaa)
|
||||
Canvas::~Canvas()
|
||||
{
|
||||
--canvasCount;
|
||||
|
||||
// reset framebuffer if still using this one
|
||||
if (current == this)
|
||||
stopGrab();
|
||||
|
||||
unloadVolatile();
|
||||
}
|
||||
|
||||
bool Canvas::createMSAAFBO(GLenum internalformat)
|
||||
{
|
||||
actual_samples = requested_samples;
|
||||
|
||||
if (actual_samples <= 1)
|
||||
{
|
||||
actual_samples = 0;
|
||||
return false;
|
||||
}
|
||||
|
||||
// Create our FBO without a texture.
|
||||
status = createFBO(fbo, 0);
|
||||
|
||||
GLuint previous = gl.getDefaultFBO();
|
||||
if (current != this)
|
||||
{
|
||||
if (current != nullptr)
|
||||
previous = current->fbo;
|
||||
|
||||
gl.bindFramebuffer(GL_FRAMEBUFFER, fbo);
|
||||
}
|
||||
|
||||
// Create and attach the MSAA buffer for our FBO.
|
||||
status = createMSAABuffer(width, height, actual_samples, internalformat, msaa_buffer);
|
||||
|
||||
// Create the FBO used for the MSAA resolve, and attach the texture.
|
||||
if (status == GL_FRAMEBUFFER_COMPLETE)
|
||||
status = createFBO(resolve_fbo, texture);
|
||||
|
||||
if (status != GL_FRAMEBUFFER_COMPLETE)
|
||||
{
|
||||
// Clean up.
|
||||
glDeleteFramebuffers(1, &fbo);
|
||||
glDeleteFramebuffers(1, &resolve_fbo);
|
||||
glDeleteRenderbuffers(1, &msaa_buffer);
|
||||
fbo = msaa_buffer = resolve_fbo = 0;
|
||||
actual_samples = 0;
|
||||
}
|
||||
|
||||
if (current != this)
|
||||
gl.bindFramebuffer(GL_FRAMEBUFFER, previous);
|
||||
|
||||
return status == GL_FRAMEBUFFER_COMPLETE;
|
||||
}
|
||||
|
||||
bool Canvas::loadVolatile()
|
||||
{
|
||||
OpenGL::TempDebugGroup debuggroup("Canvas load");
|
||||
|
||||
fbo = depth_stencil = texture = 0;
|
||||
resolve_fbo = msaa_buffer = 0;
|
||||
fbo = texture = 0;
|
||||
msaa_buffer = 0;
|
||||
status = GL_FRAMEBUFFER_COMPLETE;
|
||||
|
||||
// glTexImage2D is guaranteed to error in this case.
|
||||
@@ -239,8 +192,8 @@ bool Canvas::loadVolatile()
|
||||
while (glGetError() != GL_NO_ERROR)
|
||||
/* Clear the error buffer. */;
|
||||
|
||||
glTexImage2D(GL_TEXTURE_2D, 0, iformat, width, height, 0,
|
||||
externalformat, textype, nullptr);
|
||||
glTexImage2D(GL_TEXTURE_2D, 0, iformat, width, height, 0, externalformat,
|
||||
textype, nullptr);
|
||||
|
||||
if (glGetError() != GL_NO_ERROR)
|
||||
{
|
||||
@@ -250,24 +203,27 @@ bool Canvas::loadVolatile()
|
||||
return false;
|
||||
}
|
||||
|
||||
// Try to create a MSAA FBO if requested. On failure (or no requested MSAA),
|
||||
// fall back to a regular FBO.
|
||||
if (!createMSAAFBO(internalformat))
|
||||
status = createFBO(fbo, texture);
|
||||
// Create a canvas-local FBO used for glReadPixels as well as MSAA blitting.
|
||||
status = createFBO(fbo, texture);
|
||||
|
||||
if (status != GL_FRAMEBUFFER_COMPLETE)
|
||||
{
|
||||
if (fbo != 0)
|
||||
{
|
||||
glDeleteFramebuffers(1, &fbo);
|
||||
fbo = 0;
|
||||
}
|
||||
{
|
||||
if (fbo != 0)
|
||||
{
|
||||
gl.deleteFramebuffer(fbo);
|
||||
fbo = 0;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
actual_samples = requested_samples == 1 ? 0 : requested_samples;
|
||||
|
||||
if (actual_samples > 0 && !createMSAABuffer(width, height, actual_samples, internalformat, msaa_buffer))
|
||||
actual_samples = 0;
|
||||
|
||||
size_t prevmemsize = texture_memory;
|
||||
|
||||
texture_memory = (getFormatBitsPerPixel(format) * width * height) / 8;
|
||||
texture_memory = ((getFormatBitsPerPixel(format) * width) / 8) * height;
|
||||
if (msaa_buffer != 0)
|
||||
texture_memory += (texture_memory * actual_samples);
|
||||
|
||||
@@ -278,33 +234,35 @@ bool Canvas::loadVolatile()
|
||||
|
||||
void Canvas::unloadVolatile()
|
||||
{
|
||||
glDeleteFramebuffers(1, &fbo);
|
||||
glDeleteFramebuffers(1, &resolve_fbo);
|
||||
if (fbo != 0)
|
||||
gl.deleteFramebuffer(fbo);
|
||||
|
||||
glDeleteRenderbuffers(1, &depth_stencil);
|
||||
glDeleteRenderbuffers(1, &msaa_buffer);
|
||||
if (msaa_buffer != 0)
|
||||
glDeleteRenderbuffers(1, &msaa_buffer);
|
||||
|
||||
gl.deleteTexture(texture);
|
||||
if (texture != 0)
|
||||
gl.deleteTexture(texture);
|
||||
|
||||
fbo = 0;
|
||||
resolve_fbo = 0;
|
||||
depth_stencil = 0;
|
||||
msaa_buffer = 0;
|
||||
texture = 0;
|
||||
|
||||
attachedCanvases.clear();
|
||||
|
||||
gl.updateTextureMemorySize(texture_memory, 0);
|
||||
texture_memory = 0;
|
||||
}
|
||||
|
||||
void Canvas::drawv(const Matrix4 &t, const Vertex *v)
|
||||
{
|
||||
// FIXME: This doesn't handle cases where the Canvas is used as a texture
|
||||
// in a SpriteBatch, Mesh, or ParticleSystem, or when the Canvas is used in
|
||||
// a shader as a non-default texture.
|
||||
if (Canvas::current == this)
|
||||
throw love::Exception("Cannot draw a Canvas to itself.");
|
||||
auto gfx = Module::getInstance<Graphics>(Module::M_GRAPHICS);
|
||||
if (gfx != nullptr)
|
||||
{
|
||||
const PassInfo &info = gfx->getActivePass();
|
||||
for (const auto &attachment : info.colorAttachments)
|
||||
{
|
||||
if (attachment.canvas == this)
|
||||
throw love::Exception("Cannot render a Canvas to itself!");
|
||||
}
|
||||
}
|
||||
|
||||
OpenGL::TempDebugGroup debuggroup("Canvas draw");
|
||||
|
||||
@@ -377,320 +335,6 @@ const void *Canvas::getHandle() const
|
||||
return &texture;
|
||||
}
|
||||
|
||||
void Canvas::setupGrab()
|
||||
{
|
||||
// already grabbing
|
||||
if (current == this)
|
||||
return;
|
||||
|
||||
// cleanup after previous Canvas
|
||||
if (current != nullptr)
|
||||
{
|
||||
systemViewport = current->systemViewport;
|
||||
current->stopGrab(true);
|
||||
}
|
||||
else
|
||||
systemViewport = gl.getViewport();
|
||||
|
||||
// indicate we are using this Canvas.
|
||||
current = this;
|
||||
|
||||
// bind the framebuffer object.
|
||||
gl.bindFramebuffer(GL_FRAMEBUFFER, fbo);
|
||||
gl.setViewport({0, 0, width, height});
|
||||
|
||||
// Set up the projection matrix
|
||||
gl.matrices.projection.push_back(Matrix4::ortho(0.0, (float) width, 0.0, (float) height));
|
||||
}
|
||||
|
||||
void Canvas::startGrab(const std::vector<Canvas *> &canvases)
|
||||
{
|
||||
// Whether the new canvas list is different from the old one.
|
||||
// A more thorough check is done below.
|
||||
bool canvaseschanged = canvases.size() != attachedCanvases.size();
|
||||
bool hasSRGBcanvas = getSizedFormat(format) == FORMAT_SRGB;
|
||||
|
||||
if (canvases.size() > 0)
|
||||
{
|
||||
if ((int) canvases.size() + 1 > gl.getMaxRenderTargets())
|
||||
throw love::Exception("This system can't simultaneously render to %d canvases.", canvases.size()+1);
|
||||
|
||||
if (actual_samples != 0)
|
||||
throw love::Exception("Multi-canvas rendering is not supported with MSAA.");
|
||||
}
|
||||
|
||||
bool multiformatsupported = isMultiFormatMultiCanvasSupported();
|
||||
|
||||
for (size_t i = 0; i < canvases.size(); i++)
|
||||
{
|
||||
if (canvases[i]->getWidth() != width || canvases[i]->getHeight() != height)
|
||||
throw love::Exception("All canvases must have the same dimensions.");
|
||||
|
||||
Format otherformat = canvases[i]->getTextureFormat();
|
||||
|
||||
if (otherformat != format && !multiformatsupported)
|
||||
throw love::Exception("This system doesn't support multi-canvas rendering with different canvas formats.");
|
||||
|
||||
if (canvases[i]->getMSAA() != 0)
|
||||
throw love::Exception("Multi-canvas rendering is not supported with MSAA.");
|
||||
|
||||
if (!canvaseschanged && canvases[i] != attachedCanvases[i])
|
||||
canvaseschanged = true;
|
||||
|
||||
if (getSizedFormat(otherformat) == FORMAT_SRGB)
|
||||
hasSRGBcanvas = true;
|
||||
}
|
||||
|
||||
OpenGL::TempDebugGroup debuggroup("Canvas set");
|
||||
|
||||
setupGrab();
|
||||
|
||||
// Make sure the correct sRGB setting is used when drawing to the canvases.
|
||||
if (GLAD_VERSION_1_0 || GLAD_EXT_sRGB_write_control)
|
||||
{
|
||||
if (hasSRGBcanvas && !gl.hasFramebufferSRGB())
|
||||
gl.setFramebufferSRGB(true);
|
||||
else if (!hasSRGBcanvas && gl.hasFramebufferSRGB())
|
||||
gl.setFramebufferSRGB(false);
|
||||
}
|
||||
|
||||
// Don't attach anything if there's nothing to change.
|
||||
if (!canvaseschanged)
|
||||
return;
|
||||
|
||||
// Attach the canvas textures to the active FBO and set up MRTs.
|
||||
std::vector<GLenum> drawbuffers;
|
||||
drawbuffers.reserve(canvases.size() + 1);
|
||||
|
||||
drawbuffers.push_back(GL_COLOR_ATTACHMENT0);
|
||||
|
||||
// Attach the canvas textures to the currently bound framebuffer.
|
||||
for (int i = 0; i < (int) canvases.size(); i++)
|
||||
{
|
||||
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT1 + i,
|
||||
GL_TEXTURE_2D, *(GLuint *) canvases[i]->getHandle(), 0);
|
||||
|
||||
drawbuffers.push_back(GL_COLOR_ATTACHMENT1 + i);
|
||||
}
|
||||
|
||||
// set up multiple render targets
|
||||
glDrawBuffers((int) drawbuffers.size(), &drawbuffers[0]);
|
||||
|
||||
// We want to avoid reference cycles, so we don't retain the attached
|
||||
// Canvases here. The code in Graphics::setCanvas retains them.
|
||||
|
||||
attachedCanvases = canvases;
|
||||
}
|
||||
|
||||
void Canvas::startGrab()
|
||||
{
|
||||
OpenGL::TempDebugGroup debuggroup("Canvas set");
|
||||
|
||||
setupGrab();
|
||||
|
||||
// Make sure the correct sRGB setting is used when drawing to the canvas.
|
||||
if (GLAD_VERSION_1_0 || GLAD_EXT_sRGB_write_control)
|
||||
{
|
||||
bool isSRGB = getSizedFormat(format) == FORMAT_SRGB;
|
||||
if (isSRGB && !gl.hasFramebufferSRGB())
|
||||
gl.setFramebufferSRGB(true);
|
||||
else if (!isSRGB && gl.hasFramebufferSRGB())
|
||||
gl.setFramebufferSRGB(false);
|
||||
}
|
||||
|
||||
if (attachedCanvases.size() > 0)
|
||||
{
|
||||
// Make sure the FBO is only using a single draw buffer.
|
||||
// GLES3 only has glDrawBuffers, so we avoid using glDrawBuffer.
|
||||
const GLenum buffers[] = {GL_COLOR_ATTACHMENT0};
|
||||
glDrawBuffers(1, buffers);
|
||||
|
||||
attachedCanvases.clear();
|
||||
}
|
||||
}
|
||||
|
||||
void Canvas::stopGrab(bool switchingToOtherCanvas)
|
||||
{
|
||||
// i am not grabbing. leave me alone
|
||||
if (current != this)
|
||||
return;
|
||||
|
||||
OpenGL::TempDebugGroup debuggroup("Canvas un-set");
|
||||
|
||||
// Make sure the canvas texture is up to date if we're using MSAA.
|
||||
resolveMSAA(false);
|
||||
|
||||
if (gl.matrices.projection.size() > 1)
|
||||
gl.matrices.projection.pop_back();
|
||||
|
||||
if (!switchingToOtherCanvas)
|
||||
{
|
||||
// bind system framebuffer.
|
||||
gl.bindFramebuffer(GL_FRAMEBUFFER, gl.getDefaultFBO());
|
||||
current = nullptr;
|
||||
gl.setViewport(systemViewport);
|
||||
|
||||
if (GLAD_VERSION_1_0 || GLAD_EXT_sRGB_write_control)
|
||||
{
|
||||
if (screenHasSRGB && !gl.hasFramebufferSRGB())
|
||||
gl.setFramebufferSRGB(true);
|
||||
else if (!screenHasSRGB && gl.hasFramebufferSRGB())
|
||||
gl.setFramebufferSRGB(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool Canvas::checkCreateStencil()
|
||||
{
|
||||
// Do nothing if we've already created the stencil buffer.
|
||||
if (depth_stencil != 0)
|
||||
return true;
|
||||
|
||||
OpenGL::TempDebugGroup debuggroup("Canvas create stencil");
|
||||
|
||||
if (current != this)
|
||||
gl.bindFramebuffer(GL_FRAMEBUFFER, fbo);
|
||||
|
||||
GLenum format = GL_STENCIL_INDEX8;
|
||||
std::vector<GLenum> attachments = {GL_STENCIL_ATTACHMENT};
|
||||
|
||||
// Prefer a combined depth/stencil buffer.
|
||||
if (GLAD_ES_VERSION_3_0 || GLAD_VERSION_3_0 || GLAD_ARB_framebuffer_object)
|
||||
{
|
||||
format = GL_DEPTH24_STENCIL8;
|
||||
attachments = {GL_DEPTH_STENCIL_ATTACHMENT};
|
||||
}
|
||||
else if (GLAD_EXT_packed_depth_stencil || GLAD_OES_packed_depth_stencil)
|
||||
{
|
||||
format = GL_DEPTH24_STENCIL8;
|
||||
attachments = {GL_DEPTH_ATTACHMENT, GL_STENCIL_ATTACHMENT};
|
||||
}
|
||||
|
||||
glGenRenderbuffers(1, &depth_stencil);
|
||||
glBindRenderbuffer(GL_RENDERBUFFER, depth_stencil);
|
||||
|
||||
if (requested_samples > 1)
|
||||
glRenderbufferStorageMultisample(GL_RENDERBUFFER, requested_samples, format, width, height);
|
||||
else
|
||||
glRenderbufferStorage(GL_RENDERBUFFER, format, width, height);
|
||||
|
||||
// Attach the buffer to the framebuffer object.
|
||||
for (GLenum attachment : attachments)
|
||||
glFramebufferRenderbuffer(GL_FRAMEBUFFER, attachment, GL_RENDERBUFFER, depth_stencil);
|
||||
|
||||
glBindRenderbuffer(GL_RENDERBUFFER, 0);
|
||||
|
||||
bool success = glCheckFramebufferStatus(GL_FRAMEBUFFER) == GL_FRAMEBUFFER_COMPLETE;
|
||||
|
||||
// We don't want the stencil buffer filled with garbage.
|
||||
if (success)
|
||||
glClear(GL_STENCIL_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
||||
else
|
||||
{
|
||||
glDeleteRenderbuffers(1, &depth_stencil);
|
||||
depth_stencil = 0;
|
||||
}
|
||||
|
||||
if (current && current != this)
|
||||
gl.bindFramebuffer(GL_FRAMEBUFFER, current->fbo);
|
||||
else if (!current)
|
||||
gl.bindFramebuffer(GL_FRAMEBUFFER, gl.getDefaultFBO());
|
||||
|
||||
return success;
|
||||
}
|
||||
|
||||
love::image::ImageData *Canvas::newImageData(love::image::Image *image, int x, int y, int w, int h)
|
||||
{
|
||||
if (x < 0 || y < 0 || w <= 0 || h <= 0 || (x + w) > width || (y + h) > height)
|
||||
throw love::Exception("Invalid ImageData rectangle dimensions.");
|
||||
|
||||
GLenum datatype = GL_UNSIGNED_BYTE;
|
||||
image::ImageData::Format imageformat = image::ImageData::FORMAT_RGBA8;
|
||||
|
||||
switch (getSizedFormat(format))
|
||||
{
|
||||
case FORMAT_RGB10A2: // FIXME: Conversions aren't supported in GLES
|
||||
datatype = GL_UNSIGNED_SHORT;
|
||||
imageformat = image::ImageData::FORMAT_RGBA16;
|
||||
break;
|
||||
case FORMAT_R16F:
|
||||
case FORMAT_RG16F:
|
||||
case FORMAT_RGBA16F:
|
||||
case FORMAT_RG11B10F: // FIXME: Conversions aren't supported in GLES
|
||||
datatype = GL_HALF_FLOAT;
|
||||
imageformat = image::ImageData::FORMAT_RGBA16F;
|
||||
break;
|
||||
case FORMAT_R32F:
|
||||
case FORMAT_RG32F:
|
||||
case FORMAT_RGBA32F:
|
||||
datatype = GL_FLOAT;
|
||||
imageformat = image::ImageData::FORMAT_RGBA32F;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
size_t size = w * h * image::ImageData::getPixelSize(imageformat);
|
||||
uint8 *pixels = nullptr;
|
||||
try
|
||||
{
|
||||
pixels = new uint8[size];
|
||||
}
|
||||
catch (std::bad_alloc &)
|
||||
{
|
||||
throw love::Exception("Out of memory.");
|
||||
}
|
||||
|
||||
// Make sure the canvas texture is up to date if we're using MSAA.
|
||||
if (current == this)
|
||||
resolveMSAA(false);
|
||||
|
||||
// Our texture is attached to 'resolve_fbo' when we use MSAA.
|
||||
if (resolve_fbo != 0)
|
||||
gl.bindFramebuffer(GL_READ_FRAMEBUFFER, resolve_fbo);
|
||||
else
|
||||
gl.bindFramebuffer(GL_FRAMEBUFFER, fbo);
|
||||
|
||||
glReadPixels(x, y, w, h, GL_RGBA, datatype, pixels);
|
||||
|
||||
GLuint prevfbo = current ? current->fbo : gl.getDefaultFBO();
|
||||
gl.bindFramebuffer(GL_FRAMEBUFFER, prevfbo);
|
||||
|
||||
// The new ImageData now owns the pixel data, so we don't delete it here.
|
||||
return image->newImageData(w, h, imageformat, pixels, true);
|
||||
}
|
||||
|
||||
bool Canvas::resolveMSAA(bool restoreprev)
|
||||
{
|
||||
if (resolve_fbo == 0 || msaa_buffer == 0)
|
||||
return false;
|
||||
|
||||
OpenGL::TempDebugGroup debuggroup("Canvas MSAA resolve");
|
||||
|
||||
GLint w = width;
|
||||
GLint h = height;
|
||||
|
||||
// Do the MSAA resolve by blitting the MSAA renderbuffer to the texture.
|
||||
// For many of the MSAA extensions that add suffixes to the functions, we
|
||||
// assign function pointers in OpenGL.cpp so we can call the core functions.
|
||||
|
||||
gl.bindFramebuffer(GL_READ_FRAMEBUFFER, fbo);
|
||||
gl.bindFramebuffer(GL_DRAW_FRAMEBUFFER, resolve_fbo);
|
||||
|
||||
if (GLAD_APPLE_framebuffer_multisample)
|
||||
glResolveMultisampleFramebufferAPPLE();
|
||||
else
|
||||
glBlitFramebuffer(0, 0, w, h, 0, 0, w, h, GL_COLOR_BUFFER_BIT, GL_NEAREST);
|
||||
|
||||
if (restoreprev)
|
||||
{
|
||||
GLuint fbo = current ? current->fbo : gl.getDefaultFBO();
|
||||
gl.bindFramebuffer(GL_FRAMEBUFFER, fbo);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
Canvas::Format Canvas::getSizedFormat(Canvas::Format format)
|
||||
{
|
||||
switch (format)
|
||||
@@ -949,7 +593,7 @@ bool Canvas::isFormatSupported(Canvas::Format format)
|
||||
|
||||
GLuint fbo = 0;
|
||||
supported = (createFBO(fbo, texture) == GL_FRAMEBUFFER_COMPLETE);
|
||||
glDeleteFramebuffers(1, &fbo);
|
||||
gl.deleteFramebuffer(fbo);
|
||||
|
||||
gl.deleteTexture(texture);
|
||||
|
||||
|
||||
@@ -82,26 +82,6 @@ public:
|
||||
bool setWrap(const Texture::Wrap &w) override;
|
||||
const void *getHandle() const override;
|
||||
|
||||
/**
|
||||
* @param canvases A list of other canvases to temporarily attach to this one,
|
||||
* to allow drawing to multiple canvases at once.
|
||||
**/
|
||||
void startGrab(const std::vector<Canvas *> &canvases);
|
||||
void startGrab();
|
||||
void stopGrab(bool switchingToOtherCanvas = false);
|
||||
|
||||
/**
|
||||
* Create and attach a stencil buffer to this Canvas' framebuffer, if necessary.
|
||||
**/
|
||||
bool checkCreateStencil();
|
||||
|
||||
love::image::ImageData *newImageData(love::image::Image *image, int x, int y, int w, int h);
|
||||
|
||||
inline const std::vector<Canvas *> &getAttachedCanvases() const
|
||||
{
|
||||
return attachedCanvases;
|
||||
}
|
||||
|
||||
inline GLenum getStatus() const
|
||||
{
|
||||
return status;
|
||||
@@ -117,19 +97,26 @@ public:
|
||||
return actual_samples;
|
||||
}
|
||||
|
||||
inline int getRequestedMSAA() const
|
||||
{
|
||||
return requested_samples;
|
||||
}
|
||||
|
||||
inline ptrdiff_t getMSAAHandle() const
|
||||
{
|
||||
return msaa_buffer;
|
||||
}
|
||||
|
||||
inline GLuint getFBO() const
|
||||
{
|
||||
return fbo;
|
||||
}
|
||||
|
||||
static Format getSizedFormat(Format format);
|
||||
static bool isSupported();
|
||||
static bool isMultiFormatMultiCanvasSupported();
|
||||
static bool isFormatSupported(Format format);
|
||||
|
||||
static Canvas *current;
|
||||
|
||||
// The viewport dimensions of the system (default) framebuffer.
|
||||
static OpenGL::Viewport systemViewport;
|
||||
|
||||
// Whether the main screen should have linear -> sRGB conversions enabled.
|
||||
static bool screenHasSRGB;
|
||||
|
||||
static int canvasCount;
|
||||
|
||||
static bool getConstant(const char *in, Format &out);
|
||||
@@ -137,29 +124,20 @@ public:
|
||||
|
||||
private:
|
||||
|
||||
void setupGrab();
|
||||
|
||||
bool createMSAAFBO(GLenum internalformat);
|
||||
bool resolveMSAA(bool restoreprev);
|
||||
|
||||
void drawv(const Matrix4 &t, const Vertex *v);
|
||||
|
||||
static void convertFormat(Format format, GLenum &internalformat, GLenum &externalformat, GLenum &type);
|
||||
static size_t getFormatBitsPerPixel(Format format);
|
||||
|
||||
GLuint fbo;
|
||||
GLuint resolve_fbo;
|
||||
|
||||
GLuint texture;
|
||||
GLuint msaa_buffer;
|
||||
GLuint depth_stencil;
|
||||
|
||||
Format format;
|
||||
|
||||
GLenum status;
|
||||
|
||||
std::vector<Canvas *> attachedCanvases;
|
||||
|
||||
int requested_samples;
|
||||
int actual_samples;
|
||||
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -24,6 +24,7 @@
|
||||
// STD
|
||||
#include <stack>
|
||||
#include <vector>
|
||||
#include <unordered_map>
|
||||
|
||||
// OpenGL
|
||||
#include "OpenGL.h"
|
||||
@@ -35,8 +36,6 @@
|
||||
#include "image/Image.h"
|
||||
#include "image/ImageData.h"
|
||||
|
||||
#include "window/Window.h"
|
||||
|
||||
#include "video/VideoStream.h"
|
||||
|
||||
#include "Font.h"
|
||||
@@ -53,21 +52,63 @@
|
||||
|
||||
namespace love
|
||||
{
|
||||
|
||||
class Reference;
|
||||
|
||||
namespace graphics
|
||||
{
|
||||
namespace opengl
|
||||
{
|
||||
|
||||
struct PassInfo
|
||||
{
|
||||
enum BeginAction
|
||||
{
|
||||
BEGIN_LOAD,
|
||||
BEGIN_CLEAR,
|
||||
BEGIN_DISCARD,
|
||||
};
|
||||
|
||||
enum EndAction
|
||||
{
|
||||
END_STORE,
|
||||
END_DISCARD,
|
||||
};
|
||||
|
||||
struct ColorAttachment
|
||||
{
|
||||
Canvas *canvas = nullptr;
|
||||
Colorf clearColor = Colorf(0.0f, 0.0f, 0.0f, 0.0f);
|
||||
BeginAction beginAction = BEGIN_LOAD;
|
||||
};
|
||||
|
||||
ColorAttachment colorAttachments[MAX_COLOR_RENDER_TARGETS];
|
||||
int colorAttachmentCount = 0;
|
||||
|
||||
bool stencil = false;
|
||||
|
||||
bool addColorAttachment(const ColorAttachment &attachment)
|
||||
{
|
||||
if (colorAttachmentCount + 1 < MAX_COLOR_RENDER_TARGETS)
|
||||
{
|
||||
colorAttachments[colorAttachmentCount++] = attachment;
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
class Graphics : public love::graphics::Graphics
|
||||
{
|
||||
public:
|
||||
|
||||
struct OptionalColorf
|
||||
{
|
||||
float r, g, b, a;
|
||||
bool enabled;
|
||||
typedef void (*ScreenshotCallback)(love::image::ImageData *i, Reference *ref, void *ud);
|
||||
|
||||
Colorf toColor() const { return Colorf(r, g, b, a); }
|
||||
struct ScreenshotInfo
|
||||
{
|
||||
ScreenshotCallback callback;
|
||||
Reference *ref;
|
||||
};
|
||||
|
||||
Graphics();
|
||||
@@ -92,25 +133,19 @@ public:
|
||||
**/
|
||||
void reset();
|
||||
|
||||
/**
|
||||
* Clears the screen to a specific color.
|
||||
**/
|
||||
void clear(Colorf c);
|
||||
void beginPass(PassInfo::BeginAction beginAction, Colorf clearColor);
|
||||
void beginPass(const PassInfo &info);
|
||||
|
||||
/**
|
||||
* Clears each active canvas to a different color.
|
||||
**/
|
||||
void clear(const std::vector<OptionalColorf> &colors);
|
||||
void endPass();
|
||||
void endPass(int sX, int sY, int sW, int sH, const ScreenshotInfo *info, void *screenshotCallbackData);
|
||||
|
||||
/**
|
||||
* Discards the contents of the screen.
|
||||
**/
|
||||
void discard(const std::vector<bool> &colorbuffers, bool stencil);
|
||||
const PassInfo &getActivePass() const;
|
||||
virtual bool isPassActive() const;
|
||||
|
||||
/**
|
||||
* Flips buffers. (Rendered geometry is presented on screen).
|
||||
**/
|
||||
void present();
|
||||
void present(void *screenshotCallbackData);
|
||||
|
||||
/**
|
||||
* Gets the width of the current graphics viewport.
|
||||
@@ -122,6 +157,9 @@ public:
|
||||
**/
|
||||
int getHeight() const;
|
||||
|
||||
int getPassWidth() const;
|
||||
int getPassHeight() const;
|
||||
|
||||
/**
|
||||
* True if a graphics viewport is set.
|
||||
**/
|
||||
@@ -231,13 +269,6 @@ public:
|
||||
|
||||
Shader *getShader() const;
|
||||
|
||||
void setCanvas(Canvas *canvas);
|
||||
void setCanvas(const std::vector<Canvas *> &canvases);
|
||||
void setCanvas(const std::vector<StrongRef<Canvas>> &canvases);
|
||||
void setCanvas();
|
||||
|
||||
std::vector<Canvas *> getCanvas() const;
|
||||
|
||||
/**
|
||||
* Sets the enabled color components when rendering.
|
||||
**/
|
||||
@@ -330,6 +361,9 @@ public:
|
||||
**/
|
||||
bool isWireframe() const;
|
||||
|
||||
void draw(Drawable *drawable, const Matrix4 &m);
|
||||
void drawq(Texture *texture, Quad *quad, const Matrix4 &m);
|
||||
|
||||
/**
|
||||
* Draws text at the specified coordinates
|
||||
**/
|
||||
@@ -422,12 +456,7 @@ public:
|
||||
**/
|
||||
void polygon(DrawMode mode, const float *coords, size_t count);
|
||||
|
||||
/**
|
||||
* Creates a screenshot of the view and saves it to the default folder.
|
||||
* @param image The love.image module.
|
||||
* @param copyAlpha If the alpha channel should be copied or set to full opacity (1.0).
|
||||
**/
|
||||
love::image::ImageData *newScreenshot(love::image::Image *image, bool copyAlpha = true);
|
||||
void captureScreenshot(const ScreenshotInfo &info);
|
||||
|
||||
/**
|
||||
* Returns system-dependent renderer information.
|
||||
@@ -488,8 +517,6 @@ private:
|
||||
StrongRef<Font> font;
|
||||
StrongRef<Shader> shader;
|
||||
|
||||
std::vector<StrongRef<Canvas>> canvases;
|
||||
|
||||
ColorMask colorMask = ColorMask(true, true, true, true);
|
||||
|
||||
bool wireframe = false;
|
||||
@@ -500,19 +527,47 @@ private:
|
||||
float defaultMipmapSharpness = 0.0f;
|
||||
};
|
||||
|
||||
struct CurrentPass
|
||||
{
|
||||
PassInfo info;
|
||||
bool active;
|
||||
};
|
||||
|
||||
struct PassBufferInfo
|
||||
{
|
||||
bool stencil;
|
||||
Canvas *canvases[MAX_COLOR_RENDER_TARGETS];
|
||||
};
|
||||
|
||||
struct CachedRenderbuffer
|
||||
{
|
||||
int w;
|
||||
int h;
|
||||
int samples;
|
||||
GLenum attachments[2];
|
||||
GLuint renderbuffer;
|
||||
};
|
||||
|
||||
void restoreState(const DisplayState &s);
|
||||
void restoreStateChecked(const DisplayState &s);
|
||||
|
||||
void bindCachedFBOForPass(const PassInfo &pass);
|
||||
void discard(OpenGL::FramebufferTarget target, const std::vector<bool> &colorbuffers, bool depthstencil);
|
||||
GLuint attachCachedStencilBuffer(int w, int h, int samples);
|
||||
|
||||
void checkSetDefaultFont();
|
||||
|
||||
int calculateEllipsePoints(float rx, float ry) const;
|
||||
|
||||
StrongRef<love::window::Window> currentWindow;
|
||||
|
||||
StrongRef<Font> defaultFont;
|
||||
|
||||
std::vector<double> pixelSizeStack; // stores current size of a pixel (needed for line drawing)
|
||||
|
||||
std::vector<ScreenshotInfo> pendingScreenshotCallbacks;
|
||||
|
||||
std::unordered_map<uint32, GLuint> framebufferObjects;
|
||||
std::vector<CachedRenderbuffer> stencilBuffers;
|
||||
|
||||
QuadIndices *quadIndices;
|
||||
|
||||
int width;
|
||||
@@ -520,11 +575,17 @@ private:
|
||||
bool created;
|
||||
bool active;
|
||||
|
||||
bool canCaptureScreenshot;
|
||||
|
||||
CurrentPass currentPass;
|
||||
|
||||
bool writingToStencil;
|
||||
|
||||
std::vector<DisplayState> states;
|
||||
std::vector<StackType> stackTypes; // Keeps track of the pushed stack types.
|
||||
|
||||
int renderPassCount;
|
||||
|
||||
static const size_t MAX_USER_STACK_DEPTH = 64;
|
||||
|
||||
}; // Graphics
|
||||
|
||||
@@ -23,7 +23,6 @@
|
||||
#include "OpenGL.h"
|
||||
|
||||
#include "Shader.h"
|
||||
#include "Canvas.h"
|
||||
#include "common/Exception.h"
|
||||
|
||||
// C++
|
||||
@@ -135,6 +134,10 @@ void OpenGL::setupContext()
|
||||
else
|
||||
state.pointSize = 1.0f;
|
||||
|
||||
for (int i = 0; i < 2; i++)
|
||||
state.boundFramebuffers[i] = std::numeric_limits<GLuint>::max();
|
||||
bindFramebuffer(FRAMEBUFFER_ALL, getDefaultFBO());
|
||||
|
||||
if (GLAD_VERSION_3_0 || GLAD_ARB_framebuffer_sRGB || GLAD_EXT_framebuffer_sRGB
|
||||
|| GLAD_EXT_sRGB_write_control)
|
||||
{
|
||||
@@ -283,7 +286,7 @@ void OpenGL::initMaxValues()
|
||||
glGetIntegerv(GL_MAX_DRAW_BUFFERS, &maxdrawbuffers);
|
||||
}
|
||||
|
||||
maxRenderTargets = std::min(maxattachments, maxdrawbuffers);
|
||||
maxRenderTargets = std::max(std::min(maxattachments, maxdrawbuffers), 1);
|
||||
|
||||
if (GLAD_ES_VERSION_3_0 || GLAD_VERSION_3_0 || GLAD_ARB_framebuffer_object
|
||||
|| GLAD_EXT_framebuffer_multisample || GLAD_APPLE_framebuffer_multisample
|
||||
@@ -463,7 +466,7 @@ void OpenGL::useVertexAttribArrays(uint32 arraybits)
|
||||
glVertexAttrib4f(ATTRIB_COLOR, 1.0f, 1.0f, 1.0f, 1.0f);
|
||||
}
|
||||
|
||||
void OpenGL::setViewport(const OpenGL::Viewport &v)
|
||||
void OpenGL::setViewport(const OpenGL::Viewport &v, bool canvasActive)
|
||||
{
|
||||
glViewport(v.x, v.y, v.w, v.h);
|
||||
state.viewport = v;
|
||||
@@ -471,7 +474,7 @@ void OpenGL::setViewport(const OpenGL::Viewport &v)
|
||||
// glScissor starts from the lower left, so we compensate when setting the
|
||||
// scissor. When the viewport is changed, we need to manually update the
|
||||
// scissor again.
|
||||
setScissor(state.scissor);
|
||||
setScissor(state.scissor, canvasActive);
|
||||
}
|
||||
|
||||
OpenGL::Viewport OpenGL::getViewport() const
|
||||
@@ -479,9 +482,9 @@ OpenGL::Viewport OpenGL::getViewport() const
|
||||
return state.viewport;
|
||||
}
|
||||
|
||||
void OpenGL::setScissor(const OpenGL::Viewport &v)
|
||||
void OpenGL::setScissor(const OpenGL::Viewport &v, bool canvasActive)
|
||||
{
|
||||
if (Canvas::current)
|
||||
if (canvasActive)
|
||||
glScissor(v.x, v.y, v.w, v.h);
|
||||
else
|
||||
{
|
||||
@@ -526,12 +529,53 @@ bool OpenGL::hasFramebufferSRGB() const
|
||||
return state.framebufferSRGBEnabled;
|
||||
}
|
||||
|
||||
void OpenGL::bindFramebuffer(GLenum target, GLuint framebuffer)
|
||||
void OpenGL::bindFramebuffer(FramebufferTarget target, GLuint framebuffer)
|
||||
{
|
||||
glBindFramebuffer(target, framebuffer);
|
||||
bool bindingmodified = false;
|
||||
|
||||
if (target == GL_FRAMEBUFFER)
|
||||
++stats.framebufferBinds;
|
||||
if ((target & FRAMEBUFFER_DRAW) && state.boundFramebuffers[0] != framebuffer)
|
||||
{
|
||||
bindingmodified = true;
|
||||
state.boundFramebuffers[0] = framebuffer;
|
||||
}
|
||||
|
||||
if ((target & FRAMEBUFFER_READ) && state.boundFramebuffers[1] != framebuffer)
|
||||
{
|
||||
bindingmodified = true;
|
||||
state.boundFramebuffers[1] = framebuffer;
|
||||
}
|
||||
|
||||
if (bindingmodified)
|
||||
{
|
||||
GLenum gltarget = GL_FRAMEBUFFER;
|
||||
if (target == FRAMEBUFFER_DRAW)
|
||||
gltarget = GL_DRAW_FRAMEBUFFER;
|
||||
else if (target == FRAMEBUFFER_READ)
|
||||
gltarget = GL_READ_FRAMEBUFFER;
|
||||
|
||||
glBindFramebuffer(gltarget, framebuffer);
|
||||
}
|
||||
}
|
||||
|
||||
GLenum OpenGL::getFramebuffer(FramebufferTarget target) const
|
||||
{
|
||||
if (target & FRAMEBUFFER_DRAW)
|
||||
return state.boundFramebuffers[0];
|
||||
else if (target & FRAMEBUFFER_READ)
|
||||
return state.boundFramebuffers[1];
|
||||
else
|
||||
return 0;
|
||||
}
|
||||
|
||||
void OpenGL::deleteFramebuffer(GLuint framebuffer)
|
||||
{
|
||||
glDeleteFramebuffers(1, &framebuffer);
|
||||
|
||||
for (int i = 0; i < 2; i++)
|
||||
{
|
||||
if (state.boundFramebuffers[i] == framebuffer)
|
||||
state.boundFramebuffers[i] = 0;
|
||||
}
|
||||
}
|
||||
|
||||
void OpenGL::useProgram(GLuint program)
|
||||
@@ -680,7 +724,7 @@ int OpenGL::getMaxTextureSize() const
|
||||
|
||||
int OpenGL::getMaxRenderTargets() const
|
||||
{
|
||||
return maxRenderTargets;
|
||||
return std::min(maxRenderTargets, MAX_COLOR_RENDER_TARGETS);
|
||||
}
|
||||
|
||||
int OpenGL::getMaxRenderbufferSamples() const
|
||||
@@ -739,6 +783,36 @@ const char *OpenGL::errorString(GLenum errorcode)
|
||||
return text;
|
||||
}
|
||||
|
||||
const char *OpenGL::framebufferStatusString(GLenum status)
|
||||
{
|
||||
switch (status)
|
||||
{
|
||||
case GL_FRAMEBUFFER_COMPLETE:
|
||||
return "complete (success)";
|
||||
case GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT:
|
||||
return "Texture format cannot be rendered to on this system.";
|
||||
case GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT:
|
||||
return "Error in graphics driver (missing render texture attachment)";
|
||||
case GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER:
|
||||
return "Error in graphics driver (incomplete draw buffer)";
|
||||
case GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER:
|
||||
return "Error in graphics driver (incomplete read buffer)";
|
||||
case GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE:
|
||||
return "Canvas with the specified MSAA count cannot be rendered to on this system.";
|
||||
case GL_FRAMEBUFFER_UNSUPPORTED:
|
||||
return "Renderable textures are unsupported";
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
static char text[64] = {};
|
||||
|
||||
memset(text, 0, sizeof(text));
|
||||
sprintf(text, "0x%x", status);
|
||||
|
||||
return text;
|
||||
}
|
||||
|
||||
const char *OpenGL::debugSeverityString(GLenum severity)
|
||||
{
|
||||
switch (severity)
|
||||
|
||||
@@ -104,6 +104,13 @@ public:
|
||||
VENDOR_UNKNOWN
|
||||
};
|
||||
|
||||
enum FramebufferTarget
|
||||
{
|
||||
FRAMEBUFFER_READ = (1 << 1),
|
||||
FRAMEBUFFER_DRAW = (1 << 2),
|
||||
FRAMEBUFFER_ALL = (FRAMEBUFFER_READ | FRAMEBUFFER_DRAW),
|
||||
};
|
||||
|
||||
// A rectangle representing an OpenGL viewport or a scissor box.
|
||||
struct Viewport
|
||||
{
|
||||
@@ -173,7 +180,6 @@ public:
|
||||
{
|
||||
size_t textureMemory;
|
||||
int drawCalls;
|
||||
int framebufferBinds;
|
||||
int shaderSwitches;
|
||||
} stats;
|
||||
|
||||
@@ -282,7 +288,7 @@ public:
|
||||
* Sets the OpenGL rendering viewport to the specified rectangle.
|
||||
* The y-coordinate starts at the top.
|
||||
**/
|
||||
void setViewport(const Viewport &v);
|
||||
void setViewport(const Viewport &v, bool canvasActive);
|
||||
|
||||
/**
|
||||
* Gets the current OpenGL rendering viewport rectangle.
|
||||
@@ -293,7 +299,7 @@ public:
|
||||
* Sets the scissor box to the specified rectangle.
|
||||
* The y-coordinate starts at the top and is flipped internally.
|
||||
**/
|
||||
void setScissor(const Viewport &v);
|
||||
void setScissor(const Viewport &v, bool canvasActive);
|
||||
|
||||
/**
|
||||
* Gets the current scissor box (regardless of whether scissoring is enabled.)
|
||||
@@ -323,7 +329,9 @@ public:
|
||||
/**
|
||||
* Binds a Framebuffer Object to the specified target.
|
||||
**/
|
||||
void bindFramebuffer(GLenum target, GLuint framebuffer);
|
||||
void bindFramebuffer(FramebufferTarget target, GLuint framebuffer);
|
||||
GLuint getFramebuffer(FramebufferTarget target) const;
|
||||
void deleteFramebuffer(GLuint framebuffer);
|
||||
|
||||
/**
|
||||
* Calls glUseProgram.
|
||||
@@ -413,6 +421,7 @@ public:
|
||||
static GLint getGLWrapMode(Texture::WrapMode wmode);
|
||||
|
||||
static const char *errorString(GLenum errorcode);
|
||||
static const char *framebufferStatusString(GLenum status);
|
||||
|
||||
// Get human-readable strings for debug info.
|
||||
static const char *debugSeverityString(GLenum severity);
|
||||
@@ -456,6 +465,8 @@ private:
|
||||
|
||||
float pointSize;
|
||||
|
||||
GLuint boundFramebuffers[2];
|
||||
|
||||
bool framebufferSRGBEnabled;
|
||||
|
||||
GLuint defaultTexture;
|
||||
|
||||
@@ -22,7 +22,7 @@
|
||||
#include "common/config.h"
|
||||
|
||||
#include "Shader.h"
|
||||
#include "Canvas.h"
|
||||
#include "Graphics.h"
|
||||
|
||||
// C++
|
||||
#include <algorithm>
|
||||
@@ -76,7 +76,7 @@ Shader::Shader(const ShaderSource &source)
|
||||
, program(0)
|
||||
, builtinUniforms()
|
||||
, builtinAttributes()
|
||||
, lastCanvas((Canvas *) -1)
|
||||
, canvasWasActive(false)
|
||||
, lastViewport()
|
||||
, lastPointSize(0.0f)
|
||||
, videoTextureUnits()
|
||||
@@ -237,7 +237,7 @@ bool Shader::loadVolatile()
|
||||
OpenGL::TempDebugGroup debuggroup("Shader load");
|
||||
|
||||
// Recreating the shader program will invalidate uniforms that rely on these.
|
||||
lastCanvas = (Canvas *) -1;
|
||||
canvasWasActive = false;
|
||||
lastViewport = OpenGL::Viewport();
|
||||
|
||||
lastPointSize = -1.0f;
|
||||
@@ -339,11 +339,11 @@ bool Shader::loadVolatile()
|
||||
|
||||
void Shader::unloadVolatile()
|
||||
{
|
||||
if (current == this)
|
||||
gl.useProgram(0);
|
||||
|
||||
if (program != 0)
|
||||
{
|
||||
if (current == this)
|
||||
gl.useProgram(0);
|
||||
|
||||
glDeleteProgram(program);
|
||||
program = 0;
|
||||
}
|
||||
@@ -686,7 +686,10 @@ void Shader::checkSetScreenParams()
|
||||
{
|
||||
OpenGL::Viewport view = gl.getViewport();
|
||||
|
||||
if (view == lastViewport && lastCanvas == Canvas::current)
|
||||
auto gfx = Module::getInstance<Graphics>(Module::M_GRAPHICS);
|
||||
bool canvasActive = gfx->getActivePass().colorAttachmentCount > 0;
|
||||
|
||||
if (view == lastViewport && canvasWasActive == canvasActive)
|
||||
return;
|
||||
|
||||
// In the shader, we do pixcoord.y = gl_FragCoord.y * params.z + params.w.
|
||||
@@ -697,7 +700,7 @@ void Shader::checkSetScreenParams()
|
||||
0.0f, 0.0f,
|
||||
};
|
||||
|
||||
if (Canvas::current != nullptr)
|
||||
if (canvasActive)
|
||||
{
|
||||
// No flipping: pixcoord.y = gl_FragCoord.y * 1.0 + 0.0.
|
||||
params[2] = 1.0f;
|
||||
@@ -719,7 +722,7 @@ void Shader::checkSetScreenParams()
|
||||
glUniform4fv(location, 1, params);
|
||||
}
|
||||
|
||||
lastCanvas = Canvas::current;
|
||||
canvasWasActive = canvasActive;
|
||||
lastViewport = view;
|
||||
}
|
||||
|
||||
|
||||
@@ -41,8 +41,6 @@ namespace graphics
|
||||
namespace opengl
|
||||
{
|
||||
|
||||
class Canvas;
|
||||
|
||||
// A GLSL shader
|
||||
class Shader : public Object, public Volatile
|
||||
{
|
||||
@@ -245,8 +243,7 @@ private:
|
||||
// Uniform name to retainable objects
|
||||
std::map<std::string, Object*> boundRetainables;
|
||||
|
||||
// Pointer to the active Canvas when the screen params were last checked.
|
||||
Canvas *lastCanvas;
|
||||
bool canvasWasActive;
|
||||
OpenGL::Viewport lastViewport;
|
||||
|
||||
float lastPointSize;
|
||||
|
||||
@@ -33,55 +33,6 @@ Canvas *luax_checkcanvas(lua_State *L, int idx)
|
||||
return luax_checktype<Canvas>(L, idx, GRAPHICS_CANVAS_ID);
|
||||
}
|
||||
|
||||
int w_Canvas_renderTo(lua_State *L)
|
||||
{
|
||||
Canvas *canvas = luax_checkcanvas(L, 1);
|
||||
luaL_checktype(L, 2, LUA_TFUNCTION);
|
||||
|
||||
auto graphics = Module::getInstance<Graphics>(Module::M_GRAPHICS);
|
||||
|
||||
if (graphics)
|
||||
{
|
||||
// Save the current Canvas so we can restore it when we're done.
|
||||
std::vector<Canvas *> oldcanvases = graphics->getCanvas();
|
||||
|
||||
for (Canvas *c : oldcanvases)
|
||||
c->retain();
|
||||
|
||||
luax_catchexcept(L, [&](){ graphics->setCanvas(canvas); });
|
||||
|
||||
lua_settop(L, 2); // make sure the function is on top of the stack
|
||||
int status = lua_pcall(L, 0, 0, 0);
|
||||
|
||||
graphics->setCanvas(oldcanvases);
|
||||
|
||||
for (Canvas *c : oldcanvases)
|
||||
c->release();
|
||||
|
||||
if (status != 0)
|
||||
return lua_error(L);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int w_Canvas_newImageData(lua_State *L)
|
||||
{
|
||||
Canvas *canvas = luax_checkcanvas(L, 1);
|
||||
love::image::Image *image = luax_getmodule<love::image::Image>(L, MODULE_IMAGE_ID);
|
||||
int x = (int) luaL_optnumber(L, 2, 0);
|
||||
int y = (int) luaL_optnumber(L, 3, 0);
|
||||
int w = (int) luaL_optnumber(L, 4, canvas->getWidth());
|
||||
int h = (int) luaL_optnumber(L, 5, canvas->getHeight());
|
||||
|
||||
love::image::ImageData *img = nullptr;
|
||||
luax_catchexcept(L, [&](){ img = canvas->newImageData(image, x, y, w, h); });
|
||||
|
||||
luax_pushtype(L, IMAGE_IMAGE_DATA_ID, img);
|
||||
img->release();
|
||||
return 1;
|
||||
}
|
||||
|
||||
int w_Canvas_getFormat(lua_State *L)
|
||||
{
|
||||
Canvas *canvas = luax_checkcanvas(L, 1);
|
||||
@@ -103,8 +54,6 @@ int w_Canvas_getMSAA(lua_State *L)
|
||||
|
||||
static const luaL_Reg w_Canvas_functions[] =
|
||||
{
|
||||
{ "renderTo", w_Canvas_renderTo },
|
||||
{ "newImageData", w_Canvas_newImageData },
|
||||
{ "getFormat", w_Canvas_getFormat },
|
||||
{ "getMSAA", w_Canvas_getMSAA },
|
||||
{ 0, 0 }
|
||||
|
||||
@@ -27,6 +27,7 @@
|
||||
#include "filesystem/wrap_Filesystem.h"
|
||||
#include "video/VideoStream.h"
|
||||
#include "image/wrap_Image.h"
|
||||
#include "common/Reference.h"
|
||||
|
||||
#include <cassert>
|
||||
#include <cstring>
|
||||
@@ -60,79 +61,9 @@ int w_reset(lua_State *)
|
||||
return 0;
|
||||
}
|
||||
|
||||
int w_clear(lua_State *L)
|
||||
int w_present(lua_State *L)
|
||||
{
|
||||
Colorf color;
|
||||
|
||||
if (lua_isnoneornil(L, 1))
|
||||
color.set(0.0, 0.0, 0.0, 0.0);
|
||||
else if (lua_istable(L, 1))
|
||||
{
|
||||
std::vector<Graphics::OptionalColorf> colors((size_t) lua_gettop(L));
|
||||
|
||||
for (int i = 0; i < lua_gettop(L); i++)
|
||||
{
|
||||
if (lua_isnoneornil(L, i + 1) || luax_objlen(L, i + 1) == 0)
|
||||
{
|
||||
colors[i].enabled = false;
|
||||
continue;
|
||||
}
|
||||
|
||||
for (int j = 1; j <= 4; j++)
|
||||
lua_rawgeti(L, i + 1, j);
|
||||
|
||||
colors[i].enabled = true;
|
||||
colors[i].r = (float) luaL_checknumber(L, -4);
|
||||
colors[i].g = (float) luaL_checknumber(L, -3);
|
||||
colors[i].b = (float) luaL_checknumber(L, -2);
|
||||
colors[i].a = (float) luaL_optnumber(L, -1, 1.0);
|
||||
|
||||
lua_pop(L, 4);
|
||||
}
|
||||
|
||||
luax_catchexcept(L, [&]() { instance()->clear(colors); });
|
||||
return 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
color.r = (float) luaL_checknumber(L, 1);
|
||||
color.g = (float) luaL_checknumber(L, 2);
|
||||
color.b = (float) luaL_checknumber(L, 3);
|
||||
color.a = (float) luaL_optnumber(L, 4, 1.0);
|
||||
}
|
||||
|
||||
luax_catchexcept(L, [&]() { instance()->clear(color); });
|
||||
return 0;
|
||||
}
|
||||
|
||||
int w_discard(lua_State *L)
|
||||
{
|
||||
std::vector<bool> colorbuffers;
|
||||
|
||||
if (lua_istable(L, 1))
|
||||
{
|
||||
for (size_t i = 1; i <= luax_objlen(L, 1); i++)
|
||||
{
|
||||
lua_rawgeti(L, 1, i);
|
||||
colorbuffers.push_back(luax_optboolean(L, -1, true));
|
||||
lua_pop(L, 1);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
bool discardcolor = luax_optboolean(L, 1, true);
|
||||
size_t numbuffers = std::max((size_t) 1, instance()->getCanvas().size());
|
||||
colorbuffers = std::vector<bool>(numbuffers, discardcolor);
|
||||
}
|
||||
|
||||
bool stencil = luax_optboolean(L, 2, true);
|
||||
instance()->discard(colorbuffers, stencil);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int w_present(lua_State *)
|
||||
{
|
||||
instance()->present();
|
||||
luax_catchexcept(L, [&]() { instance()->present(L); });
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -173,6 +104,274 @@ int w_getDimensions(lua_State *L)
|
||||
return 2;
|
||||
}
|
||||
|
||||
int w_getPassWidth(lua_State *L)
|
||||
{
|
||||
lua_pushinteger(L, instance()->getPassWidth());
|
||||
return 1;
|
||||
}
|
||||
|
||||
int w_getPassHeight(lua_State *L)
|
||||
{
|
||||
lua_pushinteger(L, instance()->getPassHeight());
|
||||
return 1;
|
||||
}
|
||||
|
||||
int w_getPassDimensions(lua_State *L)
|
||||
{
|
||||
lua_pushinteger(L, instance()->getPassWidth());
|
||||
lua_pushinteger(L, instance()->getPassHeight());
|
||||
return 2;
|
||||
}
|
||||
|
||||
static int w__beginPass(lua_State *L)
|
||||
{
|
||||
int nextstartidx = 1;
|
||||
|
||||
if (lua_isnoneornil(L, 1))
|
||||
{
|
||||
luax_catchexcept(L, [&]() { instance()->beginPass(PassInfo::BEGIN_LOAD, Colorf()); });
|
||||
nextstartidx = 1;
|
||||
}
|
||||
else if (lua_isnumber(L, 1))
|
||||
{
|
||||
Colorf c;
|
||||
c.r = (float) luaL_checknumber(L, 1);
|
||||
c.g = (float) luaL_checknumber(L, 2);
|
||||
c.b = (float) luaL_checknumber(L, 3);
|
||||
|
||||
if (lua_isnumber(L, 4))
|
||||
{
|
||||
c.a = (float) lua_tonumber(L, 4);
|
||||
nextstartidx = 5;
|
||||
}
|
||||
else
|
||||
{
|
||||
c.a = 1.0f;
|
||||
nextstartidx = 4;
|
||||
}
|
||||
|
||||
luax_catchexcept(L, [&]() { instance()->beginPass(PassInfo::BEGIN_CLEAR, c); });
|
||||
}
|
||||
else if (luax_istype(L, 1, GRAPHICS_CANVAS_ID))
|
||||
{
|
||||
PassInfo::ColorAttachment attachment;
|
||||
attachment.canvas = luax_checkcanvas(L, 1);
|
||||
attachment.beginAction = PassInfo::BEGIN_LOAD;
|
||||
|
||||
if (lua_isnumber(L, 2))
|
||||
{
|
||||
attachment.beginAction = PassInfo::BEGIN_CLEAR;
|
||||
attachment.clearColor.r = (float) luaL_checknumber(L, 2);
|
||||
attachment.clearColor.g = (float) luaL_checknumber(L, 3);
|
||||
attachment.clearColor.b = (float) luaL_checknumber(L, 4);
|
||||
|
||||
if (lua_isnumber(L, 5))
|
||||
{
|
||||
attachment.clearColor.a = (float) lua_tonumber(L, 5);
|
||||
nextstartidx = 6;
|
||||
}
|
||||
else
|
||||
{
|
||||
attachment.clearColor.a = 1.0f;
|
||||
nextstartidx = 5;
|
||||
}
|
||||
}
|
||||
else
|
||||
nextstartidx = 2;
|
||||
|
||||
PassInfo info;
|
||||
info.addColorAttachment(attachment);
|
||||
|
||||
if (lua_isboolean(L, nextstartidx))
|
||||
{
|
||||
info.stencil = luax_toboolean(L, nextstartidx);
|
||||
nextstartidx++;
|
||||
}
|
||||
else
|
||||
info.stencil = false;
|
||||
|
||||
luax_catchexcept(L, [&]() { instance()->beginPass(info); });
|
||||
}
|
||||
else
|
||||
{
|
||||
luaL_checktype(L, 1, LUA_TTABLE);
|
||||
|
||||
int nattachments = std::max((int) luax_objlen(L, 1), 1);
|
||||
|
||||
if (nattachments > MAX_COLOR_RENDER_TARGETS)
|
||||
return luaL_error(L, "Cannot render to %d Canvases at once!", nattachments);
|
||||
|
||||
PassInfo info;
|
||||
|
||||
for (int i = 1; i <= nattachments; i++)
|
||||
{
|
||||
lua_rawgeti(L, 1, i);
|
||||
luaL_checktype(L, -1, LUA_TTABLE);
|
||||
|
||||
PassInfo::ColorAttachment attachment;
|
||||
attachment.beginAction = PassInfo::BEGIN_LOAD;
|
||||
|
||||
lua_rawgeti(L, -1, 1);
|
||||
attachment.canvas = luax_checkcanvas(L, -1);
|
||||
|
||||
lua_rawgeti(L, -2, 2);
|
||||
if (!lua_isnoneornil(L, -1))
|
||||
{
|
||||
attachment.beginAction = PassInfo::BEGIN_CLEAR;
|
||||
|
||||
for (int j = 3; j < 6; j++)
|
||||
lua_rawgeti(L, -j, j);
|
||||
|
||||
attachment.clearColor.r = (float) luaL_checknumber(L, -4);
|
||||
attachment.clearColor.g = (float) luaL_checknumber(L, -3);
|
||||
attachment.clearColor.b = (float) luaL_checknumber(L, -2);
|
||||
attachment.clearColor.a = (float) luaL_optnumber(L, -1, 1.0);
|
||||
}
|
||||
|
||||
lua_pop(L, 2 + (attachment.beginAction == PassInfo::BEGIN_CLEAR ? 4 : 1));
|
||||
|
||||
info.addColorAttachment(attachment);
|
||||
}
|
||||
|
||||
info.stencil = luax_boolflag(L, 1, "stencil", false);
|
||||
|
||||
luax_catchexcept(L, [&]() { instance()->beginPass(info); });
|
||||
|
||||
nextstartidx = 2;
|
||||
}
|
||||
|
||||
return nextstartidx;
|
||||
}
|
||||
|
||||
int w_beginPass(lua_State *L)
|
||||
{
|
||||
w__beginPass(L);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void screenshotCallback(love::image::ImageData *i, Reference *ref, void *gd)
|
||||
{
|
||||
if (i != nullptr)
|
||||
{
|
||||
lua_State *L = (lua_State *) gd;
|
||||
ref->push(L);
|
||||
delete ref;
|
||||
luax_pushtype(L, IMAGE_IMAGE_DATA_ID, i);
|
||||
lua_call(L, 1, 0);
|
||||
}
|
||||
else
|
||||
delete ref;
|
||||
}
|
||||
|
||||
static int w__endPass(lua_State *L, int startidx)
|
||||
{
|
||||
if (lua_isnoneornil(L, startidx))
|
||||
{
|
||||
luax_catchexcept(L, []() { instance()->endPass(); });
|
||||
}
|
||||
else
|
||||
{
|
||||
int x, y, w, h;
|
||||
|
||||
if (lua_isnumber(L, startidx))
|
||||
{
|
||||
x = (int) luaL_checknumber(L, 1);
|
||||
y = (int) luaL_checknumber(L, 2);
|
||||
w = (int) luaL_checknumber(L, 3);
|
||||
h = (int) luaL_checknumber(L, 4);
|
||||
startidx += 4;
|
||||
}
|
||||
else
|
||||
{
|
||||
x = 0;
|
||||
y = 0;
|
||||
w = instance()->getPassWidth();
|
||||
h = instance()->getPassHeight();
|
||||
}
|
||||
|
||||
luaL_checktype(L, startidx, LUA_TFUNCTION);
|
||||
|
||||
Graphics::ScreenshotInfo info;
|
||||
info.callback = screenshotCallback;
|
||||
|
||||
lua_pushvalue(L, startidx);
|
||||
info.ref = luax_refif(L, LUA_TFUNCTION);
|
||||
lua_pop(L, 1);
|
||||
|
||||
luax_catchexcept(L,
|
||||
[&]() { instance()->endPass(x, y, w, h, &info, L); },
|
||||
[&](bool except) { if (except) delete info.ref; }
|
||||
);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int w_endPass(lua_State *L)
|
||||
{
|
||||
return w__endPass(L, 1);
|
||||
}
|
||||
|
||||
int w_renderPass(lua_State *L)
|
||||
{
|
||||
int startidx = w__beginPass(L);
|
||||
|
||||
if (lua_type(L, startidx) != LUA_TFUNCTION)
|
||||
{
|
||||
w__endPass(L, startidx + 1);
|
||||
luaL_checktype(L, startidx, LUA_TFUNCTION);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int nargs = lua_gettop(L) - startidx;
|
||||
int status = lua_pcall(L, nargs, 0, 0);
|
||||
|
||||
w__endPass(L, startidx + 1);
|
||||
|
||||
if (status != 0)
|
||||
return lua_error(L);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int w_isPassActive(lua_State *L)
|
||||
{
|
||||
luax_pushboolean(L, instance()->isPassActive());
|
||||
return 1;
|
||||
}
|
||||
|
||||
int w_getPassCanvases(lua_State *L)
|
||||
{
|
||||
if (!instance()->isPassActive())
|
||||
return 0;
|
||||
|
||||
const PassInfo &info = instance()->getActivePass();
|
||||
|
||||
for (const auto &attachment : info.colorAttachments)
|
||||
luax_pushtype(L, GRAPHICS_CANVAS_ID, attachment.canvas);
|
||||
|
||||
return info.colorAttachmentCount;
|
||||
}
|
||||
|
||||
int w_captureScreenshot(lua_State *L)
|
||||
{
|
||||
luaL_checktype(L, 1, LUA_TFUNCTION);
|
||||
|
||||
Graphics::ScreenshotInfo info;
|
||||
info.callback = screenshotCallback;
|
||||
|
||||
lua_pushvalue(L, 1);
|
||||
info.ref = luax_refif(L, LUA_TFUNCTION);
|
||||
lua_pop(L, 1);
|
||||
|
||||
luax_catchexcept(L,
|
||||
[&]() { instance()->captureScreenshot(info); },
|
||||
[&](bool except) { if (except) delete info.ref; }
|
||||
);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int w_setScissor(lua_State *L)
|
||||
{
|
||||
int nargs = lua_gettop(L);
|
||||
@@ -243,13 +442,13 @@ int w_stencil(lua_State *L)
|
||||
if (lua_toboolean(L, 4) == 0)
|
||||
instance()->clearStencil();
|
||||
|
||||
instance()->drawToStencilBuffer(action, stencilvalue);
|
||||
luax_catchexcept(L, [&](){ instance()->drawToStencilBuffer(action, stencilvalue); });
|
||||
|
||||
// Call stencilfunc()
|
||||
lua_pushvalue(L, 1);
|
||||
lua_call(L, 0, 0);
|
||||
|
||||
instance()->stopDrawToStencilBuffer();
|
||||
luax_catchexcept(L, [&](){ instance()->stopDrawToStencilBuffer(); });
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -268,7 +467,7 @@ int w_setStencilTest(lua_State *L)
|
||||
comparevalue = (int) luaL_checknumber(L, 2);
|
||||
}
|
||||
|
||||
instance()->setStencilTest(compare, comparevalue);
|
||||
luax_catchexcept(L, [&](){ instance()->setStencilTest(compare, comparevalue); });
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -1246,79 +1445,6 @@ int w_isWireframe(lua_State *L)
|
||||
return 1;
|
||||
}
|
||||
|
||||
int w_newScreenshot(lua_State *L)
|
||||
{
|
||||
love::image::Image *image = luax_getmodule<love::image::Image>(L, MODULE_IMAGE_ID);
|
||||
bool copyAlpha = luax_optboolean(L, 1, false);
|
||||
love::image::ImageData *i = 0;
|
||||
|
||||
luax_catchexcept(L, [&](){ i = instance()->newScreenshot(image, copyAlpha); });
|
||||
|
||||
luax_pushtype(L, IMAGE_IMAGE_DATA_ID, i);
|
||||
i->release();
|
||||
return 1;
|
||||
}
|
||||
|
||||
int w_setCanvas(lua_State *L)
|
||||
{
|
||||
// Disable stencil writes.
|
||||
instance()->stopDrawToStencilBuffer();
|
||||
|
||||
// called with none -> reset to default buffer
|
||||
if (lua_isnoneornil(L, 1))
|
||||
{
|
||||
instance()->setCanvas();
|
||||
return 0;
|
||||
}
|
||||
|
||||
bool is_table = lua_istable(L, 1);
|
||||
std::vector<Canvas *> canvases;
|
||||
|
||||
if (is_table)
|
||||
{
|
||||
for (int i = 1; i <= (int) luax_objlen(L, 1); i++)
|
||||
{
|
||||
lua_rawgeti(L, 1, i);
|
||||
canvases.push_back(luax_checkcanvas(L, -1));
|
||||
lua_pop(L, 1);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
for (int i = 1; i <= lua_gettop(L); i++)
|
||||
canvases.push_back(luax_checkcanvas(L, i));
|
||||
}
|
||||
|
||||
luax_catchexcept(L, [&]() {
|
||||
if (canvases.size() > 0)
|
||||
instance()->setCanvas(canvases);
|
||||
else
|
||||
instance()->setCanvas();
|
||||
});
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int w_getCanvas(lua_State *L)
|
||||
{
|
||||
const std::vector<Canvas *> canvases = instance()->getCanvas();
|
||||
int n = 0;
|
||||
|
||||
for (Canvas *c : canvases)
|
||||
{
|
||||
luax_pushtype(L, GRAPHICS_CANVAS_ID, c);
|
||||
n++;
|
||||
}
|
||||
|
||||
if (n == 0)
|
||||
{
|
||||
lua_pushnil(L);
|
||||
n = 1;
|
||||
}
|
||||
|
||||
return n;
|
||||
}
|
||||
|
||||
int w_setShader(lua_State *L)
|
||||
{
|
||||
if (lua_isnoneornil(L,1))
|
||||
@@ -1497,8 +1623,8 @@ int w_getStats(lua_State *L)
|
||||
lua_pushinteger(L, stats.drawCalls);
|
||||
lua_setfield(L, -2, "drawcalls");
|
||||
|
||||
lua_pushinteger(L, stats.canvasSwitches);
|
||||
lua_setfield(L, -2, "canvasswitches");
|
||||
lua_pushinteger(L, stats.renderPasses);
|
||||
lua_setfield(L, -2, "renderpasses");
|
||||
|
||||
lua_pushinteger(L, stats.shaderSwitches);
|
||||
lua_setfield(L, -2, "shaderswitches");
|
||||
@@ -1555,9 +1681,9 @@ int w_draw(lua_State *L)
|
||||
|
||||
luax_catchexcept(L, [&]() {
|
||||
if (texture && quad)
|
||||
texture->drawq(quad, m);
|
||||
instance()->drawq(texture, quad, m);
|
||||
else if (drawable)
|
||||
drawable->draw(m);
|
||||
instance()->draw(drawable, m);
|
||||
});
|
||||
|
||||
return 0;
|
||||
@@ -1698,11 +1824,14 @@ int w_points(lua_State *L)
|
||||
coords[i] = luax_tofloat(L, i + 1);
|
||||
}
|
||||
|
||||
instance()->points(coords, colors, numpoints);
|
||||
|
||||
delete[] coords;
|
||||
if (colors)
|
||||
delete[] colors;
|
||||
luax_catchexcept(L,
|
||||
[&](){ instance()->points(coords, colors, numpoints); },
|
||||
[&](bool) {
|
||||
delete[] coords;
|
||||
if (colors)
|
||||
delete[] colors;
|
||||
}
|
||||
);
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -1738,9 +1867,11 @@ int w_line(lua_State *L)
|
||||
coords[i] = luax_tofloat(L, i + 1);
|
||||
}
|
||||
|
||||
instance()->polyline(coords, args);
|
||||
luax_catchexcept(L,
|
||||
[&](){ instance()->polyline(coords, args); },
|
||||
[&](bool) { delete[] coords; }
|
||||
);
|
||||
|
||||
delete[] coords;
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -1766,11 +1897,11 @@ int w_rectangle(lua_State *L)
|
||||
float ry = (float)luaL_optnumber(L, 7, rx);
|
||||
|
||||
if (lua_isnoneornil(L, 8))
|
||||
instance()->rectangle(mode, x, y, w, h, rx, ry);
|
||||
luax_catchexcept(L, [&](){ instance()->rectangle(mode, x, y, w, h, rx, ry); });
|
||||
else
|
||||
{
|
||||
int points = (int) luaL_checknumber(L, 8);
|
||||
instance()->rectangle(mode, x, y, w, h, rx, ry, points);
|
||||
luax_catchexcept(L, [&](){ instance()->rectangle(mode, x, y, w, h, rx, ry, points); });
|
||||
}
|
||||
|
||||
return 0;
|
||||
@@ -1788,11 +1919,11 @@ int w_circle(lua_State *L)
|
||||
float radius = (float)luaL_checknumber(L, 4);
|
||||
|
||||
if (lua_isnoneornil(L, 5))
|
||||
instance()->circle(mode, x, y, radius);
|
||||
luax_catchexcept(L, [&](){ instance()->circle(mode, x, y, radius); });
|
||||
else
|
||||
{
|
||||
int points = (int) luaL_checknumber(L, 5);
|
||||
instance()->circle(mode, x, y, radius, points);
|
||||
luax_catchexcept(L, [&](){ instance()->circle(mode, x, y, radius, points); });
|
||||
}
|
||||
|
||||
return 0;
|
||||
@@ -1811,11 +1942,11 @@ int w_ellipse(lua_State *L)
|
||||
float b = (float)luaL_optnumber(L, 5, a);
|
||||
|
||||
if (lua_isnoneornil(L, 6))
|
||||
instance()->ellipse(mode, x, y, a, b);
|
||||
luax_catchexcept(L, [&](){ instance()->ellipse(mode, x, y, a, b); });
|
||||
else
|
||||
{
|
||||
int points = (int) luaL_checknumber(L, 6);
|
||||
instance()->ellipse(mode, x, y, a, b, points);
|
||||
luax_catchexcept(L, [&](){ instance()->ellipse(mode, x, y, a, b, points); });
|
||||
}
|
||||
|
||||
return 0;
|
||||
@@ -1848,11 +1979,11 @@ int w_arc(lua_State *L)
|
||||
float angle2 = (float) luaL_checknumber(L, startidx + 4);
|
||||
|
||||
if (lua_isnoneornil(L, startidx + 5))
|
||||
instance()->arc(drawmode, arcmode, x, y, radius, angle1, angle2);
|
||||
luax_catchexcept(L, [&](){ instance()->arc(drawmode, arcmode, x, y, radius, angle1, angle2); });
|
||||
else
|
||||
{
|
||||
int points = (int) luaL_checknumber(L, startidx + 5);
|
||||
instance()->arc(drawmode, arcmode, x, y, radius, angle1, angle2, points);
|
||||
luax_catchexcept(L, [&](){ instance()->arc(drawmode, arcmode, x, y, radius, angle1, angle2, points); });
|
||||
}
|
||||
|
||||
return 0;
|
||||
@@ -1900,8 +2031,11 @@ int w_polygon(lua_State *L)
|
||||
// make a closed loop
|
||||
coords[args] = coords[0];
|
||||
coords[args+1] = coords[1];
|
||||
instance()->polygon(mode, coords, args+2);
|
||||
delete[] coords;
|
||||
|
||||
luax_catchexcept(L,
|
||||
[&](){ instance()->polygon(mode, coords, args+2); },
|
||||
[&](bool) { delete[] coords; }
|
||||
);
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -1987,8 +2121,6 @@ int w_inverseTransformPoint(lua_State *L)
|
||||
static const luaL_Reg functions[] =
|
||||
{
|
||||
{ "reset", w_reset },
|
||||
{ "clear", w_clear },
|
||||
{ "discard", w_discard },
|
||||
{ "present", w_present },
|
||||
|
||||
{ "newImage", w_newImage },
|
||||
@@ -2030,9 +2162,6 @@ static const luaL_Reg functions[] =
|
||||
{ "getPointSize", w_getPointSize },
|
||||
{ "setWireframe", w_setWireframe },
|
||||
{ "isWireframe", w_isWireframe },
|
||||
{ "newScreenshot", w_newScreenshot },
|
||||
{ "setCanvas", w_setCanvas },
|
||||
{ "getCanvas", w_getCanvas },
|
||||
|
||||
{ "setShader", w_setShader },
|
||||
{ "getShader", w_getShader },
|
||||
@@ -2046,6 +2175,14 @@ static const luaL_Reg functions[] =
|
||||
{ "getSystemLimits", w_getSystemLimits },
|
||||
{ "getStats", w_getStats },
|
||||
|
||||
{ "beginPass", w_beginPass },
|
||||
{ "endPass", w_endPass },
|
||||
{ "renderPass", w_renderPass },
|
||||
{ "isPassActive", w_isPassActive },
|
||||
{ "getPassCanvases", w_getPassCanvases },
|
||||
|
||||
{ "captureScreenshot", w_captureScreenshot },
|
||||
|
||||
{ "draw", w_draw },
|
||||
|
||||
{ "print", w_print },
|
||||
@@ -2057,6 +2194,9 @@ static const luaL_Reg functions[] =
|
||||
{ "getWidth", w_getWidth },
|
||||
{ "getHeight", w_getHeight },
|
||||
{ "getDimensions", w_getDimensions },
|
||||
{ "getPassWidth", w_getPassWidth },
|
||||
{ "getPassHeight", w_getPassHeight },
|
||||
{ "getPassDimensions", w_getPassDimensions },
|
||||
|
||||
{ "setScissor", w_setScissor },
|
||||
{ "intersectScissor", w_intersectScissor },
|
||||
|
||||
Reference in New Issue
Block a user