diff --git a/src/modules/graphics/Graphics.cpp b/src/modules/graphics/Graphics.cpp index f6e2ceff6..964909cda 100644 --- a/src/modules/graphics/Graphics.cpp +++ b/src/modules/graphics/Graphics.cpp @@ -388,19 +388,13 @@ void Graphics::setShader(love::graphics::Shader *shader) if (shader == nullptr) return setShader(); - flushStreamDraws(); - shader->attach(); - states.back().shader.set(shader); } void Graphics::setShader() { - flushStreamDraws(); - Shader::attachDefault(Shader::STANDARD_DEFAULT); - states.back().shader.set(nullptr); } @@ -613,16 +607,14 @@ Graphics::StreamVertexData Graphics::requestStreamDraw(const StreamDrawRequest & StreamBufferState &state = streamBufferState; - if (state.vertexCount == 0 && Shader::current != nullptr && req.texture != nullptr) - Shader::current->checkMainTexture(req.texture); - bool shouldflush = false; bool shouldresize = false; if (req.primitiveMode != state.primitiveMode || req.formats[0] != state.formats[0] || req.formats[1] != state.formats[1] || ((req.indexMode != TriangleIndexMode::NONE) != (state.indexCount > 0)) - || req.texture != state.texture) + || req.texture != state.texture + || req.standardShaderType != state.standardShaderType) { shouldflush = true; } @@ -681,8 +673,15 @@ Graphics::StreamVertexData Graphics::requestStreamDraw(const StreamDrawRequest & state.formats[0] = req.formats[0]; state.formats[1] = req.formats[1]; state.texture = req.texture; + state.standardShaderType = req.standardShaderType; } + if (state.vertexCount == 0 && Shader::isDefaultActive()) + Shader::attachDefault(state.standardShaderType); + + if (state.vertexCount == 0 && Shader::current != nullptr && req.texture != nullptr) + Shader::current->checkMainTexture(req.texture); + if (shouldresize) { for (int i = 0; i < 2; i++) diff --git a/src/modules/graphics/Graphics.h b/src/modules/graphics/Graphics.h index 195bb7dfc..a21977961 100644 --- a/src/modules/graphics/Graphics.h +++ b/src/modules/graphics/Graphics.h @@ -242,6 +242,7 @@ public: vertex::TriangleIndexMode indexMode = vertex::TriangleIndexMode::NONE; int vertexCount = 0; Texture *texture = nullptr; + Shader::StandardShader standardShaderType = Shader::STANDARD_DEFAULT; StreamDrawRequest() { @@ -840,6 +841,7 @@ protected: vertex::PrimitiveMode primitiveMode = vertex::PrimitiveMode::TRIANGLES; vertex::CommonFormat formats[2]; StrongRef texture; + Shader::StandardShader standardShaderType = Shader::STANDARD_DEFAULT; int vertexCount = 0; int indexCount = 0; diff --git a/src/modules/graphics/Texture.cpp b/src/modules/graphics/Texture.cpp index a98672f2d..e8655ffaa 100644 --- a/src/modules/graphics/Texture.cpp +++ b/src/modules/graphics/Texture.cpp @@ -183,6 +183,7 @@ void Texture::drawLayer(Graphics *gfx, int layer, Quad *q, const Matrix4 &m) req.indexMode = TriangleIndexMode::QUADS; req.vertexCount = 4; req.texture = this; + req.standardShaderType = Shader::STANDARD_ARRAY; Graphics::StreamVertexData data = gfx->requestStreamDraw(req); diff --git a/src/modules/graphics/Video.cpp b/src/modules/graphics/Video.cpp index bd9d67584..00c872ee1 100644 --- a/src/modules/graphics/Video.cpp +++ b/src/modules/graphics/Video.cpp @@ -107,21 +107,6 @@ void Video::draw(Graphics *gfx, const Matrix4 &m) { update(); - gfx->flushStreamDraws(); - - bool usingdefaultshader = Shader::isDefaultActive(); - Shader *prevdefaultshader = nullptr; - - // If we're using the default shader, substitute the video version. - if (usingdefaultshader) - { - prevdefaultshader = Shader::current; - Shader::standardShaders[Shader::STANDARD_VIDEO]->attach(); - } - - if (Shader::current != nullptr) - Shader::current->setVideoTextures(images[0], images[1], images[2]); - const Matrix4 &tm = gfx->getTransform(); bool is2D = tm.isAffine2DTransform(); @@ -132,6 +117,7 @@ void Video::draw(Graphics *gfx, const Matrix4 &m) req.formats[1] = vertex::CommonFormat::STf_RGBAub; req.indexMode = vertex::TriangleIndexMode::QUADS; req.vertexCount = 4; + req.standardShaderType = Shader::STANDARD_VIDEO; Graphics::StreamVertexData data = gfx->requestStreamDraw(req); @@ -151,10 +137,10 @@ void Video::draw(Graphics *gfx, const Matrix4 &m) verts[i].color = c; } - gfx->flushStreamDraws(); + if (Shader::current != nullptr) + Shader::current->setVideoTextures(images[0], images[1], images[2]); - if (usingdefaultshader && prevdefaultshader != nullptr) - prevdefaultshader->attach(); + gfx->flushStreamDraws(); } void Video::update() diff --git a/src/modules/graphics/opengl/Graphics.cpp b/src/modules/graphics/opengl/Graphics.cpp index c7984b619..884df355d 100644 --- a/src/modules/graphics/opengl/Graphics.cpp +++ b/src/modules/graphics/opengl/Graphics.cpp @@ -339,22 +339,6 @@ void Graphics::flushStreamDraws() if (sbstate.vertexCount == 0 && sbstate.indexCount == 0) return; - love::graphics::Shader *prevdefaultshader = nullptr; - - if (sbstate.texture.get()) - { - TextureType textype = sbstate.texture->getTextureType(); - - if (textype == TEXTURE_2D_ARRAY && Shader::isDefaultActive()) - { - if (!Shader::standardShaders[Shader::STANDARD_ARRAY]) - throw love::Exception("Standard array texture shader has not been initialized!"); - - prevdefaultshader = Shader::current; - Shader::standardShaders[Shader::STANDARD_ARRAY]->attach(); - } - } - OpenGL::TempDebugGroup debuggroup("Stream vertices flush and draw"); uint32 attribs = 0; @@ -434,9 +418,6 @@ void Graphics::flushStreamDraws() streamBufferState.vertexCount = 0; streamBufferState.indexCount = 0; - - if (prevdefaultshader != nullptr) - prevdefaultshader->attach(); } static void APIENTRY debugCB(GLenum source, GLenum type, GLuint id, GLenum severity, GLsizei /*len*/, const GLchar *msg, const GLvoid* /*usr*/) diff --git a/src/modules/graphics/opengl/Mesh.cpp b/src/modules/graphics/opengl/Mesh.cpp index d9c65bf6a..f84a779b8 100644 --- a/src/modules/graphics/opengl/Mesh.cpp +++ b/src/modules/graphics/opengl/Mesh.cpp @@ -99,11 +99,14 @@ void Mesh::drawInstanced(love::graphics::Graphics *gfx, const love::Matrix4 &m, if (instancecount > 1 && !gl.isInstancingSupported()) throw love::Exception("Instancing is not supported on this system."); + gfx->flushStreamDraws(); + + if (Shader::isDefaultActive()) + Shader::attachDefault(Shader::STANDARD_DEFAULT); + if (Shader::current && texture.get()) Shader::current->checkMainTexture(texture); - gfx->flushStreamDraws(); - OpenGL::TempDebugGroup debuggroup("Mesh draw"); uint32 enabledattribs = 0; diff --git a/src/modules/graphics/opengl/ParticleSystem.cpp b/src/modules/graphics/opengl/ParticleSystem.cpp index 6f402b6f2..36a465ae7 100644 --- a/src/modules/graphics/opengl/ParticleSystem.cpp +++ b/src/modules/graphics/opengl/ParticleSystem.cpp @@ -59,6 +59,9 @@ void ParticleSystem::draw(Graphics *gfx, const Matrix4 &m) Graphics::TempTransform transform(gfx, m); + if (Shader::isDefaultActive()) + Shader::attachDefault(Shader::STANDARD_DEFAULT); + if (Shader::current && texture.get()) Shader::current->checkMainTexture(texture); diff --git a/src/modules/graphics/opengl/Shader.cpp b/src/modules/graphics/opengl/Shader.cpp index 3c946d91d..35636c5cd 100644 --- a/src/modules/graphics/opengl/Shader.cpp +++ b/src/modules/graphics/opengl/Shader.cpp @@ -522,6 +522,8 @@ void Shader::attach() { if (current != this) { + Graphics::flushStreamDrawsGlobal(); + gl.useProgram(program); current = this; // retain/release happens in Graphics::setShader. @@ -777,7 +779,7 @@ void Shader::setVideoTextures(Texture *ytexture, Texture *cbtexture, Texture *cr const UniformInfo *info = builtinUniformInfo[builtins[i]]; if (info != nullptr) - sendTextures(info, &textures[i], 1); + sendTextures(info, &textures[i], 1, true); } } diff --git a/src/modules/graphics/opengl/SpriteBatch.cpp b/src/modules/graphics/opengl/SpriteBatch.cpp index 69af74003..2e5eaa10d 100644 --- a/src/modules/graphics/opengl/SpriteBatch.cpp +++ b/src/modules/graphics/opengl/SpriteBatch.cpp @@ -60,19 +60,15 @@ void SpriteBatch::draw(Graphics *gfx, const Matrix4 &m) gfx->flushStreamDraws(); - Shader *prevdefaultshader = nullptr; - if (texture.get()) { - TextureType textype = texture->getTextureType(); - - if (textype == TEXTURE_2D_ARRAY && Shader::isDefaultActive()) + if (Shader::isDefaultActive()) { - if (!Shader::standardShaders[Shader::STANDARD_ARRAY]) - throw love::Exception("Standard array texture shader has not been initialized!"); + Shader::StandardShader defaultshader = Shader::STANDARD_DEFAULT; + if (texture->getTextureType() == TEXTURE_2D_ARRAY) + defaultshader = Shader::STANDARD_ARRAY; - prevdefaultshader = Shader::current; - Shader::standardShaders[Shader::STANDARD_ARRAY]->attach(); + Shader::attachDefault(defaultshader); } if (Shader::current) @@ -138,9 +134,6 @@ void SpriteBatch::draw(Graphics *gfx, const Matrix4 &m) gl.drawElements(GL_TRIANGLES, (GLsizei) quad_indices.getIndexCount(count), gltype, indices); } - - if (prevdefaultshader != nullptr) - prevdefaultshader->attach(); } } // opengl diff --git a/src/modules/graphics/opengl/Text.cpp b/src/modules/graphics/opengl/Text.cpp index 17a7e07bd..f91f8f368 100644 --- a/src/modules/graphics/opengl/Text.cpp +++ b/src/modules/graphics/opengl/Text.cpp @@ -45,11 +45,14 @@ void Text::draw(Graphics *gfx, const Matrix4 &m) if (vbo == nullptr || draw_commands.empty()) return; + gfx->flushStreamDraws(); + + if (Shader::isDefaultActive()) + Shader::attachDefault(Shader::STANDARD_DEFAULT); + if (Shader::current) Shader::current->checkMainTextureType(TEXTURE_2D, false); - gfx->flushStreamDraws(); - OpenGL::TempDebugGroup debuggroup("Text object draw"); // Re-generate the text if the Font's texture cache was invalidated.