I have simple declarations:
uses ...
Winapi.Direct3D9, Winapi.DXTypes, Winapi.D3DX9,
var
g_pVertexDeclaration: IDirect3DVertexDeclaration9 = nil;
const
decl: array [0 .. 4] of TD3DVertexElement9 = (
(Stream: 0; Offset: 0; _Type: D3DDECLTYPE_FLOAT3; Method: D3DDECLMETHOD_DEFAULT;
Usage: D3DDECLUSAGE_POSITION; UsageIndex: 0),
(Stream: 0; Offset: 3 * sizeof(single); _Type: D3DDECLTYPE_FLOAT3; Method: D3DDECLMETHOD_DEFAULT;
Usage: D3DDECLUSAGE_NORMAL; UsageIndex: 0),
(Stream: 0; Offset: 6 * sizeof(single); _Type: D3DDECLTYPE_FLOAT2; Method: D3DDECLMETHOD_DEFAULT;
Usage: D3DDECLUSAGE_TEXCOORD; UsageIndex: 0),
(Stream: 0; Offset: 8 * sizeof(single); _Type: D3DDECLTYPE_FLOAT2; Method: D3DDECLMETHOD_DEFAULT;
Usage: D3DDECLUSAGE_TEXCOORD; UsageIndex: 0),
(Stream: $FF; Offset: 0; _Type: D3DDECLTYPE_UNUSED; Method: TD3DDeclMethod(0);
Usage: TD3DDeclUsage(0); UsageIndex: 0) //({D3DDECL_END})
);
function to log errors
function CheckDxError(e: dword): boolean;
begin
if Failed(e) then begin
raise Exception.Create(format('dx error %d: %s, %s', [(e shr 16) and $7FFF, DXGetErrorString9(e),
DXGetErrorDescription9(e)]));
exit(false);
end;
result := true;
end;
and I have dx error 0: E_FAIL, An Undetermined error occured
at
CheckDxError(m_pd3dDevice.CreateVertexDeclaration(@decl, g_pVertexDeclaration));
DirectX 9, device etc initialized before call (and code works without shaders, I trying to study shaders), there was no error in this code with decl array with only first position and D3DDECL_END. But my vertex shader contains 4 input values and compiled without errors with fxc.exe from DirectX SDK.
struct VSInput
{
float3 Pos : POSITION;
float3 Normal : NORMAL;
float2 TexcoordUV : TEXCOORD0;
float2 TexcoordST : TEXCOORD1;
};
Why this error occurs and what to do to avoid?
Your second TEXCOORD
needs to have UsageIndex
set to 1
.
The documentation for D3DDECLUSAGE
does cover this:
D3DDECLUSAGE_TEXCOORD
Texture coordinate data. Use D3DDECLUSAGE_TEXCOORD, n to specify texture coordinates in fixed function vertex processing and in pixel shaders prior to ps_3_0. These can be used to pass user defined data.
The value of n
is the usage index and if you want to have multiple vertex elements with the same usage, you need to give them distinct usage indices.