BBC BASIC
« D3D11 Pixel Shader »

Welcome Guest. Please Login or Register.
Jan 22nd, 2018, 5:12pm


Cross-platform BBC BASIC (Win32, Linux x86, Android, Mac OS-X, Raspberry Pi)

« Previous Topic | Next Topic »
Pages: 1  Notify Send Topic Print
 thread  Author  Topic: D3D11 Pixel Shader  (Read 49 times)
Ric
New Member
Image


member is offline

Avatar




PM


Posts: 21
xx 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
User IP Logged

You can't succeed without failing a few times first, CHIN UP.
Richard Russell
Administrator
ImageImageImageImageImage


member is offline

Avatar




Homepage PM


Posts: 691
xx Re: D3D11 Pixel Shader
« Reply #1 on: Dec 23rd, 2017, 9:52pm »

on Dec 23rd, 2017, 9:10pm, Ric wrote:
Can anybody help?

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.
User IP Logged

Ric
New Member
Image


member is offline

Avatar




PM


Posts: 21
xx Re: D3D11 Pixel Shader
« Reply #2 on: Dec 23rd, 2017, 9:59pm »

Thank you Richard, Ill ask Michael.
Ric
User IP Logged

You can't succeed without failing a few times first, CHIN UP.
Richard Russell
Administrator
ImageImageImageImageImage


member is offline

Avatar




Homepage PM


Posts: 691
xx 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.
User IP Logged

Ric
New Member
Image


member is offline

Avatar




PM


Posts: 21
xx 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
User IP Logged

You can't succeed without failing a few times first, CHIN UP.
Pages: 1  Notify Send Topic Print
« Previous Topic | Next Topic »

Donate $6.99 for 50,000 Ad-Free Pageviews!


This forum powered for FREE by Conforums ©
Sign up for your own Free Message Board today!
Terms of Service | Privacy Policy | Conforums Support | Parental Controls