@@ -848,18 +848,9 @@ struct Renderer::Impl {
848848 }
849849
850850 if (state.mRenderTargetTexture != mLastCommittedState .mRenderTargetTexture ) {
851- base::Extents framebufferSize;
852- if (state.mRenderTargetTexture != 0 ) {
853- const auto iData = mRenderTargetDict .find (state.mRenderTargetTexture );
854- assert (iData != mRenderTargetDict .end ());
855-
856- glBindFramebuffer (GL_FRAMEBUFFER , iData->second .mFbo );
857- framebufferSize = iData->second .mSize ;
858- } else {
859- glBindFramebuffer (GL_FRAMEBUFFER , 0 );
860- framebufferSize = mWindowSize ;
861- }
851+ const auto framebufferSize = currentFramebufferSize ();
862852
853+ commitRenderTarget (state);
863854 glViewport (0 , 0 , framebufferSize.width , framebufferSize.height );
864855 commitClipRect (state, framebufferSize);
865856 commitVertexAttributeFormat ();
@@ -903,6 +894,17 @@ struct Renderer::Impl {
903894 }
904895
905896
897+ void commitRenderTarget (const State& state) {
898+ if (state.mRenderTargetTexture != 0 ) {
899+ const auto iData = mRenderTargetDict .find (state.mRenderTargetTexture );
900+ assert (iData != mRenderTargetDict .end ());
901+ glBindFramebuffer (GL_FRAMEBUFFER , iData->second .mFbo );
902+ } else {
903+ glBindFramebuffer (GL_FRAMEBUFFER , 0 );
904+ }
905+ }
906+
907+
906908 void commitClipRect (
907909 const State& state,
908910 const base::Extents& framebufferSize
@@ -1016,6 +1018,8 @@ struct Renderer::Impl {
10161018 const int width,
10171019 const int height
10181020 ) {
1021+ submitBatch ();
1022+
10191023 const auto textureHandle =
10201024 createGlTexture (GLsizei (width), GLsizei (height), nullptr );
10211025
@@ -1029,8 +1033,8 @@ struct Renderer::Impl {
10291033 textureHandle,
10301034 0 );
10311035
1032- mStateChanged = true ;
10331036 glBindTexture (GL_TEXTURE_2D , mLastUsedTexture );
1037+ commitRenderTarget (mLastCommittedState );
10341038
10351039 mRenderTargetDict .insert ({textureHandle, {{width, height}, fboHandle}});
10361040
@@ -1039,6 +1043,8 @@ struct Renderer::Impl {
10391043
10401044
10411045 TextureId createTexture (const data::Image& image) {
1046+ submitBatch ();
1047+
10421048 // OpenGL wants pixel data in bottom-up format, so transform it accordingly
10431049 std::vector<std::uint8_t > pixelData;
10441050 pixelData.resize (image.width () * image.height () * 4 );
@@ -1068,6 +1074,8 @@ struct Renderer::Impl {
10681074
10691075
10701076 void destroyTexture (TextureId texture) {
1077+ submitBatch ();
1078+
10711079 const auto iRenderTarget = mRenderTargetDict .find (texture);
10721080 if (iRenderTarget != mRenderTargetDict .end ()) {
10731081 glDeleteFramebuffers (1 , &iRenderTarget->second .mFbo );
0 commit comments