D3D9 error keeps stream from working (i couldn't think of a better title)

Started by
10 comments, last by Enerjak 12 years, 11 months ago
Hello, my name's rosario Corsini, i've recently decided to make my own D3D9/10/11/OPENGL graphics engine, I'm trying to make it shader based
basically use shaders to render everything so it can be nice and work with all 3 APIS (9/10/11) I'm running into a problem, see, I'm using hlsl rather then FX files and i tried to create a declaration and then set the decl and set the register/data and when i run it, i get this D3DDEBUG error:



[size="1"]Direct3D9: (ERROR) :Vertex stride in stream 0 is less than in the declaration

First-chance exception at 0x75d8b727 in IApp.exe: Microsoft C++ exception: long at memory location 0x005bf980..

Direct3D9: (ERROR) :DrawPrimitive failed.





now here is my code for setting/drawing the quad


ScreenQuad::ScreenQuad()
{
pGPU9 = new IHighLevelGpu9();
pGPU9->createProgram("Screen","main",VSP_VS_2_0,"Screen_Pixel",PSP_2_0);
pGPU9->loadProgram("Screen_Quad.hlsl");
}

ScreenQuad::~ScreenQuad()
{

}

void ScreenQuad::setRenderTarget(IRenderTarget9* pTarget)
{
mTarget = pTarget;

}
bool ScreenQuad::createScreenQuad()
{
ID3D9Renderer::getDev9()->CreateVertexBuffer(6* sizeof(VertexPCT),
0,
0,
D3DPOOL_DEFAULT,
&pQuadVert,
NULL);

D3DVERTEXELEMENT9 mVertexElements[] =
{
{0, 0, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0},
{0, 12, D3DDECLTYPE_FLOAT4, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_COLOR, 0},
{0, 24, D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 0},
D3DDECL_END(),
};

ID3D9Renderer::getDev9()->CreateVertexDeclaration(mVertexElements,&pDec);

pQuadVert->Lock(0,0,(void**)&this->pVertPCT,0);

pVertPCT[0] = VertexPCT(-1.0f, 1.0f, 0.0f,D3DCOLOR_ARGB(255,255,255,255),0.0f, 0.0f);
pVertPCT[1] = VertexPCT(1.0f, 1.0f, 0.0f,D3DCOLOR_ARGB(255,255,255,255), 1.0f, 0.0f);
pVertPCT[2] = VertexPCT(-1.0f, -1.0f, 0.0f,D3DCOLOR_ARGB(255,255,255,255), 0.0f, 1.0f);
pVertPCT[3] = VertexPCT(-1.0f, -1.0f, 0.0f,D3DCOLOR_ARGB(255,255,255,255), 0.0f, 1.0f);
pVertPCT[4] = VertexPCT(1.0f, 1.0f, 0.0f, D3DCOLOR_ARGB(255,255,255,255), 1.0f, 0.0f);
pVertPCT[5] = VertexPCT(1.0f, -1.0f, 0.0f,D3DCOLOR_ARGB(255,255,255,255), 1.0f, 1.0f);
pQuadVert->Unlock();
return true;
}

void ScreenQuad::drawQuad()
{

ID3D9Renderer::getDev9()->SetStreamSource(0,pQuadVert,0,sizeof(VertexPCT));
ID3D9Renderer::getDev9()->SetStreamSourceFreq(0,D3DSTREAMSOURCE_INDEXEDDATA | 20);
ID3D9Renderer::getDev9()->SetVertexDeclaration(pDec);
ID3D9Renderer::getDev9()->SetPixelShaderConstantF(0,reinterpret_cast<float*>(mTarget->getTxt()),2);
pGPU9->BeginProgram();
ID3D9Renderer::getDev9()->DrawPrimitive(D3DPT_TRIANGLELIST,0,2);

}







Advertisement
Your vertex declaration doesn't seem to match the layout of your [color="#660066"][font="CourierNew, monospace"]VertexPCT[/font] structure.

Your vertex declaration is describing each property as being made of float types, whereas your [color="#660066"][font="CourierNew, monospace"]VertexPCT[/font] structure seems to use bytes (not floats) for it's colour values. Furthermore, your Texcoord and Colour properties are overlapping, like so (i.e. with everything made of floats, the texcoords should have an offset of 28, not 24):struct
{
float x, y, z;
float r, g, b;
union { float a; float u; };
float v;
}
I can't be sure without seeing your [color="#660066"][font="CourierNew, monospace"]VertexPCT[/font] structure , but I'm guessing your declaration should look like D3DVERTEXELEMENT9 mVertexElements[] =
{
{0, 0, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0},
{0, 12, D3DDECLTYPE_D3DCOLOR, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_COLOR, 0},
{0, 16, D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 0},
D3DDECL_END(),
};

Your vertex declaration doesn't seem to match the layout of your [color="#660066"][font="CourierNew, monospace"]VertexPCT[/font] structure.

Your vertex declaration is describing each property as being made of float types, whereas your [color="#660066"][font="CourierNew, monospace"]VertexPCT[/font] structure seems to use bytes (not floats) for it's colour values. Furthermore, your Texcoord and Colour properties are overlapping, like so (i.e. with everything made of floats, the texcoords should have an offset of 28, not 24):struct
{
float x, y, z;
float r, g, b;
union { float a; float u; };
float v;
}
I can't be sure without seeing your [color="#660066"][font="CourierNew, monospace"]VertexPCT[/font] structure , but I'm guessing your declaration should look like D3DVERTEXELEMENT9 mVertexElements[] =
{
{0, 0, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0},
{0, 12, D3DDECLTYPE_D3DCOLOR, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_COLOR, 0},
{0, 16, D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 0},
D3DDECL_END(),
};





class VertexPCT : public VertexPosColor
{
public:
VertexPCT(float X, float Y, float Z,
DWORD color, float U, float V)
: VertexPosColor(X,Y,Z,color),fU(U),
fV(V)
{

}

float fU;
float fV;



here 's my VertexPCT class/struct hope this helps

I actually got it to work, thanks, you got any advice so that this will never happen again? like how can i tell how much to offset when declaring the vertex elements? I want to learn as much as possible so it be easy to set this up in the future, thank you


struct VS_OUTPUT
{
float4 pos : POSITION0;
float4 Color : COLOR;
float2 Texture : TEXCOORD;
};

struct VS_INPUT
{
float4 pos : POSITION0;
float4 Color : COLOR;
float2 Texture : TEXCOORD;
};

VS_OUTPUT main(VS_INPUT input)
{
VS_OUTPUT outP;
outP.pos = float4(input.pos.xy,0,1);

outP.Color = input.Color;

outP.Texture.x = 0.5*(1 - input.pos.x);
outP.Texture.y = 0.5*(1 + input.pos.y);

return outP;
}





do you see anything wrong with this? because the compiler is giving me errors.




J:\maxTo3DS\IRenderer\Debug\Screen_Q
t semantic 'POSITION0': Legal indice
J:\maxTo3DS\IRenderer\Debug\Screen_Q
_0 input semantic 'POSITION0'
J:\maxTo3DS\IRenderer\Debug\Screen_Q
0 output semantic 'POSITION0'
J:\maxTo3DS\IRenderer\Debug\Screen_Q
0 output semantic 'TEXCOORD'

compilation failed; no code produced



looks ok to me....let me know what you think

here 's my VertexPCT class/struct hope this helps
That doesn't help because it doesn't show the entire data definition (it only shows [font="Lucida Console"]u[/font] and [font="Lucida Console"]v[/font]). There could still be anything in [font="Lucida Console"]VertexPosColor[/font] for all we know.
any advice so that this will never happen again? like how can i tell how much to offset when declaring the vertex elements?
If you want to use structs to represent your vertices (and be able to map vertex buffers to arrays of those structs), you should first be familliar with your compiler's packing rules.

You're basically guessing at the byte-offset of each member variable inside the structure.struct VertexPCT
{
float x;//first member, offset = 0
float y;
float z;
DWORD color; // sizeof(float) == 4. There's 3 floats before this. So the offset is 3x4 == 12
float u;//size of DWORD is also 4. There's 3 floats and a DWORD before this, so the offset is 12+4 == 16
float v;
};
You can check if you're right by checking your guess against the compiler's answer:VertexPCT test;
char* base = (char*)&test;
assert( &test.x == (float*)(base + 0) );
assert( &test.y == (float*)(base + 4) );
assert( &test.z == (float*)(base + 8) );
assert( &test.color == (DWORD*)(base + 12) );
assert( &test.u == (float*)(base + 16) );
assert( &test.v == (float*)(base + 20) );

do you see anything wrong with this? because the compiler is giving me errors.
looks ok to me....let me know what you think
Your error messages are cut off mid-sentence...

[quote name='Enerjak' timestamp='1303878939' post='4803395']
do you see anything wrong with this? because the compiler is giving me errors.
looks ok to me....let me know what you think
Your error messages are cut off mid-sentence...
[/quote]


Untitled-3.png?t=1303880258

hope this is better...
Usage: fxc <options> <files>[/quote]I dunno if this is your problem, but you're supposed to put your usage switches before the filename.
i.e.
[font="Courier New"]fxc /T ps_3_0 blah.fx[/font]
instead of:
[font="Courier New"]fxc blah.fx /T ps_3_0[/font]

Also, if you're trying to compile that shader you posted earlier, you'll want the vs_3_0 profile, as it's a vertex shader.

Usage: fxc <options> <files>
I dunno if this is your problem, but you're supposed to put your usage switches before the filename.
i.e.
[font="Courier New"]fxc /T ps_3_0 blah.fx[/font]
instead of:
[font="Courier New"]fxc blah.fx /T ps_3_0[/font]

Also, if you're trying to compile that shader you posted earlier, you'll want the vs_3_0 profile, as it's a vertex shader.
[/quote]


Ok, that compiled but this was my original intention:



struct VS_OUTPUT
{
float4 pos : POSITION0;
float4 Color : COLOR;
float2 Texture : TEXCOORD;
};

struct VS_INPUT
{
float4 pos : POSITION0;
float4 Color : COLOR;
float2 Texture : TEXCOORD;
};

VS_OUTPUT main(VS_INPUT input)
{
VS_OUTPUT outP;
outP.pos = float4(input.pos.xy,0,1);

outP.Color = input.Color;

outP.Texture.x = 0.5*(1 - input.pos.x);
outP.Texture.y = 0.5*(1 + input.pos.y);

return outP;
}

texture gpuTex;



sampler2D Screen : register(s0) = sampler_state
{
Texture = (gpuTex);
ADDRESSU = WRAP;
ADDRESSV = WRAP;
MINFILTER = LINEAR;
MAGFILTER = LINEAR;
};

float4 PS(float2 tex : TEXCOORD0) : COLOR0
{
return tex2D(Screen,tex);
}



to have both in one hlsl file now when i compile to vs_3_0 how do i know that the PS also went to 3_0? i assume it got both, but it only shows one profile.

This topic is closed to new replies.

Advertisement