Sign in to follow this  
SiegeLord

OpenGL Using custom vertex declarations without a shader

Recommended Posts

SiegeLord    100
In a certain library that I am working on I have created this vertex format that is understandable by both OpenGL and Direct3D. On the Direct3D side I do this via the D3D vertex declarations (i.e. not the FVF constants). I do not need any vertex/pixel shaders, so I don't use any. This seems to work fine in retail mode, but when I run my library using the debug Direct3D libraries I get a lot of errors like this: 00000007 0.18344063 [724] Direct3D9: Decl Validator: X247: (Element Error) (Decl Element [0]) Declaration can't map to fixed function FVF because position must use D3DDECLTYPE_FLOAT3. 00000008 0.18347779 [724] Direct3D9: Decl Validator: X291: (Element Error) (Decl Element [2]) Declaration can't map to fixed function FVF because gaps or overlap between vertex elements are not allowed. Offset encountered is: 28, but expected offset is 12. 00000009 0.18351243 [724] Direct3D9: Decl Validator: X288: (Global Error) Declaration can't map to fixed function FVF because position field is missing. Usage: D3DDECLUSAGE_POSITION, with usage index 0 is required for fixed function. 00000010 0.18356439 [724] Direct3D9: (ERROR) :DrawPrimitiveUP failed. I.e. for some reason D3D is trying to shoehorn my vertex declaration into the FVF code, and is obviously failing. I read somewhere that unless you specify a vertex shader, D3D will assume that you are using the fixed function pipeline. Is that correct? Any way I can tell it that I don't want to use it? I'd prefer not to have to create a dummy shader, and I definitely can't alter the vertex structure to conform to a FVF vertex. Feel free to ask for code if that's needed.

Share this post


Link to post
Share on other sites
freeworld    341
If you're not using a shader, how do you expect directx to interpret you information, the vertex and pixel shader are major points in the pipeline.

Share this post


Link to post
Share on other sites
SiegeLord    100
I guess I expect it to generate a do-nothing shader, which is what the retail libraries apparently do, as it works without a hitch there. Perhaps I can ignore these errors?

Share this post


Link to post
Share on other sites
Buckeye    10747
You must call dev->SetFVF() before you call DrawPrimitiveUP so the pipeline knows how to render the buffer. And it expects the vertex data to match the vertex format to the extent it needs position, color, normals, etc. It also expects the vertex data in a certain order.

It appears you're using some vertex parameters for your use outside the pipeline. Is that correct?

If so, the following should work (I say not knowing exactly how you're doing things).

If your custom vertex structure is in the proper order to satisfy FVF demands, you can simply pad the FVF with extra TEXCOORDS so the FVF calculated size is the same as your custom size. The stride in the SetStreamSource must match the size calculated from the FVF. If you "fake" the FVF, you can set the stride in SetStreamSource to match - provided, of course, that your pipeline settings will ignore the extra data.

FVF data expects the actual vertex data order to match any FVF flags that are set for: "position, vertex blending weights, normal, colors, and the number and format of texture coordinates" (from the docs for Fixed Function FVF Codes). It will use, however, only the data from each vertex that the pipeline demands. Beyond that, your vertex can contain whatever data you want.

Share this post


Link to post
Share on other sites
MJP    19786
Quote:
Original post by SiegeLord
I read somewhere that unless you specify a vertex shader, D3D will assume that you are using the fixed function pipeline. Is that correct? Any way I can tell it that I don't want to use it? I'd prefer not to have to create a dummy shader, and I definitely can't alter the vertex structure to conform to a FVF vertex.


If your shaders are set to NULL, you'll get the fixed-function pipeline. The only way around that is to set shaders.


Share this post


Link to post
Share on other sites
SiegeLord    100
Quote:

You must call dev->SetFVF() before you call DrawPrimitiveUP so the pipeline knows how to render the buffer.

I don't use SetFVF(), I use SetVertexDeclaration(). As for rearranging the vertex layout, that's not really an option for me.

Quote:

If your shaders are set to NULL, you'll get the fixed-function pipeline. The only way around that is to set shaders.

I see, that's unfortunate... Do you know if setting either shader type will do the trick, or it must be a vertex shader?

Wonder if just this might be sufficient, hehe:
"mov oPos, v0".

Share this post


Link to post
Share on other sites
MJP    19786
Quote:
Original post by SiegeLord
Wonder if just this might be sufficient, hehe:
"mov oPos, v0".


You'll need to set both. And that's a sufficient vertex shader, at least if you add the header and dcl stuff. If you want you can even create a super-basic shader, compile it to byte code, and then store the compiled byte code as a static array of unsigned int in some class. That way you don't have to depend on a file.

Share this post


Link to post
Share on other sites
SiegeLord    100
Okay. I think I'll end up just ignoring the errors... making these shaders work is too much trouble, as the library supports arbitrary vertex layouts and I'd have to generate the shaderes during runtime, which I just don't feel like doing. Direct3D side of the library is already a giant hack to bring it up to a semblance of flexibility offered by OpenGL, no need to make it worse, hah.

Thanks for the help, everyone.

Share this post


Link to post
Share on other sites
SiegeLord    100
Sure.


//The vertex structure
struct PRIM_COLOR
{
uint32_t d3d_color;
float r, g, b, a;
};

struct VERTEX
{
float x, y;
PRIM_COLOR color;
float u, v;
};

//The declaration
D3DVERTEXELEMENT9 vertex_decl[] =
{
{0, 0, D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0},
{0, 8, D3DDECLTYPE_D3DCOLOR, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_COLOR, 0},
{0, 28, D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 0},
D3DDECL_END()
}



Note the color structure. That is the reason I can't change the vertex layout, because I want to keep the OpenGL color (the 4 floats) together with the D3D color (so the user can just treat the structure as a cross-API color), and this is not possible if you use an FVF, since it requires the vertex to be: position,color,texture coords,non-fvf data. Since the non-FVF data is in this case the OpenGL color, I am stuck. Also, OpenGL cannot use the D3D color because the color components are in a different order.

Also, I have this function elsewhere in the code:


VERTEX_DECL* create_vertex_decl(const VERTEX_ELEMENT* elements, int stride)
{
VERTEX_DECL* ret = malloc(sizeof(VERTEX_DECL));
ret->elements = malloc(sizeof(VERTEX_ELEMENT) * PRIM_ATTR_NUM);
memset(ret->elements, 0, sizeof(VERTEX_ELEMENT) * PRIM_ATTR_NUM);
while(elements->attribute) {
ret->elements[elements->attribute] = *elements;
elements++;
}

#ifdef CFG_D3D
{
int flags = get_display_flags();
if (flags & DIRECT3D) {
DISPLAY *display;
LPDIRECT3DDEVICE9 device;
D3DVERTEXELEMENT9 d3delements[PRIM_ATTR_NUM + 1];
int idx = 0;
VERTEX_ELEMENT* e;
D3DCAPS9 caps;

display = get_current_display();
device = d3d_get_device(display);

IDirect3DDevice9_GetDeviceCaps(device, &caps);
if(caps.PixelShaderVersion < D3DPS_VERSION(3, 0)) {
ret->d3d_decl = 0;
} else {
e = &ret->elements[PRIM_POSITION];
if(e->attribute) {
int type = 0;
switch(e->storage) {
case PRIM_FLOAT_2:
type = D3DDECLTYPE_FLOAT2;
break;
case PRIM_FLOAT_3:
type = D3DDECLTYPE_FLOAT3;
break;
case PRIM_SHORT_2:
type = D3DDECLTYPE_SHORT2;
break;
}
d3delements[idx].Stream = 0;
d3delements[idx].Offset = e->offset;
d3delements[idx].Type = type;
d3delements[idx].Method = D3DDECLMETHOD_DEFAULT;
d3delements[idx].Usage = D3DDECLUSAGE_POSITION;
d3delements[idx].UsageIndex = 0;
idx++;
}

e = &ret->elements[PRIM_TEX_COORD];
if(!e->attribute)
e = &ret->elements[PRIM_TEX_COORD_PIXEL];
if(e->attribute) {
int type = 0;
switch(e->storage) {
case PRIM_FLOAT_2:
case PRIM_FLOAT_3:
type = D3DDECLTYPE_FLOAT2;
break;
case PRIM_SHORT_2:
type = D3DDECLTYPE_SHORT2;
break;
}
d3delements[idx].Stream = 0;
d3delements[idx].Offset = e->offset;
d3delements[idx].Type = type;
d3delements[idx].Method = D3DDECLMETHOD_DEFAULT;
d3delements[idx].Usage = D3DDECLUSAGE_TEXCOORD;
d3delements[idx].UsageIndex = 0;
idx++;
}

e = &ret->elements[PRIM_COLOR_ATTR];
if(e->attribute) {
d3delements[idx].Stream = 0;
d3delements[idx].Offset = e->offset;
d3delements[idx].Type = D3DDECLTYPE_D3DCOLOR;
d3delements[idx].Method = D3DDECLMETHOD_DEFAULT;
d3delements[idx].Usage = D3DDECLUSAGE_COLOR;
d3delements[idx].UsageIndex = 0;
idx++;
}

d3delements[idx].Stream = 0xFF;
d3delements[idx].Offset = 0;
d3delements[idx].Type = D3DDECLTYPE_UNUSED;
d3delements[idx].Method = 0;
d3delements[idx].Usage = 0;
d3delements[idx].UsageIndex = 0;

IDirect3DDevice9_CreateVertexDeclaration(device, d3delements, (IDirect3DVertexDeclaration9**)&ret->d3d_decl);
}
}
}
#else
ret->d3d_decl = 0;
#endif

ret->stride = stride;
return ret;
}



So, even if I do write a shader for that declaration up-top, it will still not work for the custom declarations I support.

Share this post


Link to post
Share on other sites
SiegeLord    100
Because I just want the no-frills one texture, Goraud shaded polygons. The only reason I'm using the custom vertex declaration is because the FVF vertex formats place unduly strict restrictions on my vertex structure layout (see my previous post).

Share this post


Link to post
Share on other sites
jyk    2094
Quote:
Original post by SiegeLord
Because I just want the no-frills one texture, Goraud shaded polygons. The only reason I'm using the custom vertex declaration is because the FVF vertex formats place unduly strict restrictions on my vertex structure layout (see my previous post).
My understanding is that you can use vertex declarations with the FFP, but the layout must match one of the FVF formats (which it sounds like you already know). If that's true, then it seems like you would either have to change your vertex format, or use shaders.

In your engine, is the graphics API switchable at run time? Or do you have separate builds for the Direct3D and OpenGL versions?

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this  

  • Similar Content

    • By pseudomarvin
      I assumed that if a shader is computationally expensive then the execution is just slower. But running the following GLSL FS instead just crashes
      void main() { float x = 0; float y = 0; int sum = 0; for (float x = 0; x < 10; x += 0.00005) { for (float y = 0; y < 10; y += 0.00005) { sum++; } } fragColor = vec4(1, 1, 1 , 1.0); } with unhandled exception in nvoglv32.dll. Are there any hard limits on the number of steps/time that a shader can take before it is shut down? I was thinking about implementing some time intensive computation in shaders where it would take on the order of seconds to compute a frame, is that possible? Thanks.
    • By Arulbabu Donbosco
      There are studios selling applications which is just copying any 3Dgraphic content and regenerating into another new window. especially for CAVE Virtual reality experience. so that the user opens REvite or CAD or any other 3D applications and opens a model. then when the user selects the rendered window the VR application copies the 3D model information from the OpenGL window. 
      I got the clue that the VR application replaces the windows opengl32.dll file. how this is possible ... how can we copy the 3d content from the current OpenGL window.
      anyone, please help me .. how to go further... to create an application like VR CAVE. 
       
      Thanks
    • By cebugdev
      hi all,

      i am trying to build an OpenGL 2D GUI system, (yeah yeah, i know i should not be re inventing the wheel, but this is for educational and some other purpose only),
      i have built GUI system before using 2D systems such as that of HTML/JS canvas, but in 2D system, i can directly match a mouse coordinates to the actual graphic coordinates with additional computation for screen size/ratio/scale ofcourse.
      now i want to port it to OpenGL, i know that to render a 2D object in OpenGL we specify coordiantes in Clip space or use the orthographic projection, now heres what i need help about.
      1. what is the right way of rendering the GUI? is it thru drawing in clip space or switching to ortho projection?
      2. from screen coordinates (top left is 0,0 nd bottom right is width height), how can i map the mouse coordinates to OpenGL 2D so that mouse events such as button click works? In consideration ofcourse to the current screen/size dimension.
      3. when let say if the screen size/dimension is different, how to handle this? in my previous javascript 2D engine using canvas, i just have my working coordinates and then just perform the bitblk or copying my working canvas to screen canvas and scale the mouse coordinates from there, in OpenGL how to work on a multiple screen sizes (more like an OpenGL ES question).
      lastly, if you guys know any books, resources, links or tutorials that handle or discuss this, i found one with marekknows opengl game engine website but its not free,
      Just let me know. Did not have any luck finding resource in google for writing our own OpenGL GUI framework.
      IF there are no any available online, just let me know, what things do i need to look into for OpenGL and i will study them one by one to make it work.
      thank you, and looking forward to positive replies.
    • By fllwr0491
      I have a few beginner questions about tesselation that I really have no clue.
      The opengl wiki doesn't seem to talk anything about the details.
       
      What is the relationship between TCS layout out and TES layout in?
      How does the tesselator know how control points are organized?
          e.g. If TES input requests triangles, but TCS can output N vertices.
             What happens in this case?
      In this article,
      http://www.informit.com/articles/article.aspx?p=2120983
      the isoline example TCS out=4, but TES in=isoline.
      And gl_TessCoord is only a single one.
      So which ones are the control points?
      How are tesselator building primitives?
    • By Orella
      I've been developing a 2D Engine using SFML + ImGui.
      Here you can see an image
      The editor is rendered using ImGui and the scene window is a sf::RenderTexture where I draw the GameObjects and then is converted to ImGui::Image to render it in the editor.
      Now I need to create a 3D Engine during this year in my Bachelor Degree but using SDL2 + ImGui and I want to recreate what I did with the 2D Engine. 
      I've managed to render the editor like I did in the 2D Engine using this example that comes with ImGui. 
      3D Editor preview
      But I don't know how to create an equivalent of sf::RenderTexture in SDL2, so I can draw the 3D scene there and convert it to ImGui::Image to show it in the editor.
      If you can provide code will be better. And if you want me to provide any specific code tell me.
      Thanks!
  • Popular Now