这里不再介绍effect框架的具体使用,有关effect框架使用可参考
实现的功能依然是画一个简单的三角形,只不过使用了effect框架。
为了体现使用effect框架方便变量绑定的优点,我们对着色器代码做了修改,增加了一个常量float4x4 gWorldViewProj
cbuffer cbPerObject{ float4x4 gWorldViewProj; };float4 VS_Main( float4 pos : POSITION ) : SV_POSITION { pos = mul(pos,gWorldViewProj); return pos; } float4 PS_Main( float4 pos : SV_POSITION ) : SV_TARGET { return float4( 1.0f, 0.0f, 0.0f, 1.0f ); } technique11 ColorTech{ pass P0 { SetVertexShader( CompileShader( vs_5_0, VS_Main() ) ); SetGeometryShader( NULL ); SetPixelShader( CompileShader( ps_5_0, PS_Main() ) ); }}
由于代码很简单,这里不再给出解释,直接看代码吧!
下面是修改后的TriangleDemo.h
1 #pragma once 2 3 #include "Dx11DemoBase.h" 4 #include "d3dx11effect.h" 5 6 class TriangleDemo : public Dx11DemoBase 7 { 8 public: 9 TriangleDemo();10 ~TriangleDemo();11 12 bool LoadContent() override;13 void UnLoadContent() override;14 15 void Update(float dt) override;16 void Render() override;17 18 private:19 ID3D11Buffer *m_pVertexBuffer;20 ID3D11InputLayout *m_pInputLayout;21 22 ID3DX11Effect *m_pFx;23 ID3DX11EffectTechnique *m_pTechnique;24 ID3DX11EffectMatrixVariable *m_pFxWorldViewProj;25 XMFLOAT4X4 m_world;26 XMFLOAT4X4 m_view;27 XMFLOAT4X4 m_proj;28 29 };
TriangleDemo.cpp
1 #include "TriangleDemo.h" 2 3 4 struct VertexPos 5 { 6 XMFLOAT3 pos; 7 }; 8 9 TriangleDemo::TriangleDemo() 10 : m_pInputLayout(0), m_pVertexBuffer(0) 11 { 12 XMMATRIX I = XMMatrixIdentity(); 13 XMStoreFloat4x4(&m_world, I); 14 XMStoreFloat4x4(&m_view, I); 15 XMStoreFloat4x4(&m_proj, I); 16 } 17 18 19 TriangleDemo::~TriangleDemo() 20 { 21 } 22 23 void TriangleDemo::UnLoadContent() 24 { 25 if (m_pVertexBuffer) 26 m_pVertexBuffer->Release(); 27 if (m_pInputLayout) 28 m_pInputLayout->Release(); 29 if (m_pFx) 30 m_pFx->Release(); 31 m_pVertexBuffer = 0; 32 m_pInputLayout = 0; 33 m_pFx = 0; 34 } 35 36 bool TriangleDemo::LoadContent() 37 { 38 HRESULT result; 39 VertexPos vertices[] = 40 { 41 XMFLOAT3(0.5f, 0.5f, 0.5f), 42 XMFLOAT3(0.5f, -0.5f, 0.5f), 43 XMFLOAT3(-0.5f, -0.5f, 0.5f) 44 }; 45 46 D3D11_BUFFER_DESC vertexDesc; 47 ZeroMemory(&vertexDesc, sizeof(vertexDesc)); 48 vertexDesc.Usage = D3D11_USAGE_DEFAULT; 49 vertexDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER; 50 vertexDesc.ByteWidth = sizeof(VertexPos)* 3; 51 52 D3D11_SUBRESOURCE_DATA resourceData; 53 ZeroMemory(&resourceData, sizeof(resourceData)); 54 resourceData.pSysMem = vertices; 55 56 result = m_pd3dDevice->CreateBuffer(&vertexDesc, &resourceData, &m_pVertexBuffer); 57 if (FAILED(result)) 58 { 59 return false; 60 } 61 62 63 DWORD shaderFlags = D3DCOMPILE_ENABLE_STRICTNESS; 64 #if defined _DEBUG || defined DEBUG 65 shaderFlags = D3DCOMPILE_DEBUG; 66 #endif 67 68 ID3D10Blob *compiledShader = 0; 69 ID3D10Blob *compilationMsgs = 0; 70 result = D3DX11CompileFromFile("SolidColor.fx", 0, 0, 0, "fx_5_0", shaderFlags, 71 0, 0, &compiledShader, &compilationMsgs, 0); 72 if (compilationMsgs != 0) 73 { 74 MessageBox(0, (char*)compilationMsgs->GetBufferPointer(), 0, 0); 75 compilationMsgs->Release(); 76 compilationMsgs = 0; 77 } 78 if (FAILED(result)) 79 { 80 MessageBox(0, "载入着色器错误", 0, 0); 81 return false; 82 } 83 84 result = D3DX11CreateEffectFromMemory(compiledShader->GetBufferPointer(), compiledShader->GetBufferSize(), 85 0, m_pd3dDevice, &m_pFx); 86 compiledShader->Release(); 87 if (FAILED(result)) 88 { 89 MessageBox(0, "载入着色器失败", 0, 0); 90 return false; 91 } 92 m_pTechnique = m_pFx->GetTechniqueByName("ColorTech"); 93 m_pFxWorldViewProj = m_pFx->GetVariableByName("gWorldViewProj")->AsMatrix(); 94 95 D3D11_INPUT_ELEMENT_DESC solidColorLayout[] = 96 { 97 { "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 } 98 }; 99 UINT numLayoutElements = ARRAYSIZE(solidColorLayout);100 D3DX11_PASS_DESC passDesc;101 m_pTechnique->GetPassByIndex(0)->GetDesc(&passDesc);102 103 result = m_pd3dDevice->CreateInputLayout(solidColorLayout, numLayoutElements, passDesc.pIAInputSignature,104 passDesc.IAInputSignatureSize, &m_pInputLayout);105 106 return true;107 }108 109 110 111 void TriangleDemo::Update(float dt)112 {113 114 }115 116 void TriangleDemo::Render()117 {118 if (m_pImmediateContext == 0)119 return;120 //清除渲染目标视图121 float clearColor[4] = { 0.5f, 0.5f, 0.5f, 1.0f };//背景颜色122 m_pImmediateContext->ClearRenderTargetView(m_pRenderTargetView, clearColor);123 124 UINT stride = sizeof(VertexPos);125 UINT offset = 0;126 //设置数据信息格式控制信息127 m_pImmediateContext->IASetInputLayout(m_pInputLayout);128 //设置要绘制的几何体信息129 m_pImmediateContext->IASetVertexBuffers(0,1,&m_pVertexBuffer,&stride,&offset);130 //指明如何绘制三角形131 m_pImmediateContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);132 133 //设置常量 134 XMMATRIX world = XMLoadFloat4x4(&m_world);135 XMMATRIX view = XMLoadFloat4x4(&m_view);136 XMMATRIX proj = XMLoadFloat4x4(&m_proj);137 XMMATRIX worldViewProj = world*view*proj;138 m_pFxWorldViewProj->SetMatrix(reinterpret_cast(&worldViewProj));139 140 D3DX11_TECHNIQUE_DESC techDesc;141 m_pTechnique->GetDesc(&techDesc);142 for (UINT i = 0; i < techDesc.Passes; ++i)143 {144 m_pTechnique->GetPassByIndex(i)->Apply(0, m_pImmediateContext);145 m_pImmediateContext->Draw(3, 0);146 }147 //马上输出148 m_pSwapChain->Present(0, 0);149 }
运行结果同之前的一样,不再贴图了