c++ - What is the correct way to set global parameters in hlsl shader? -



c++ - What is the correct way to set global parameters in hlsl shader? -

what right way of setting global params in hlsl shader? if have next global params:

float4x4 world; float4x4 view; float4x4 projection;

and utilize them within vertex shader:

void vertexshaderfunction( in float4 inputposition : position, in float4 colorin : color, out float4 posout : sv_position, out float4 colorout : colour) { //set values output float4 worldposition = mul(inputposition, world); float4 viewposition = mul(worldposition, view); float4 position = mul(viewposition, projection); posout = position; colorout = colorin; }

then how set these global values c++ code f.e. when photographic camera moved? should create shader, sets these values can access buffer this?

void setprojectionmatrix(float4x4 inputmatrix : matrix){ projection = inputmatrix; }

please tell me proper way accomplish this.

first, in shader want set matrices constant buffer:

cbuffer camerabuffer : register( b0 ) { float4x4 world; float4x4 view; float4x4 projection; }

if don't declare constant buffer, created you, much improve declare them explicitly , grouping them frequency of update. example, grouping constants updated per frame , constants set 1 time together. allows update constants need update without sending info gpu.

even though within cbuffer structure, still accessed same way within shader.

in c++ code want declare similar construction store matrices:

struct cameraconstants { xmfloat4x4 world; xmfloat4x4 view; xmfloat4x4 projection; };

it's of import take care packing rules constant variables. construction have no problems, in cases may need add together padding c++ structures business relationship fact shader cbuffers pack info not cross 16 byte boundaries due 16 byte nature of gpu registers.

during initialization, you'll need create constant buffer. process same vertex buffer except you'll need utilize next flags declare constant buffer writable cpu:

cbdesc.usage = d3d11_usage_dynamic; cbdesc.bindflags = d3d11_bind_constant_buffer; cbdesc.cpuaccessflags = d3d11_cpu_access_write;

whenever update matrices, need upload them gpu. this, map constant buffer cpu, , re-create on cameraconstants structure:

d3d11_mapped_subresource resource; m_devicecontext->map( cameracbuffer, 0, d3d11_map_write_discard, 0, &resource ); memcpy( resource.pdata, cameraconstants, sizeof( cameraconstants ) ); m_devicecontext->unmap( cameracbuffer, 0 );

now bind constant buffer vertex shader:

m_devicecontext->vssetconstantbuffers( 0, 1, &cameracbuffer );

note first parameter maps register used in shader cbuffer declaration (b0 in case).

one more thing, matrices in hlsl column major default. if matrices row major in c++ (probably), need either transpose them before sending gpu or declare matrices row_major in shader.

check out directx samples source code of this: https://code.msdn.microsoft.com/windowsdesktop/direct3d-tutorial-win32-829979ef

c++ 3d hlsl direct3d11

Comments

Popular posts from this blog

xslt - DocBook 5 to PDF transform failing with error: "fo:flow" is missing child elements. Required content model: marker* -

mediawiki - How do I insert tables inside infoboxes on Wikia pages? -

Local Service User Logged into Windows -