<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<!DOCTYPE bugzilla SYSTEM "https://jogamp.org/bugzilla/page.cgi?id=bugzilla.dtd">

<bugzilla version="5.2"
          urlbase="https://jogamp.org/bugzilla/"
          
          maintainer="sgothel@jausoft.com"
>

    <bug>
          <bug_id>1220</bug_id>
          
          <creation_ts>2015-09-18 14:25:28 +0200</creation_ts>
          <short_desc>glTexSubImage3D interprets the z index as third dimension although a 2d array texture has been provided</short_desc>
          <delta_ts>2016-01-23 13:24:57 +0100</delta_ts>
          <reporter_accessible>1</reporter_accessible>
          <cclist_accessible>1</cclist_accessible>
          <classification_id>3</classification_id>
          <classification>JogAmp</classification>
          <product>Jogl</product>
          <component>core</component>
          <version>2.3.2</version>
          <rep_platform>pc_x86_64</rep_platform>
          <op_sys>windows</op_sys>
          <bug_status>RESOLVED</bug_status>
          <resolution>INVALID</resolution>
          
          
          <bug_file_loc></bug_file_loc>
          <status_whiteboard></status_whiteboard>
          <keywords></keywords>
          <priority>P1</priority>
          <bug_severity>blocker</bug_severity>
          <target_milestone>---</target_milestone>
          
          
          <everconfirmed>1</everconfirmed>
          <reporter name="Giuseppe Barbieri">elect86</reporter>
          <assigned_to name="Sven Gothel">sgothel</assigned_to>
          <cc>sgothel</cc>
          
          <cf_type>DEFECT</cf_type>
          <cf_scm_refs></cf_scm_refs>
          <cf_workaround>---</cf_workaround>

      

      

      

          <comment_sort_order>oldest_to_newest</comment_sort_order>  
          <long_desc isprivate="0" >
    <commentid>5151</commentid>
    <comment_count>0</comment_count>
    <who name="Giuseppe Barbieri">elect86</who>
    <bug_when>2015-09-18 14:25:28 +0200</bug_when>
    <thetext>glTexSubImage3D interprets the z index as third dimension although a 2d array texture has been provided

ByteBuffer data = GLBuffers.newDirectByteBuffer(256*256);

gl4.glTexSubImage3D(GL4.GL_TEXTURE_2D_ARRAY, 0, 0, 0, 0, 256, 256, 2, GL_RED_INTEGER, GL_UNSIGNED_BYTE, data);

Caused by: java.lang.IndexOutOfBoundsException: Required 131072 remaining bytes in buffer, only had 65536
	at com.jogamp.common.nio.Buffers.rangeCheckBytes(Buffers.java:1056)
	at jogamp.opengl.gl4.GL4bcImpl.glTexSubImage3D(GL4bcImpl.java:7979)</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>5152</commentid>
    <comment_count>1</comment_count>
    <who name="Giuseppe Barbieri">elect86</who>
    <bug_when>2015-09-18 14:26:38 +0200</bug_when>
    <thetext>Crap, we can&apos;t edit comments..

anyway, here it&apos;s the api doc https://www.opengl.org/sdk/docs/man3/xhtml/glTexSubImage3D.xml

void glTexSubImage3D(GLenum target,  GLint level,  GLint xoffset,  GLint yoffset,  GLint zoffset,  GLsizei width,  GLsizei height,  GLsizei depth,  GLenum format,  GLenum type,  const GLvoid * data);

&quot;For three-dimensional textures, the z index refers to the third
            dimension. For two-dimensional array textures, the z index refers to
            the slice index.
&quot;</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>5273</commentid>
    <comment_count>2</comment_count>
    <who name="Sven Gothel">sgothel</who>
    <bug_when>2015-09-28 04:13:27 +0200</bug_when>
    <thetext>Implementation example:
  public void glTexSubImage3D(int target, int level, int xoffset, int yoffset, int zoffset, int width, int height, int depth, int format, int type, Buffer pixels)  {
    checkUnpackPBOUnbound(true);
    Buffers.rangeCheckBytes(pixels, 
                imageSizeInBytes(format, type, width, height, depth, false));
    ...

+++

Spec:

&quot;glTexSubImage3D redefines a contiguous subregion of an existing three-
dimensional or two-dimensioanl array texture image.
The texels referenced by data replace the portion of the
existing texture array with 
  x indices xoffset and xoffset+width-1, inclusive,
  y indices yoffset and yoffset+height-1, inclusive
  z indices zoffset and zoffset+depth-1, inclusive&quot;

Further, as Giuseppe mentioned:

&quot;For three-dimensional textures, the z index refers to the third
dimension. For two-dimensional array textures, the z index refers to
the slice index.&quot;
            
+++

Hence implementation completely ignores the [xyz] offset
while calculating the required minimum-size of the given buffer!

Impact: Application always needs to supply a fully-texture-sized buffer,
where only the updated region needs to be supplied in the buffer.

+++

Wonder how this bug could have survived undetected that long ..</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>5275</commentid>
    <comment_count>3</comment_count>
    <who name="Sven Gothel">sgothel</who>
    <bug_when>2015-09-28 04:27:51 +0200</bug_when>
    <thetext>Further more, we need to range-check and PBO check &apos;glTextureSubImage[123]D&apos;,
see Bug 1233.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>5277</commentid>
    <comment_count>4</comment_count>
    <who name="Sven Gothel">sgothel</who>
    <bug_when>2015-09-28 04:53:34 +0200</bug_when>
    <thetext>Invalidating my comment 2, guess I was reading the spec to quickly
w/o thinking (too later probably).

  x indices xoffset and xoffset+width-1, inclusive,
  y indices yoffset and yoffset+height-1, inclusive
  z indices zoffset and zoffset+depth-1, inclusive&quot;

this means that we simply have to calculate the imageSize
on the width x height x depth, as we do it already!

reverting the bug description to original.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>5278</commentid>
    <comment_count>5</comment_count>
    <who name="Sven Gothel">sgothel</who>
    <bug_when>2015-09-28 04:59:07 +0200</bug_when>
    <thetext>(In reply to Giuseppe Barbieri from comment #0)
&gt; glTexSubImage3D interprets the z index as third dimension although a 2d
&gt; array texture has been provided
&gt; 
&gt; ByteBuffer data = GLBuffers.newDirectByteBuffer(256*256);
&gt; 
&gt; gl4.glTexSubImage3D(GL4.GL_TEXTURE_2D_ARRAY, 0, 0, 0, 0, 256, 256, 2,
&gt; GL_RED_INTEGER, GL_UNSIGNED_BYTE, data);
&gt; 
&gt; Caused by: java.lang.IndexOutOfBoundsException: Required 131072 remaining
&gt; bytes in buffer, only had 65536
&gt; 	at com.jogamp.common.nio.Buffers.rangeCheckBytes(Buffers.java:1056)
&gt; 	at jogamp.opengl.gl4.GL4bcImpl.glTexSubImage3D(GL4bcImpl.java:7979)


You seem to use the API wrong,
i.e. you probably want to update slice index 2
using your TEXTURE_2D sub-image.
The depth should be 1 in this case, of course!</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>5279</commentid>
    <comment_count>6</comment_count>
    <who name="Sven Gothel">sgothel</who>
    <bug_when>2015-09-28 05:00:34 +0200</bug_when>
    <thetext>(In reply to Sven Gothel from comment #5)
&gt; 
&gt; You seem to use the API wrong,
&gt; i.e. you probably want to update slice index 2
&gt; using your TEXTURE_2D sub-image.
&gt; The depth should be 1 in this case, of course!

like this:

gl4.glTexSubImage3D(GL4.GL_TEXTURE_2D_ARRAY, 0, 0, 0, 2, 256, 256, 1,
GL_RED_INTEGER, GL_UNSIGNED_BYTE, data);

as mentioned above, no offset is even being used while calculating the
image size for the buffer range check.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>5331</commentid>
    <comment_count>7</comment_count>
    <who name="Giuseppe Barbieri">elect86</who>
    <bug_when>2015-10-03 17:28:38 +0200</bug_when>
    <thetext>I don&apos;t get it, if I want to update layer 2 of a 256x256 red/unsigned byte texture I need to supply a 256*256*1=65536 bytes buffer, not a 131072-bytes one.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>5333</commentid>
    <comment_count>8</comment_count>
    <who name="Sven Gothel">sgothel</who>
    <bug_when>2015-10-03 18:26:58 +0200</bug_when>
    <thetext>(In reply to Giuseppe Barbieri from comment #7)
&gt; I don&apos;t get it, if I want to update layer 2 of a 256x256 red/unsigned byte
&gt; texture I need to supply a 256*256*1=65536 bytes buffer, not a 131072-bytes
&gt; one.

131072 / 2 = 256*256

You put in factor 2, by depth argument, which must be 1 instead,
since you only want to update 1 layer. 

Read the comment above.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>5334</commentid>
    <comment_count>9</comment_count>
    <who name="Giuseppe Barbieri">elect86</who>
    <bug_when>2015-10-03 19:14:29 +0200</bug_when>
    <thetext>Ah, now I got what you meant, I&apos;ll try next week

thanks</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>5521</commentid>
    <comment_count>10</comment_count>
    <who name="Giuseppe Barbieri">elect86</who>
    <bug_when>2016-01-23 13:24:57 +0100</bug_when>
    <thetext>Yep, you were right, Sven, sorry for the trouble

Thanks</thetext>
  </long_desc>
      
      

    </bug>

</bugzilla>