Remapping a VBO to a texture?

Started by
7 comments, last by Dabacle 16 years, 10 months ago
Hi all. I've been trying to write some code to take a VBO and use it as a texture. I read something about the new extension GL_TEXTURE_BUFFER_EXT where I could simply bind the vertex buffer as a texture buffer using glTexBuffer and access it using samplerBuffer in a shader, but I'm currently using Cg and I tried it and samplerBuffer is not accepted as a variable type. Does anyone have any experience using the texture buffer object and Cg? Or does anyone know of another way to map the VBO to a texture without having to copy the data back to the cpu? Is it just only supported in GLSL at the moment? Thanks in advance! Dabacle
----------------------------Yes, I know Dabacle is spelled wrong! :P
Advertisement
The extensions which let you remap a buffer to another type of buffer are going to be DX10 cards only; so G80 and R600 based.

However, it might be posible to readback the data from the vbo and get the gpu to copy it into a texture. note: this is huge theory, I've not tried it and I'm a bit tired so it might not work, however it's the only way I can think of.

glGenBuffers(1, &textureBuffer);glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, textureBuffer);glBufferData(GL_PIXEL_UNPACK_BUFFER_ARB, __size__,                     NULL, GL_DYNAMIC_DRAW);glBindBuffer(GL_ARRAY_BUFFER_ARB, vertexbuffer);// Read into PBOglGetBufferSubDataARB(GL_ARRAY_BUFFER_ARB,0,__size__,NULL);glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB,0);glBindTexture(GL_TEXTURE_2D,textureBuffer);// use as texture?


At 6:30am this makes sense to my brain; logically it should work.. as to if OGL will let you do it.. well, that's another matter [smile]

__size__ is the size of the data to be copied in bytes.
Requires the PBO extension to work.
phantom if i understood you post correctly, you are using a PBO as a texture directly. I don't think this is possible. Textures and buffer objects are different things. What i think should work is this (though i have never tried it) :

glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, vboID);glBindTexture(GL_TEXTURE_2D, texID);glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, texWidth, texHeight, texFormat, dataType, 0);glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, 0);


where texWidth, texHeight, texFormat and dataType have the appropriate values to match the data in the VBO.

Basically you bind the ARRAY_BUFFER id as a PIXEL_UNPACK_BUFFER, which is supported by the extensions. This way you may have to copy the data from the VBO to the texture, but the copy should be fast because everything stay on the GPU side (i think so at least).

Hope i haven't said anything wrong.

HellRaiZer
HellRaiZer
Thanks for the replies guys. I'm going to try that out. I've heard there was a way to do it without involving the texture buffer object but I wasn't sure what it involved until now. I guess I'll go read up on GL_PIXEL_UNPACK_BUFFER_ARB too. :)
----------------------------Yes, I know Dabacle is spelled wrong! :P
Quote:Original post by HellRaiZer
phantom if i understood you post correctly, you are using a PBO as a texture directly. I don't think this is possible. Textures and buffer objects are different things. What i think should work is this (though i have never tried it) :


Ah yes, your code does make more sense than mine... well, it was 6:30am after all [grin]

@Dabacle

One question does occure to me; why do you need todo this? How do you not have a copy of the VBO in memory to upload to a texture anyways?
Yeah it does seem kind of weird huh?

Basically I want the geometry shader output, which gets streamed to a VBO, to end up in a texture :). For a college project my partner and I are implementing LSystems and subsequent tree branch generation completely on the GPU using the geometry shader to both create the LSystem and to parse it again to create the branches. Since the branch creation is inherantly serial (to get more than just fractals) we need a way to traverse the LSystem stored in the VBO. So the problem was we had to map it back to the CPU and save it as a texture for lookups. I'm going to implement the method you guys suggested either tomorrow or monday. I don't have a G80 at home so I have to do all the work on campus :(...
----------------------------Yes, I know Dabacle is spelled wrong! :P
Quote:Original post by Dabacle
Basically I want the geometry shader output, which gets streamed to a VBO, to end up in a texture :).
Ok, so you are in the DX10 feature set, in which case, it is possible to simply coerce the VBO to a texture. Problem is, I have no idea how to do it in OGL, just D3D 10.
SlimDX | Ventspace Blog | Twitter | Diverse teams make better games. I am currently hiring capable C++ engine developers in Baltimore, MD.
Quote:Original post by Dabacle
Basically I want the geometry shader output, which gets streamed to a VBO, to end up in a texture :). For a college project my partner and I are implementing LSystems and subsequent tree branch generation completely on the GPU using the geometry shader to both create the LSystem and to parse it again to create the branches. Since the branch creation is inherantly serial (to get more than just fractals) we need a way to traverse the LSystem stored in the VBO. So the problem was we had to map it back to the CPU and save it as a texture for lookups. I'm going to implement the method you guys suggested either tomorrow or monday.

You don't have to do the copy if you are on a G80. The process you described in the first post sounds reasonable. I don't have a G80 so i can't test it, but you said your problem was that Cg don't understand the samplerBuffer type.

What Cg version are you using? I assume it's 2.0, which comes with the nVidia's OGL SDK 10.0, because you are already using geometry shaders. Unfortunately all the SM4.0 related things are undocumented yet. I've done a quick disasm of the cg.dll and it looks like samplerBuffer is a valid keyword. One thing i can suggest, is trying to replace samplerBuffer with samplerBUF and see what happens. It appears in the string list of cg.dll, and because Cg is capable of transforming code to GLSL i think samplerBuffer is the keyword used for the GLSL output. And taking into account the texture arrays are specified with e.g. sampler2DARRAY (all caps) then i think samplerBUF is what should work.

Other than that i have no clue :)

HellRaiZer
HellRaiZer
Hey thanks for the advice! I honestly hadn't thought about the version of Cg installed on that particular machine. I guess I assumed it was the most recent but that could totally be a possibility. When I go back in tomorrow/Monday I'll see what version is installed and try out samplerBUF (and samplerBuffer again if I have to install Cg 2.0) to see if that works. If those don't I'll fall back on the pixel unpack buffer. :)
----------------------------Yes, I know Dabacle is spelled wrong! :P

This topic is closed to new replies.

Advertisement