Author |
Topic: D3D11 Pixel Shader (Read 151 times) |
|
Ric
New Member
member is offline


Posts: 30
|
 |
D3D11 Pixel Shader
« Thread started on: Dec 23rd, 2017, 9:10pm » |
|
Code:cbuffer ConstantBuffer : register( b0 ){ matrix World; matrix View; matrix Projection; float4 vLightDir; float4 vLightColor; float4 ambient; //float4 vOutputColor;}//--------------------------------------------------------------------------------------struct VS_INPUT{ float4 Pos : POSITION; float3 Norm : NORMAL; float4 Color : COLOR;};struct PS_INPUT{ float4 Pos : SV_POSITION; float3 Norm : TEXCOORD0; float4 Color : COLOR;};//--------------------------------------------------------------------------------------// Vertex Shader//--------------------------------------------------------------------------------------PS_INPUT VS( VS_INPUT input ){ float4 normal; float4 diffuseColor = 0; PS_INPUT output = (PS_INPUT)0; output.Pos = mul( input.Pos, World ); output.Pos = mul( output.Pos, View ); output.Pos = mul( output.Pos, Projection ); normal = mul(input.Norm, World); diffuseColor += dot((float3)vLightDir, normal); diffuseColor += 1; diffuseColor /= 2; diffuseColor * (1 - ambient); diffuseColor *= input.Color; diffuseColor += ambient; output.Color = diffuseColor * vLightColor; return output;}//--------------------------------------------------------------------------------------// Pixel Shader//--------------------------------------------------------------------------------------float4 PS( PS_INPUT input) : SV_Target{ return input.Color;}
I have managed to convert the vertex shader to sensible light effect, but no matter what I do the pixel shader returns a fault. I woud like to be able to use color.alpha to affect the other 3 components (why are they x,y,x,w and not r,g,b,a?), if I use input.color.w, or just input.color it fails, WHY?
I have modified the simple vertex to Code: DIM SimpleVertex{Pos{}=XMFLOAT3{},Normal{}=XMFLOAT3{},Color{}=XMFLOAT4{}} DIM layout{(2)} = D3D11_INPUT_ELEMENT_DESC{} sn0$ = "POSITION" + CHR$(0) layout{(0)}.SemanticName% = !^sn0$ layout{(0)}.Format% = DXGI_FORMAT_R32G32B32_FLOAT layout{(0)}.AlignedByteOffset% = 0 layout{(0)}.InputSlotClass% = D3D11_INPUT_PER_VERTEX_DATA sn1$ = "NORMAL" + CHR$(0) layout{(1)}.SemanticName% = !^sn1$ layout{(1)}.Format% = DXGI_FORMAT_R32G32B32_FLOAT layout{(1)}.AlignedByteOffset% = 12 layout{(1)}.InputSlotClass% = D3D11_INPUT_PER_VERTEX_DATA sn2$ = "COLOR" + CHR$(0) layout{(2)}.SemanticName% = !^sn2$ layout{(2)}.Format% = DXGI_FORMAT_R32G32B32A32_FLOAT layout{(2)}.AlignedByteOffset% = 24 layout{(2)}.InputSlotClass% = D3D11_INPUT_PER_VERTEX_DATA numElements% = DIM(layout{()},1) + 1
Can anybody help?
Ric
|
|
Logged
|
You can't succeed without failing a few times first, CHIN UP.
|
|
|
Richard Russell
Administrator
member is offline


Posts: 803
|
 |
Re: D3D11 Pixel Shader
« Reply #1 on: Dec 23rd, 2017, 9:52pm » |
|
on Dec 23rd, 2017, 9:10pm, Ric wrote: Let's take one step back. Why are you wanting to use D3D11 shaders? Are the effects you are trying to achieve not possible using the fixed rendering pipeline (e.g. in D3D9)? I rather doubt that anybody at this forum has experience of writing HLSL code, I certainly don't.
If you do particularly want to use D3D11 shaders have you considered asking your question at the BB4W forum? One of the leading members of that forum - Michael Hutton - is the only person I know who has written significant amounts of HLSL code for use in BBC BASIC programs.
Richard.
|
|
Logged
|
|
|
|
Ric
New Member
member is offline


Posts: 30
|
 |
Re: D3D11 Pixel Shader
« Reply #2 on: Dec 23rd, 2017, 9:59pm » |
|
Thank you Richard, Ill ask Michael. Ric
|
|
Logged
|
You can't succeed without failing a few times first, CHIN UP.
|
|
|
Richard Russell
Administrator
member is offline


Posts: 803
|
 |
Re: D3D11 Pixel Shader
« Reply #3 on: Dec 23rd, 2017, 10:45pm » |
|
on Dec 23rd, 2017, 9:59pm, Ric wrote:| Thank you Richard, Ill ask Michael. |
|
One comment I would make, ignoring the shader aspects and just looking at your calculations, is that your matrix dimensions look a bit strange to me. For example you have this:
Code:diffuseColor += dot((float3)vLightDir, normal); According to the declarations in your code, 'normal' is a float4 and you are casting 'vLightDir' to a float3, but - unless HLSL arithmetic is very different from conventional arithmetic - you can't perform a dot-product between two vectors of different sizes!
Here's another:
Code:output.Pos = mul(input.Pos, World);...normal = mul(input.Norm, World); The first of these two lines multiplies 'input.Pos', a float4, with 'World' and the second multiplies 'input.Norm', a float3, with 'World'. Again it may be because HLSL arithmetic is non-standard, but superficially that ought not to be possible (what dimensions would 'World' have?).
If your version is a cut-and-paste from known working code then obviously I must be wrong, but it sure looks odd!
You could try prototyping that code in BBC BASIC - it supports matrix arithmetic as you know - to check that it runs and produces the results you expect.
Richard.
|
|
Logged
|
|
|
|
Ric
New Member
member is offline


Posts: 30
|
 |
Re: D3D11 Pixel Shader
« Reply #4 on: Dec 24th, 2017, 10:02am » |
|
I agree it looks odd but it works just like it should in the vertex shader and produces the correct results. Must have automatic clipping etc...
Ric
|
|
Logged
|
You can't succeed without failing a few times first, CHIN UP.
|
|
|
|