pouët.net

Go to bottom

Passing a float from c++ to the pixel shader

category: general [glöplog]
 
Hej,

iam programming my first pixel shader, but how can i pass data to the hlsl code?

Code: // hlsl pixel shader struct PS_IN { float4 pos : SV_POSITION; //float4 k : LOLIMASEMANTIC; //float4 color : COLOR; }; struct PS_OUT { float4 color : COLOR; }; PS_OUT SimplePixelShader(PS_IN input) : SV_Target { PS_OUT output; output.color.g = 0.0f; output.color.r = input.pos.r/1280; output.color.b = input.pos.g/720; return output; }


Displays a nice color an the screen, but how can i use Data from the c++ side?

How can i pass the
DWORD currentTime = timeGetTime();

currentTime to the pixel shader?

Thaaanx
added on the 2015-06-26 12:10:03 by nebulus nebulus
added on the 2015-06-26 12:41:32 by Gargaj Gargaj
Iam on DX11
added on the 2015-06-26 13:25:57 by nebulus nebulus
added on the 2015-06-26 13:30:23 by Gargaj Gargaj
HLSL:
DX_Effect->SetFloat("t", time);
You can pass in whatever you want, it´s easy once you know how to, but always remember: passing out ain´t possible, except you write your data to some Texture! ;) (isPossible == true)
Declare a constant buffer in your hlsl code: (you can have an implicit default constant buffer if you want for size reasons, but..)
Code: cbuffer mybuffer{ int time; }


create a constant buffer and bind it to the pixel shader:
Code: D3D11_BUFFER_DESC ConstBufferDesc = {16, D3D11_USAGE_DEFAULT,D3D11_BIND_CONSTANT_BUFFER,0,0,0}; pDevice->CreateBuffer ( &ConstBufferDesc, NULL, &pConstants); pImmediateContext->PSSetConstantBuffers( 0, 1, &pConstants );


Update the buffer data every frame:
Code: int time = timeGetTime(); pImmediateContext->UpdateSubresource(pConstants,0,0,&time ,4,4);



hardy: effect is so dx9 ;)
added on the 2015-06-26 16:31:51 by Psycho Psycho
Psycho: i love you and all you do!
But why the overhead size of a struct for a simple "field","member","variable" ?
Didn't want to go all out dirty 1k style ;)

But if you just declare variables in the global scope they will actually form an implicit constant buffer (I think it's only there if no explicit buffer is declared). Of course it then becomes harder to figure out the byte offsets for the variables in that case.
The above code (even without the cbuffer{}) with a single value is more overhead than dx9/ogl, but with more variables it's nice just moving a chunk of data instead of setting a bunch of different kinds.
added on the 2015-06-26 19:31:32 by Psycho Psycho
memorywize 32bit is still just like 0.5 times amount of data...why do i still use dx9? ;)
i did global illumination in a directx9-shader, so what for going up at all? tessellation? noone needs this feat, concerning intros! :p

login

Go to top