Sign in to follow this  
Slaaitjuh

vertex shader input [FIXED]

Recommended Posts

Slaaitjuh    130
with vertex shaders, does the input need to be fully similar to what you are sending to it? so does the input struct need to be the same as the vertex declaration? this is the hlsl code:
struct VertexToPixel
{
    float4 Position     : POSITION;
    float3 Normal	: NORMAL;
     float2 TexCoords    : TEXCOORD0;


};

struct PixelToFrame
{
    float4 Color : COLOR0;
};

float4x4 xWorldViewProjection;



 Texture xColoredTexture;

sampler ColoredTextureSampler = sampler_state { texture = <xColoredTexture> ; magfilter = LINEAR; minfilter = LINEAR; mipfilter=LINEAR;AddressU = wrap; AddressV = wrap;};


 VertexToPixel SimplestVertexShader( float4 inPos : POSITION, float3 inNormal : NORMAL, float2 inTexCoords : TEXCOORD0
)


{
    VertexToPixel Output = (VertexToPixel)0;
    
    Output.Position = mul(inPos, xWorldViewProjection);

     Output.TexCoords = inTexCoords;
     Output.Normal = mul(inNormal, xWorldViewProjection);


    
    return Output;    
}

PixelToFrame OurFirstPixelShader(VertexToPixel PSIn)
{
    PixelToFrame Output = (PixelToFrame)0;



     Output.Color = tex2D(ColoredTextureSampler, PSIn.TexCoords);


    return Output;
}

technique Simplest
{
    pass Pass0
    {        
        VertexShader = compile vs_3_0 SimplestVertexShader();
        PixelShader = compile ps_3_0 OurFirstPixelShader();
    }
}



and this is my own vertex class, with the declaration in it:
    public struct VertexCollada : IVertex
    {
        private SlimDX.Vector4 position;
        public SlimDX.Vector3 Normal;
        public SlimDX.Vector2 TextureCoordinates;
        public SlimDX.Vector3 Tangent;
        public SlimDX.Vector3 Binormal;

        public SlimDX.Vector4 Position
        {
            get { return position; }
            set { position = value; }
        }

        public VertexCollada(SlimDX.Vector4 Position, SlimDX.Vector2 TextureCoordinates, SlimDX.Vector3 Normal, SlimDX.Vector3 Tangent, SlimDX.Vector3 Binormal)
        {
            this.position = Position;
            this.TextureCoordinates = TextureCoordinates;
            this.Normal = Normal;
            //this.Color = Color;
            this.Tangent = Tangent;
            this.Binormal = Binormal;
        }

        private static Framework.Graphics.VertexElement[] vertexElements = {
            new Framework.Graphics.VertexElement(0, 0, Framework.Graphics.DeclarationType.Float4, Framework.Graphics.DeclarationMethod.Default, Framework.Graphics.DeclarationUsage.Position, 0),
            new Framework.Graphics.VertexElement(0, 16, Framework.Graphics.DeclarationType.Float3, Framework.Graphics.DeclarationMethod.Default, Framework.Graphics.DeclarationUsage.Normal, 0),
            new Framework.Graphics.VertexElement(0, 28, Framework.Graphics.DeclarationType.Float2, Framework.Graphics.DeclarationMethod.Default, Framework.Graphics.DeclarationUsage.TextureCoordinate, 0),
            new Framework.Graphics.VertexElement(0, 36, Framework.Graphics.DeclarationType.Float3, Framework.Graphics.DeclarationMethod.Default, Framework.Graphics.DeclarationUsage.Tangent, 0),
            new Framework.Graphics.VertexElement(0, 48, Framework.Graphics.DeclarationType.Float3, Framework.Graphics.DeclarationMethod.Default, Framework.Graphics.DeclarationUsage.Binormal, 0)
        };

        public static Framework.Graphics.VertexElement[] VertexElements
        {
            get { return vertexElements; }
        }

        private static int sizeBytes = Marshal.SizeOf(typeof(VertexCollada));

        public static int SizeBytes
        {
            get { return sizeBytes; }
        }

        public static Framework.Graphics.VertexFormat VertexFormat
        {
            get
            {
                return VertexFormat.Position | VertexFormat.Normal | VertexFormat.Texture | VertexFormat.Tangent | VertexFormat.Binormal;
            }
        }


    }



the result is that the positions are right, but the texture coordinates are totally screwed up. I am 100% sure that the texture coordinates in the vertexBuffer are allright. The loading code is ported from an XNA importer. it almost looks right, but it's just not fully correct. Does anyone have a clue if i am doing something wrong in the above codes? [Edited by - Slaaitjuh on March 25, 2008 9:02:48 AM]

Share this post


Link to post
Share on other sites
Buckeye    10747
Not sure what the result is supposed to be, but I think you want to multiply the normal by just the world matrix, not the world-view-projection.

There are several shader examples in the SDK you may want to look at.

Share this post


Link to post
Share on other sites
Jroggy    172
Hmmm the HLSL code looks fine. I'm assuming you're matrix multiplying in the correct order. I'm not too familiar with SlimDX/XNA, so I can't help you there. I don't fully understand your first question, but if you mean do you have to send the shader the Position, Normal, etc.. in the same order as in the shader, I don't believe so. If you pass it the Tangent and Binormal, but don't use it in the shader that's fine. I doubt that would cause any overhead (if so, unnoticeable I'm sure), as you'd be sending it nothing anyway (since this particular shader does not use either).
EDIT: Actually one thing, I'm not familiar with SlimDX/XNA, I've only used HLSL with OGRE, but do you need to multiple the normal by the world view project matrix? I'm just asking because I don't do that (maybe the normals are transformed before given to the shader in OGRE? I'm not sure tbh). I'm still a beginner at HLSL, so don't take my word as gospel :P

Share this post


Link to post
Share on other sites
FoxHunter2    157
Show us the DirectX Debug output, I guess there's just something wrong with the input format, the debug runtime will tell you exactly what.
Usually the VS input should match the vertex declaration, so you might try this first.

Apart from that, you need to multiply the normals with the inverse world matrix, not the WVP matrix.

Share this post


Link to post
Share on other sites
Slaaitjuh    130

'Sample_Collada.exe': Loaded 'K:\development\realityFramework\trunk\Samples\Sample_Collada\bin\Release\Sample_Collada.exe', No native symbols in symbol file.
'Sample_Collada.exe': Loaded 'C:\WINDOWS\system32\ntdll.dll'
'Sample_Collada.exe': Loaded 'C:\WINDOWS\system32\mscoree.dll'
'Sample_Collada.exe': Loaded 'C:\WINDOWS\system32\kernel32.dll'
'Sample_Collada.exe': Loaded 'C:\WINDOWS\system32\advapi32.dll'
'Sample_Collada.exe': Loaded 'C:\WINDOWS\system32\rpcrt4.dll'
'Sample_Collada.exe': Loaded 'C:\WINDOWS\system32\shlwapi.dll'
'Sample_Collada.exe': Loaded 'C:\WINDOWS\system32\gdi32.dll'
'Sample_Collada.exe': Loaded 'C:\WINDOWS\system32\user32.dll'
'Sample_Collada.exe': Loaded 'C:\WINDOWS\system32\msvcrt.dll'
'Sample_Collada.exe': Loaded 'C:\WINDOWS\system32\imm32.dll'
'Sample_Collada.exe': Loaded 'C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\mscorwks.dll'
'Sample_Collada.exe': Loaded 'C:\WINDOWS\WinSxS\x86_Microsoft.VC80.CRT_1fc8b3b9a1e18e3b_8.0.50727.1433_x-ww_5cf844d2\msvcr80.dll'
'Sample_Collada.exe': Loaded 'C:\WINDOWS\system32\shell32.dll'
'Sample_Collada.exe': Loaded 'C:\WINDOWS\WinSxS\x86_Microsoft.Windows.Common-Controls_6595b64144ccf1df_6.0.2600.2982_x-ww_ac3f9c03\comctl32.dll'
'Sample_Collada.exe': Loaded 'C:\WINDOWS\system32\comctl32.dll'
'Sample_Collada.exe': Loaded 'C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Culture.dll'
'Sample_Collada.exe': Unloaded 'C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Culture.dll'
'Sample_Collada.exe': Loaded 'C:\WINDOWS\assembly\NativeImages_v2.0.50727_32\mscorlib\32e6f703c114f3a971cbe706586e3655\mscorlib.ni.dll'
'Sample_Collada.exe' (Managed): Loaded 'C:\WINDOWS\assembly\GAC_32\mscorlib\2.0.0.0__b77a5c561934e089\mscorlib.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'Sample_Collada.exe': Loaded 'C:\WINDOWS\system32\ole32.dll'
'Sample_Collada.exe': Loaded 'C:\WINDOWS\system32\uxtheme.dll'
'Sample_Collada.exe': Loaded 'C:\WINDOWS\system32\MSCTF.dll'
'Sample_Collada.exe' (Managed): Loaded 'K:\development\realityFramework\trunk\Samples\Sample_Collada\bin\Release\Sample_Collada.exe', Symbols loaded.
'Sample_Collada.exe': Loaded 'C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\mscorjit.dll'
'Sample_Collada.exe': Loaded 'C:\WINDOWS\system32\xpsp2res.dll', Binary was not built with debug information.
'Sample_Collada.exe' (Managed): Loaded 'K:\development\realityFramework\trunk\Samples\Sample_Collada\bin\Release\Framework.dll', Symbols loaded.
'Sample_Collada.exe': Loaded 'C:\WINDOWS\assembly\NativeImages_v2.0.50727_32\System\ba0e3a22211ba7343e0116b051f2965a\System.ni.dll'
'Sample_Collada.exe': Loaded 'C:\WINDOWS\assembly\NativeImages_v2.0.50727_32\System.Drawing\0e83aac37b2623f1a24c70979f31dd56\System.Drawing.ni.dll'
'Sample_Collada.exe': Loaded 'C:\WINDOWS\assembly\NativeImages_v2.0.50727_32\System.Windows.Forms\3d8c79c45aa674e43f075e2e66b8caf5\System.Windows.Forms.ni.dll'
'Sample_Collada.exe' (Managed): Loaded 'C:\WINDOWS\assembly\GAC_MSIL\System.Windows.Forms\2.0.0.0__b77a5c561934e089\System.Windows.Forms.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'Sample_Collada.exe' (Managed): Loaded 'C:\WINDOWS\assembly\GAC_MSIL\System\2.0.0.0__b77a5c561934e089\System.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'Sample_Collada.exe' (Managed): Loaded 'C:\WINDOWS\assembly\GAC_MSIL\System.Drawing\2.0.0.0__b03f5f7f11d50a3a\System.Drawing.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'Sample_Collada.exe': Loaded 'ImageAtBase0x10000000', No symbols loaded.
'Sample_Collada.exe': Unloaded 'ImageAtBase0x10000000'
'Sample_Collada.exe': Loaded 'K:\development\realityFramework\trunk\Samples\Sample_Collada\bin\Release\SlimDX.dll', Binary was not built with debug information.
'Sample_Collada.exe': Loaded 'C:\WINDOWS\system32\xinput1_3.dll'
'Sample_Collada.exe': Loaded 'C:\WINDOWS\system32\setupapi.dll'
'Sample_Collada.exe': Loaded 'C:\WINDOWS\WinSxS\x86_Microsoft.VC80.CRT_1fc8b3b9a1e18e3b_8.0.50727.1433_x-ww_5cf844d2\msvcm80.dll'
'Sample_Collada.exe': Loaded 'C:\WINDOWS\WinSxS\x86_Microsoft.VC80.CRT_1fc8b3b9a1e18e3b_8.0.50727.1433_x-ww_5cf844d2\msvcp80.dll'
'Sample_Collada.exe' (Managed): Loaded 'K:\development\realityFramework\trunk\Samples\Sample_Collada\bin\Release\SlimDX.dll'
'Sample_Collada.exe': Loaded 'K:\development\realityFramework\trunk\Samples\Sample_Collada\bin\Release\fmodex.dll', Binary was not built with debug information.
'Sample_Collada.exe': Loaded 'C:\WINDOWS\system32\msacm32.dll'
'Sample_Collada.exe': Loaded 'C:\WINDOWS\system32\winmm.dll'
'Sample_Collada.exe': Loaded 'C:\WINDOWS\system32\wsock32.dll'
'Sample_Collada.exe': Loaded 'C:\WINDOWS\system32\ws2_32.dll'
'Sample_Collada.exe': Loaded 'C:\WINDOWS\system32\ws2help.dll'
'Sample_Collada.exe': Loaded 'C:\WINDOWS\system32\wdmaud.drv'
'Sample_Collada.exe': Loaded 'C:\WINDOWS\system32\wintrust.dll'
'Sample_Collada.exe': Loaded 'C:\WINDOWS\system32\crypt32.dll'
'Sample_Collada.exe': Loaded 'C:\WINDOWS\system32\msasn1.dll'
'Sample_Collada.exe': Loaded 'C:\WINDOWS\system32\imagehlp.dll'
'Sample_Collada.exe': Unloaded 'C:\WINDOWS\system32\wdmaud.drv'
'Sample_Collada.exe': Loaded 'C:\WINDOWS\system32\wdmaud.drv'
'Sample_Collada.exe': Loaded 'C:\WINDOWS\system32\msacm32.drv'
'Sample_Collada.exe': Loaded 'C:\WINDOWS\system32\midimap.dll'
'Sample_Collada.exe': Loaded 'C:\WINDOWS\system32\dsound.dll'
'Sample_Collada.exe': Loaded 'C:\WINDOWS\system32\version.dll'
'Sample_Collada.exe': Loaded 'C:\WINDOWS\system32\ksuser.dll'
The thread 'Win32 Thread' (0xf30) has exited with code 0 (0x0).
The thread 'Win32 Thread' (0x828) has exited with code 0 (0x0).
'Sample_Collada.exe': Loaded 'C:\WINDOWS\WinSxS\x86_Microsoft.Windows.GdiPlus_6595b64144ccf1df_1.0.2600.2180_x-ww_522f9f82\GdiPlus.dll'
'Sample_Collada.exe': Loaded 'C:\WINDOWS\system32\MSCTFIME.IME'
'Sample_Collada.exe': Loaded 'D:\docks\YzDock.dll', Binary was not built with debug information.
'Sample_Collada.exe': Loaded 'C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\diasymreader.dll'
'Sample_Collada.exe': Loaded 'C:\WINDOWS\system32\rsaenh.dll'
'Sample_Collada.exe' (Managed): Loaded 'C:\WINDOWS\WinSxS\x86_Microsoft.VC80.CRT_1fc8b3b9a1e18e3b_8.0.50727.1433_x-ww_5cf844d2\msvcm80.dll'
'Sample_Collada.exe' (Managed): Loaded 'K:\development\realityFramework\trunk\Samples\Sample_Collada\bin\Release\dx9_renderer.dll', Symbols loaded.
'Sample_Collada.exe': Loaded 'C:\WINDOWS\system32\dinput8.dll'
'Sample_Collada.exe': Loaded 'C:\WINDOWS\system32\hid.dll'
'Sample_Collada.exe': Loaded 'C:\WINDOWS\system32\d3d9.dll'
'Sample_Collada.exe': Loaded 'C:\WINDOWS\system32\d3d8thk.dll'
'Sample_Collada.exe': Loaded 'C:\WINDOWS\system32\d3d9d.dll'
'Sample_Collada.exe': Loaded 'C:\WINDOWS\system32\d3dx9d_33.dll'
Direct3D9: (INFO) :Direct3D9 Debug Runtime selected.
'Sample_Collada.exe': Loaded 'C:\WINDOWS\system32\D3DX9_37.dll'
Direct3D9: (INFO) :======================= Hal HWVP Pure device selected

Direct3D9: (INFO) :HalDevice Driver Style 9

'Sample_Collada.exe' (Managed): Loaded 'K:\development\realityFramework\trunk\Samples\Sample_Collada\bin\Release\ICSharpCode.SharpZipLib.dll'
'Sample_Collada.exe': Loaded 'C:\WINDOWS\system32\usp10.dll'
'Sample_Collada.exe' (Managed): Loaded 'K:\development\realityFramework\trunk\Samples\Sample_Collada\bin\Release\COLLADADocument.dll'
'Sample_Collada.exe': Loaded 'C:\WINDOWS\assembly\NativeImages_v2.0.50727_32\System.Xml\c98cb65a79cfccb44ea727ebe4593ede\System.Xml.ni.dll'
'Sample_Collada.exe' (Managed): Loaded 'C:\WINDOWS\assembly\GAC_MSIL\System.Xml\2.0.0.0__b77a5c561934e089\System.Xml.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
Direct3D9: (WARN) :Vertexbuffer created with POOL_DEFAULT but WRITEONLY not set. Performance penalty could be severe.
Direct3D9: (WARN) :Indexbuffer created with POOL_DEFAULT but WRITEONLY not set. Performance penalty could be severe.
Direct3D9: (WARN) :Indexbuffer created with POOL_DEFAULT but WRITEONLY not set. Performance penalty could be severe.
Direct3D9: (WARN) :Indexbuffer created with POOL_DEFAULT but WRITEONLY not set. Performance penalty could be severe.
Direct3D9: (WARN) :Indexbuffer created with POOL_DEFAULT but WRITEONLY not set. Performance penalty could be severe.

'Sample_Collada.exe': Loaded 'C:\WINDOWS\assembly\NativeImages_v2.0.50727_32\System.Configuration\eee9b48577689e92db5a7b5c5de98d9b\System.Configuration.ni.dll'
'Sample_Collada.exe' (Managed): Loaded 'C:\WINDOWS\assembly\GAC_MSIL\System.Configuration\2.0.0.0__b03f5f7f11d50a3a\System.Configuration.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'Sample_Collada.exe' (Managed): Loaded 'C:\WINDOWS\assembly\GAC_MSIL\Microsoft.VisualStudio.Debugger.Runtime\9.0.0.0__b03f5f7f11d50a3a\Microsoft.VisualStudio.Debugger.Runtime.dll'
'Sample_Collada.exe': Loaded 'C:\Program Files\Microsoft Visual Studio 9.0\Common7\Packages\Debugger\x86\Microsoft.VisualStudio.Debugger.Runtime.Impl.dll'
'Sample_Collada.exe': Loaded 'C:\WINDOWS\WinSxS\x86_Microsoft.VC90.CRT_1fc8b3b9a1e18e3b_9.0.21022.8_x-ww_d08d0375\msvcr90.dll', Symbols loaded.
Direct3D9: (WARN) :Stream 0 stride and vertex size, computed from the current vertex declaration or FVF, are different, which might not work with pre-DX8 drivers




i also changed the shader code a bit


struct VertexToPixel
{
float4 Position : POSITION;
float3 Normal : NORMAL;
float2 TexCoords : TEXCOORD0;
float3 Tangent : TANGENT;
float3 Binormal : BINORMAL;


};

struct PixelToFrame
{
float4 Color : COLOR0;
};

float4x4 xWorldViewProjection;



Texture xColoredTexture;

sampler ColoredTextureSampler = sampler_state { texture = <xColoredTexture> ; magfilter = LINEAR; minfilter = LINEAR; mipfilter=LINEAR;AddressU = wrap; AddressV = wrap;};


VertexToPixel SimplestVertexShader( float4 inPos : POSITION, float3 inNormal : NORMAL,
float2 inTexCoords : TEXCOORD0, float3 inTangent : TANGENT, float3 inBinormal : BINORMAL)


{
VertexToPixel Output = (VertexToPixel)0;

Output.Position = mul(inPos, xWorldViewProjection);

Output.TexCoords = inTexCoords;

return Output;
}

PixelToFrame OurFirstPixelShader(VertexToPixel PSIn)
{
PixelToFrame Output = (PixelToFrame)0;



Output.Color = tex2D(ColoredTextureSampler, PSIn.TexCoords);


return Output;
}

technique Simplest
{
pass Pass0
{
VertexShader = compile vs_3_0 SimplestVertexShader();
PixelShader = compile ps_3_0 OurFirstPixelShader();
}
}




can it be happening that if i do this: List.toArray() that properties are also made to array?, which means, the stride would not be right?

Share this post


Link to post
Share on other sites
Slaaitjuh    130
okay guys i fixed the problem, after finding out this matching error. The problem was in the FVF declaration, where PositionW instead of Position should have been used. Thanks a lot for your help!

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