<p>I want to create a single-channel 2d texture with bytebuffer which contain each float for each pixel (let say it is an alpha value) to use further in shader</p>
<p>Btw, I've tried this</p>
<pre><code>glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, width, height, 0, GL_ALPHA, GL_FLOAT, null)
// Then I call below when I want to change value in those texture
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height, GL_ALPHA, GL_FLOAT, buffer)
</code></pre>
<p>with access the alpha value as <code>tex.a</code> in shader
And it just works fine on only some devices, and as you can see as a reference from <a href="https://www.khronos.org/registry/OpenGL-Refpages/es3.0/html/glTexImage2D.xhtml" rel="nofollow noreferrer">doc</a> it would raise <code>INVALID_OPERATION</code> which happens in other devices but not some devices I mention before.</p>
<p>So the question is how would I make this right?</p>
<p>I also tried this</p>
<pre><code>glTexImage2D(GL_TEXTURE_2D, 0, GL_R32F, width, height, 0, GL_RED, GL_FLOAT, null)
// Then I call below when I want to change value in those texture
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height, GL_RED, GL_FLOAT, buffer)
</code></pre>
<p>I think it suppose to do the same but place value in red channel but It seems the value is rgba (0, 0, 0, 1) on every pixel.</p>
<p>And another question is what happened in those devices that work?</p>
<p>PS. All I want to do is some kind of use that buffer as alpha value for texture so If it possible to send it directly to texture please show me.</p>
<p>Update: Addition information I've found</p>
<ul>
<li>bytebuffer is actually a floatbuffer</li>
<li>both of the solution work on the device that work but in shader of the second one It need to access <code>tex.r</code> as we tell GL that we provide data in red channel</li>
</ul>

CC BY-SA 4.0
2021-09-03T11:42:36.023Z
According to the tables on the linked web page It looks like the type variable needs to be GL_UNSIGNED_BYTE not GL_FLOAT for the call to glTexImage2D. It sounds like the format of the data being sent is byte data as well so the call to TexSubImage2D should have one of the  byte formats too.
pmw1234
CC BY-SA 4.0
2021-09-03T12:34:19.430Z
@pmw1234 Thanks for your comment, I assumed you talk about first one, and yes I have tried that but the problem here is I just found that those bytebuffer actually a float value with 4 byte for each buffer (so it may call floatbuffer instead ?) anyway as the data is float with 4 byte It won't map really well, in that case since we tell GL we are sending 1 byte for a buffer so the result is kind of like the texture is a noise not a mask.
GottaK
CC BY-SA 4.0
2021-09-03T14:48:18.430Z
Either way you still have to stick to the allowed values that are set in the tables for the respective calls. Not every combination of data is allowed and if that type is not allowed then you will have to convert the data to the allowable formats.
pmw1234
CC BY-SA 4.0
2021-09-04T07:58:41.930Z
Yep, That why I change to my second fix but even the second fix in some devices I tested on, it resulted in texture that only contain rgba(0, 0, 0, 1) (know this by direct texture out of fragment shader and see black screen) and I got no clue after that.
GottaK
<texture><opengl-es>
glTexImage2d from bytebuffer
GottaK
# glTexImage2d from bytebuffer

(Asked by: username_0 on 2021-09-03)

I want to create a single-channel 2d texture with bytebuffer which contain each float for each pixel (let say it is an alpha value) to use further in shader

Btw, I've tried this

```
glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, width, height, 0, GL_ALPHA, GL_FLOAT, null)
// Then I call below when I want to change value in those texture
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height, GL_ALPHA, GL_FLOAT, buffer)

```

with access the alpha value as `tex.a` in shader
And it just works fine on only some devices, and as you can see as a reference from [doc](https://www.khronos.org/registry/OpenGL-Refpages/es3.0/html/glTexImage2D.xhtml) it would raise `INVALID_OPERATION` which happens in other devices but not some devices I mention before.

So the question is how would I make this right?

I also tried this

```
glTexImage2D(GL_TEXTURE_2D, 0, GL_R32F, width, height, 0, GL_RED, GL_FLOAT, null)
// Then I call below when I want to change value in those texture
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height, GL_RED, GL_FLOAT, buffer)

```

I think it suppose to do the same but place value in red channel but It seems the value is rgba (0, 0, 0, 1) on every pixel.

And another question is what happened in those devices that work?

PS. All I want to do is some kind of use that buffer as alpha value for texture so If it possible to send it directly to texture please show me.

Update: Addition information I've found

* bytebuffer is actually a floatbuffer
* both of the solution work on the device that work but in shader of the second one It need to access `tex.r` as we tell GL that we provide data in red channel

**Tags:** texture, opengl-es

> **Comments:**
> **username_1**: According to the tables on the linked web page It looks like the type variable needs to be GL_UNSIGNED_BYTE not GL_FLOAT for the call to glTexImage2D. It sounds like the format of the data being sent is byte data as well so the call to TexSubImage2D should have one of the  byte formats too.  
> **username_0**: @username_1 Thanks for your comment, I assumed you talk about first one, and yes I have tried that but the problem here is I just found that those bytebuffer actually a float value with 4 byte for each buffer (so it may call floatbuffer instead ?) anyway as the data is float with 4 byte It won't map really well, in that case since we tell GL we are sending 1 byte for a buffer so the result is kind of like the texture is a noise not a mask.  
> **username_1**: Either way you still have to stick to the allowed values that are set in the tables for the respective calls. Not every combination of data is allowed and if that type is not allowed then you will have to convert the data to the allowable formats.  
> **username_0**: Yep, That why I change to my second fix but even the second fix in some devices I tested on, it resulted in texture that only contain rgba(0, 0, 0, 1) (know this by direct texture out of fragment shader and see black screen) and I got no clue after that.  

---

## 0 answers

