title: Rendering an OpenGL quad as smaller sub tiles
body: <p>I am trying to render a full screen quad as sub tiles.  The reason for this is that the GLSL shader I am running can take a while to execute (up to 10 seconds).  While the 10 seconds is calculated the video driver can timeout or (if TDR timeout is disabled) the app will seem to have hung for the 10 seconds.</p>
  
  <p>I thought I could break up the full screen quad into sub tiles.  They each render quicker and don't lock up the app.</p>
  
  <p>The problem I am having is how to call glTexCoord2f and glVertex2f within the tile loop.  Example code from the loop;</p>
  
  <p>xtilesize is the image width divided by the number of tiles (xtiles) I want to render the quad as
  ytilesize is the image height divided by the number of tiles (ytiles) I want to render the quad as</p>
  
  <pre><code>     for yloop:=0 to ytiles-1 do
       begin
            for xloop:=0 to xtiles-1 do
            begin
                 glviewport(xloop*xtilesize,yloop*ytilesize,xloop*xtilesize+xtilesize,yloop*ytilesize+ytilesize);
                 glBegin(GL_QUADS);
  
                 //tex coords - extents are (0,0) to (1,1)
                 tilewidth:=1/xtiles;
                 tileheight:=1/ytiles;
                 x1:=xloop*tilewidth;
                 y1:=yloop*tileheight;
                 x2:=x1+tilewidth;
                 y2:=y1+tileheight;
  
                 //vertex - extents are (-1,-1) to (1,1)
                 //but for testing make them -0.9 to 0.9 - this gives a border around them to see how they are rendered
                 x3:=-0.9;
                 y3:=-0.9;
                 x4:=0.9;
                 y4:=0.9;
  
                 glTexCoord2f(x1,y1); glVertex2f(x3,y3);
                 glTexCoord2f(x2,y1); glVertex2f(x4,y3);
                 glTexCoord2f(x2,y2); glVertex2f(x4,y4);
                 glTexCoord2f(x1,y2); glVertex2f(x3,y4);
  
                 glEnd;
  
  
            end;
  
       end;
  
  
  The GLSL shaders are very simple;
  </code></pre>
  
  <p>The Vertex Shader</p>
  
  <pre><code>void main()
  {
      gl_Position = ftransform();
  }
  </code></pre>
  
  <p>The Fragment Shader</p>
  
  <pre><code>#version 400
  
  uniform vec2 resolution;
  
  void main(void)
  {
      gl_FragColor = vec4(1-gl_FragCoord.x/resolution.x,gl_FragCoord.y/resolution.y,1,0);
  }
  </code></pre>
  
  <p>Here is the problem.  If I set the xtiles and ytiles to 1 then the output is a single quad.  The shaded rectangle is shown within the 0.1 size border, ie</p>
  
  <p><a href="https://ibb.co/2kHPWNb" rel="nofollow noreferrer">https://ibb.co/2kHPWNb</a></p>
  
  <p>If I increase the X and Y tile counts to 2, the sub tiles seem to be slightly larger than what they should, so they go past the edges, ie</p>
  
  <p><a href="https://ibb.co/JrLvCvT" rel="nofollow noreferrer">https://ibb.co/JrLvCvT</a></p>
  
  <p>And for 3 sub tiles</p>
  
  <p><a href="https://ibb.co/MVyvQRQ" rel="nofollow noreferrer">https://ibb.co/MVyvQRQ</a></p>
  
  <p>And for 5 suub tiles</p>
  
  <p><a href="https://ibb.co/XXVRqZn" rel="nofollow noreferrer">https://ibb.co/XXVRqZn</a></p>
  
  <p>What am I missing here?  How do I reliably sub divide a full screen quad into sub tiles? I must be missing something obvious, but I just cannot get this working.</p>