2929import static org .lwjgl .opengl .GL42 .GL_FRAMEBUFFER_BARRIER_BIT ;
3030import static org .lwjgl .opengl .GL43 .*;
3131import static org .lwjgl .opengl .GL43 .GL_SHADER_STORAGE_BUFFER ;
32+ import static org .lwjgl .opengl .GL45C .glBindTextureUnit ;
3233import static org .lwjgl .opengl .GL45C .glClearNamedBufferData ;
3334
3435public class Gl46FarWorldRenderer extends AbstractFarWorldRenderer <Gl46Viewport > {
@@ -56,12 +57,9 @@ public Gl46FarWorldRenderer(int geometryBuffer, int maxSections) {
5657 this .glCommandBuffer = new GlBuffer (maxSections *5L *4 * 6 );
5758 this .glCommandCountBuffer = new GlBuffer (4 *2 );
5859 glClearNamedBufferData (this .glCommandBuffer .id , GL_R8UI , GL_RED_INTEGER , GL_UNSIGNED_BYTE , new int [1 ]);
59- setupVao ();
6060 }
6161
62- @ Override
63- protected void setupVao () {
64- glBindVertexArray (this .vao );
62+ protected void bindResources (Gl46Viewport viewport ) {
6563 glBindBuffer (GL_DRAW_INDIRECT_BUFFER , this .glCommandBuffer .id );
6664 glBindBuffer (GL_PARAMETER_BUFFER_ARB , this .glCommandCountBuffer .id );
6765 glBindBuffer (GL_ELEMENT_ARRAY_BUFFER , SharedIndexBuffer .INSTANCE .id ());
@@ -70,10 +68,10 @@ protected void setupVao() {
7068 glBindBufferBase (GL_SHADER_STORAGE_BUFFER , 2 , this .glCommandBuffer .id );
7169 glBindBufferBase (GL_SHADER_STORAGE_BUFFER , 3 , this .glCommandCountBuffer .id );
7270 glBindBufferBase (GL_SHADER_STORAGE_BUFFER , 4 , this .geometry .metaId ());
71+ glBindBufferBase (GL_SHADER_STORAGE_BUFFER , 5 , viewport .visibilityBuffer .id );
7372 glBindBufferBase (GL_SHADER_STORAGE_BUFFER , 6 , this .models .getBufferId ());
7473 glBindBufferBase (GL_SHADER_STORAGE_BUFFER , 7 , this .models .getColourBufferId ());
7574 glBindBufferBase (GL_SHADER_STORAGE_BUFFER , 8 , this .lightDataBuffer .id );//Lighting LUT
76- glBindVertexArray (0 );
7775 }
7876
7977 //FIXME: dont do something like this as it breaks multiviewport mods
@@ -115,30 +113,26 @@ public void renderFarAwayOpaque(Gl46Viewport viewport) {
115113
116114
117115 RenderLayer .getCutoutMipped ().startDrawing ();
118- int oldActiveTexture = glGetInteger (GL_ACTIVE_TEXTURE );
119116 //RenderSystem.enableBlend();
120117 //RenderSystem.defaultBlendFunc();
121118
122119 this .updateUniformBuffer (viewport );
123-
124120 UploadStream .INSTANCE .commit ();
125-
126- glBindVertexArray (this .vao );
127- glBindBufferBase (GL_SHADER_STORAGE_BUFFER , 5 , viewport .visibilityBuffer .id );
121+ glBindVertexArray (AbstractFarWorldRenderer .STATIC_VAO );
128122
129123
130124 //Bind the texture atlas
131- glActiveTexture (GL_TEXTURE0 );
132- int oldBoundTexture = glGetInteger (GL_TEXTURE_BINDING_2D );
133125 glBindSampler (0 , this .models .getSamplerId ());
134- glBindTexture ( GL_TEXTURE_2D , this .models .getTextureId ());
126+ glBindTextureUnit ( 0 , this .models .getTextureId ());
135127
136128 glClearNamedBufferData (this .glCommandCountBuffer .id , GL_R32UI , GL_RED_INTEGER , GL_UNSIGNED_INT , new int [1 ]);
137129 this .commandGen .bind ();
130+ this .bindResources (viewport );
138131 glDispatchCompute ((this .geometry .getSectionCount ()+127 )/128 , 1 , 1 );
139132 glMemoryBarrier (GL_COMMAND_BARRIER_BIT | GL_SHADER_STORAGE_BARRIER_BIT | GL_UNIFORM_BARRIER_BIT );
140133
141134 this .lodShader .bind ();
135+ this .bindResources (viewport );
142136 glDisable (GL_CULL_FACE );
143137 //glPointSize(10);
144138 glMultiDrawElementsIndirectCountARB (GL_TRIANGLES , GL_UNSIGNED_SHORT , 0 , 0 , (int ) (this .geometry .getSectionCount ()*4.4 ), 0 );
@@ -159,6 +153,7 @@ public void renderFarAwayOpaque(Gl46Viewport viewport) {
159153 glMemoryBarrier (GL_PIXEL_BUFFER_BARRIER_BIT | GL_FRAMEBUFFER_BARRIER_BIT );
160154
161155 this .cullShader .bind ();
156+ this .bindResources (viewport );
162157
163158 glColorMask (false , false , false , false );
164159 glDepthMask (false );
@@ -174,15 +169,14 @@ public void renderFarAwayOpaque(Gl46Viewport viewport) {
174169
175170 glBindVertexArray (0 );
176171 glBindSampler (0 , 0 );
177- GL11C .glBindTexture (GL_TEXTURE_2D , oldBoundTexture );
178- glActiveTexture (oldActiveTexture );
172+ glBindTextureUnit (0 , 0 );
179173 RenderLayer .getCutoutMipped ().endDrawing ();
180174 }
181175
182176 @ Override
183- public void renderFarAwayTranslucent () {
177+ public void renderFarAwayTranslucent (Gl46Viewport viewport ) {
184178 RenderLayer .getTranslucent ().startDrawing ();
185- glBindVertexArray (this . vao );
179+ glBindVertexArray (AbstractFarWorldRenderer . STATIC_VAO );
186180 glDisable (GL_CULL_FACE );
187181 glEnable (GL_BLEND );
188182
@@ -192,11 +186,11 @@ public void renderFarAwayTranslucent() {
192186
193187
194188 glBindSampler (0 , this .models .getSamplerId ());
195- glActiveTexture (GL_TEXTURE0 );
196- glBindTexture (GL_TEXTURE_2D , this .models .getTextureId ());
189+ glBindTextureUnit (0 , this .models .getTextureId ());
197190
198191 //RenderSystem.blendFunc(GlStateManager.SrcFactor.ONE, GlStateManager.DstFactor.ONE);
199192 this .lodShader .bind ();
193+ this .bindResources (viewport );
200194
201195 glDepthMask (false );
202196 glMultiDrawElementsIndirectCountARB (GL_TRIANGLES , GL_UNSIGNED_SHORT , 400_000 * 4 * 5 , 4 , this .geometry .getSectionCount (), 0 );
@@ -207,6 +201,7 @@ public void renderFarAwayTranslucent() {
207201
208202
209203 glBindSampler (0 , 0 );
204+ glBindTextureUnit (0 , 0 );
210205 glDisable (GL_BLEND );
211206
212207 RenderLayer .getTranslucent ().endDrawing ();
0 commit comments