From e099e570521844b8a82e95de6eada72e9113bd2b Mon Sep 17 00:00:00 2001 From: Alex Szpakowski Date: Sun, 3 Dec 2017 23:04:40 -0400 Subject: [PATCH] Cleaned up some internal love.graphics code. --HG-- branch : minor --- src/modules/graphics/Canvas.cpp | 34 ++++++++++++------------ src/modules/graphics/Graphics.cpp | 4 ++- src/modules/graphics/Graphics.h | 3 ++- src/modules/graphics/opengl/Graphics.cpp | 21 +++++++++++---- src/modules/graphics/opengl/Graphics.h | 2 +- src/modules/graphics/opengl/OpenGL.cpp | 20 -------------- src/modules/graphics/opengl/OpenGL.h | 7 +---- 7 files changed, 40 insertions(+), 51 deletions(-) diff --git a/src/modules/graphics/Canvas.cpp b/src/modules/graphics/Canvas.cpp index b286cdacc..5dfc98a09 100644 --- a/src/modules/graphics/Canvas.cpp +++ b/src/modules/graphics/Canvas.cpp @@ -142,23 +142,23 @@ love::image::ImageData *Canvas::newImageData(love::image::Image *module, int sli PixelFormat dataformat; switch (getPixelFormat()) { - case PIXELFORMAT_RGB10A2: // FIXME: Conversions aren't supported in GLES - dataformat = PIXELFORMAT_RGBA16; - break; - case PIXELFORMAT_R16F: - case PIXELFORMAT_RG16F: - case PIXELFORMAT_RGBA16F: - case PIXELFORMAT_RG11B10F: // FIXME: Conversions aren't supported in GLES - dataformat = PIXELFORMAT_RGBA16F; - break; - case PIXELFORMAT_R32F: - case PIXELFORMAT_RG32F: - case PIXELFORMAT_RGBA32F: - dataformat = PIXELFORMAT_RGBA32F; - break; - default: - dataformat = PIXELFORMAT_RGBA8; - break; + case PIXELFORMAT_RGB10A2: // FIXME: Conversions aren't supported in GLES + dataformat = PIXELFORMAT_RGBA16; + break; + case PIXELFORMAT_R16F: + case PIXELFORMAT_RG16F: + case PIXELFORMAT_RGBA16F: + case PIXELFORMAT_RG11B10F: // FIXME: Conversions aren't supported in GLES + dataformat = PIXELFORMAT_RGBA16F; + break; + case PIXELFORMAT_R32F: + case PIXELFORMAT_RG32F: + case PIXELFORMAT_RGBA32F: + dataformat = PIXELFORMAT_RGBA32F; + break; + default: + dataformat = PIXELFORMAT_RGBA8; + break; } return module->newImageData(r.w, r.h, dataformat); diff --git a/src/modules/graphics/Graphics.cpp b/src/modules/graphics/Graphics.cpp index d8002f4f2..435ad30cb 100644 --- a/src/modules/graphics/Graphics.cpp +++ b/src/modules/graphics/Graphics.cpp @@ -117,6 +117,7 @@ Graphics::Graphics() , streamBufferState() , projectionMatrix() , canvasSwitchCount(0) + , drawCalls(0) , drawCallsBatched(0) , capabilities() , cachedShaderStages() @@ -1459,8 +1460,9 @@ Graphics::Stats Graphics::getStats() const { Stats stats; - getAPIStats(stats.drawCalls, stats.shaderSwitches); + getAPIStats(stats.shaderSwitches); + stats.drawCalls = drawCalls; if (streamBufferState.vertexCount > 0) stats.drawCalls++; diff --git a/src/modules/graphics/Graphics.h b/src/modules/graphics/Graphics.h index 2219579aa..6185b14c1 100644 --- a/src/modules/graphics/Graphics.h +++ b/src/modules/graphics/Graphics.h @@ -908,7 +908,7 @@ protected: virtual void setCanvasInternal(const RenderTargets &rts, int w, int h, int pixelw, int pixelh, bool hasSRGBcanvas) = 0; virtual void initCapabilities() = 0; - virtual void getAPIStats(int &drawcalls, int &shaderswitches) const = 0; + virtual void getAPIStats(int &shaderswitches) const = 0; Canvas *getTemporaryCanvas(PixelFormat format, int w, int h, int samples); @@ -946,6 +946,7 @@ protected: std::vector temporaryCanvases; int canvasSwitchCount; + int drawCalls; int drawCallsBatched; Capabilities capabilities; diff --git a/src/modules/graphics/opengl/Graphics.cpp b/src/modules/graphics/opengl/Graphics.cpp index bdec13537..7939690be 100644 --- a/src/modules/graphics/opengl/Graphics.cpp +++ b/src/modules/graphics/opengl/Graphics.cpp @@ -323,7 +323,13 @@ void Graphics::draw(PrimitiveType primtype, int vertexstart, int vertexcount, in gl.bindTextureToUnit(texture, 0, false); GLenum glprimitivetype = OpenGL::getGLPrimitiveType(primtype); - gl.drawArrays(glprimitivetype, vertexstart, vertexcount, instancecount); + + if (instancecount > 1) + glDrawArraysInstanced(glprimitivetype, vertexstart, vertexcount, instancecount); + else + glDrawArrays(glprimitivetype, vertexstart, vertexcount); + + ++drawCalls; } void Graphics::drawIndexed(PrimitiveType primtype, int indexcount, int instancecount, IndexDataType datatype, Resource *indexbuffer, size_t indexoffset, const vertex::Attributes &attribs, const vertex::Buffers &buffers, love::graphics::Texture *texture) @@ -337,7 +343,13 @@ void Graphics::drawIndexed(PrimitiveType primtype, int indexcount, int instancec GLenum gldatatype = OpenGL::getGLIndexDataType(datatype); gl.bindBuffer(BUFFER_INDEX, indexbuffer->getHandle()); - gl.drawElements(glprimitivetype, indexcount, gldatatype, gloffset, instancecount); + + if (instancecount > 1) + glDrawElementsInstanced(glprimitivetype, indexcount, gldatatype, gloffset, instancecount); + else + glDrawElements(glprimitivetype, indexcount, gldatatype, gloffset); + + ++drawCalls; } static void APIENTRY debugCB(GLenum source, GLenum type, GLuint id, GLenum severity, GLsizei /*len*/, const GLchar *msg, const GLvoid* /*usr*/) @@ -949,7 +961,7 @@ void Graphics::present(void *screenshotCallbackData) window->swapBuffers(); // Reset the per-frame stat counts. - gl.stats.drawCalls = 0; + drawCalls = 0; gl.stats.shaderSwitches = 0; canvasSwitchCount = 0; drawCallsBatched = 0; @@ -1260,9 +1272,8 @@ Graphics::RendererInfo Graphics::getRendererInfo() const return info; } -void Graphics::getAPIStats(int &drawcalls, int &shaderswitches) const +void Graphics::getAPIStats(int &shaderswitches) const { - drawcalls = gl.stats.drawCalls; shaderswitches = gl.stats.shaderSwitches; } diff --git a/src/modules/graphics/opengl/Graphics.h b/src/modules/graphics/opengl/Graphics.h index cc9cbe1b4..bd5c4f75d 100644 --- a/src/modules/graphics/opengl/Graphics.h +++ b/src/modules/graphics/opengl/Graphics.h @@ -115,7 +115,7 @@ private: love::graphics::StreamBuffer *newStreamBuffer(BufferType type, size_t size) override; void setCanvasInternal(const RenderTargets &rts, int w, int h, int pixelw, int pixelh, bool hasSRGBcanvas) override; void initCapabilities() override; - void getAPIStats(int &drawcalls, int &shaderswitches) const override; + void getAPIStats(int &shaderswitches) const override; void endPass(); void bindCachedFBO(const RenderTargets &targets); diff --git a/src/modules/graphics/opengl/OpenGL.cpp b/src/modules/graphics/opengl/OpenGL.cpp index 7b774b241..322ac57b4 100644 --- a/src/modules/graphics/opengl/OpenGL.cpp +++ b/src/modules/graphics/opengl/OpenGL.cpp @@ -627,26 +627,6 @@ void OpenGL::deleteBuffer(GLuint buffer) } } -void OpenGL::drawArrays(GLenum mode, GLint first, GLsizei count, GLsizei instancecount) -{ - if (instancecount > 1) - glDrawArraysInstanced(mode, first, count, instancecount); - else - glDrawArrays(mode, first, count); - - ++stats.drawCalls; -} - -void OpenGL::drawElements(GLenum mode, GLsizei count, GLenum type, const void *indices, GLsizei instancecount) -{ - if (instancecount > 1) - glDrawElementsInstanced(mode, count, type, indices, instancecount); - else - glDrawElements(mode, count, type, indices); - - ++stats.drawCalls; -} - void OpenGL::setVertexAttributes(const vertex::Attributes &attributes, const vertex::Buffers &buffers) { uint32 enablediff = attributes.enablebits ^ state.enabledAttribArrays; diff --git a/src/modules/graphics/opengl/OpenGL.h b/src/modules/graphics/opengl/OpenGL.h index 8c30272e8..be671af19 100644 --- a/src/modules/graphics/opengl/OpenGL.h +++ b/src/modules/graphics/opengl/OpenGL.h @@ -115,7 +115,6 @@ public: struct Stats { - int drawCalls; int shaderSwitches; } stats; @@ -201,12 +200,8 @@ public: void deleteBuffer(GLuint buffer); /** - * glDrawArrays and glDrawElements which increment the draw-call counter by - * themselves. + * Set all vertex attribute state. **/ - void drawArrays(GLenum mode, GLint first, GLsizei count, GLsizei instancecount = 1); - void drawElements(GLenum mode, GLsizei count, GLenum type, const void *indices, GLsizei instancecount = 1); - void setVertexAttributes(const vertex::Attributes &attributes, const vertex::Buffers &buffers); /**