Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Here is a reference sheet I wrote back when I was first learning how to use HLSL shaders in DirectX9. Perhaps it will be of assistance.</p> <p><strong>IN THE APPLICATION:<br></strong> Declare needed variables:</p> <pre><code>ID3DXEffect* shader; </code></pre> <p><br> Load the .fx file:</p> <pre><code>D3DXCreateEffectFromFile( d3dDevice, _T("filepath.fx"), 0, 0, 0, 0, &amp;shader, 0 ); </code></pre> <p><br> Clean up the effect object (some people use a SAFE_RELEASE macro):</p> <pre><code>if(shader) shader-&gt;Release(); shader = nullptr; </code></pre> <p><br> Use the shader to render something:</p> <pre><code>void Application::Render() { unsigned passes = 0; shader-&gt;Begin(&amp;passes,0); for(unsigned i=0;i&lt;passes;++i) { shader-&gt;BeginPass(i); // Set uniforms shader-&gt;SetMatrix("gWorld",&amp;theMatrix); shader-&gt;CommitChanges(); // Not necessary if SetWhatevers are done OUTSIDE of a BeginPass/EndPass pair. /* Insert rendering instructions here */ // BEGIN EXAMPLE: d3dDevice-&gt;SetVertexDeclaration(vertexDecl); d3dDevice-&gt;SetStreamSource(0,vertexBuffer,0,sizeof(VERT)); d3dDevice-&gt;SetIndices(indexBuffer); d3dDevice-&gt;DrawIndexedPrimitive(D3DPT_TRIANGLELIST,0,0,numVerts,0,8); // END EXAMPLE shader-&gt;EndPass(); } shader-&gt;End(); } </code></pre> <p><br> <strong>IN THE .FX FILE:<br></strong> Declare the uniforms (variables you want to set from within the application):</p> <pre><code>float4x4 gWorld : WORLD; float4x4 gViewProj : VIEWPROJECTION; float gTime : TIME; Texture2D gDiffuseTexture; // requires a matching sampler sampler gDiffuseSampler = sampler_state // here's the matching sampler { Texture = &lt;gDiffuseTexture&gt;; FILTER = MIN_MAG_MIP_LINEAR; AddressU = Wrap; AddressV = Wrap; }; </code></pre> <p><br> Define the vertex shader input structure:</p> <pre><code>struct VS_INPUT // make this match the vertex structure in Application { float3 untransformed_pos : POSITION0; float3 untransformed_nrm : NORMAL0; float4 color : COLOR0; float2 uv_coords : TEXCOORD0; }; </code></pre> <p><br> Define the pixel shader input structure (vertex shader output):</p> <pre><code>struct PS_INPUT { float4 transformed_pos : POSITION0; float4 transformed_nrm : NORMAL0; float4 color : COLOR0; float2 uv_coords : TEXCOORD0; }; </code></pre> <p><br> Define a vertex shader:</p> <pre><code>PS_INPUT mainVS (VS_INPUT input) { PS_INPUT output; /* Insert shader instructions here */ return output; } </code></pre> <p><br> Define a pixel shader:</p> <pre><code>float4 mainPS (PS_INPUT input) : COLOR { /* Insert shader instructions here */ return float4(resultColor,1.0f); } </code></pre> <p><br> Define a technique:</p> <pre><code>technique myTechnique { // Here is a quick sample pass FirstPass { vertexShader = compile vs_3_0 mainVS(); pixelShader = compile ps_3_0 mainPS(); // Setting a few of the many D3D renderstates via the effect framework ShadeMode = FLAT; // flat color interpolation across triangles FillMode = SOLID; // no wireframes, no point drawing. CullMode = CCW; // cull any counter-clockwise polygons. } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload