private void UpdateConstantBuffer(ConstantBufferCPU data) { deviceContext.UpdateSubresource(ref data, constantBuffer); deviceContext.VertexShader.SetConstantBuffer(0, constantBuffer); deviceContext.PixelShader.SetConstantBuffer(0, constantBuffer); }
private void InitializeShaders() { ConstantBufferCPU data = new ConstantBufferCPU(); data.worldViewProjectionMatrix = Matrix.Identity; data.time = 0.0f; data.padding = Vector3.Zero; constantBuffer = D3D11.Buffer.Create(device, D3D11.BindFlags.ConstantBuffer, ref data); string textVS = System.IO.File.ReadAllText("Shaders\\MainVS.hlsl"); using (var vertexShaderByteCode = ShaderBytecode.Compile(textVS, "main", "vs_4_0", ShaderFlags.Debug | ShaderFlags.WarningsAreErrors, EffectFlags.None, null, new StreamInclude(), sourceFileName: "Shaders\\MainVS.hlsl")) { if (vertexShaderByteCode.Bytecode == null) { MessageBox.Show(vertexShaderByteCode.Message, "Shader Compilation Error", MessageBoxButtons.OK, MessageBoxIcon.Error); System.Environment.Exit(-1); } vertexShader = new D3D11.VertexShader(device, vertexShaderByteCode); using (var inputSignature = ShaderSignature.GetInputSignature(vertexShaderByteCode)) inputLayout = new D3D11.InputLayout(device, inputSignature, inputElements); } string textPS = System.IO.File.ReadAllText("Shaders\\MainPS.hlsl"); using (var pixelShaderByteCode = ShaderBytecode.Compile(textPS, "main", "ps_4_0", ShaderFlags.Debug | ShaderFlags.WarningsAreErrors, EffectFlags.None, null, new StreamInclude(), sourceFileName: "Shaders\\MainPS.hlsl")) { if (pixelShaderByteCode.Bytecode == null) { MessageBox.Show(pixelShaderByteCode.Message, "Shader Compilation Error", MessageBoxButtons.OK, MessageBoxIcon.Error); System.Environment.Exit(-1); } pixelShader = new D3D11.PixelShader(device, pixelShaderByteCode); } }
public void Render() { Viewport viewport = new Viewport(0, 0, width, height); #if RENDER_CUBE ConstantBufferCPU data = GetConstantBufferForCube(); #else ConstantBufferCPU data = GetConstantBufferForTriangle(); #endif int vertexSize = Utilities.SizeOf <Vector3>(); int vertexCount = trianglePositionVertexBuffer.Description.SizeInBytes / vertexSize; if (mode == RenderMode.RenderModeHardware) { deviceContext.InputAssembler.SetVertexBuffers(0, new D3D11.VertexBufferBinding(trianglePositionVertexBuffer, vertexSize, 0)); deviceContext.InputAssembler.SetVertexBuffers(1, new D3D11.VertexBufferBinding(triangleColorVertexBuffer, vertexSize, 0)); deviceContext.InputAssembler.InputLayout = inputLayout; deviceContext.InputAssembler.PrimitiveTopology = SharpDX.Direct3D.PrimitiveTopology.TriangleList; deviceContext.VertexShader.Set(vertexShader); deviceContext.PixelShader.Set(pixelShader); deviceContext.Rasterizer.SetViewport(viewport); deviceContext.Rasterizer.State = rasterizerState; deviceContext.OutputMerger.SetDepthStencilState(depthStencilState, 1); deviceContext.OutputMerger.SetRenderTargets(depthDSV, backbufferRTV); //alpha blending deviceContext.OutputMerger.SetBlendState(blendState, new SharpDX.Color4(0.0f, 0.0f, 0.0f, 0.0f), 0xFFFFFFFF); deviceContext.ClearDepthStencilView(depthDSV, D3D11.DepthStencilClearFlags.Depth, 1.0f, 0); deviceContext.ClearRenderTargetView(backbufferRTV, new SharpDX.Color(32, 103, 178)); } else { softwareRasterizer.Clear(new Color(32, 103, 178)); } if (mode == RenderMode.RenderModeHardware) { UpdateConstantBuffer(data); #if RENDER_CUBE int indexCount = triangleIndexBuffer.Description.SizeInBytes / Utilities.SizeOf <int>(); deviceContext.InputAssembler.SetIndexBuffer(triangleIndexBuffer, DXGI.Format.R32_UInt, 0); deviceContext.DrawIndexed(indexCount, 0, 0); #else deviceContext.Draw(vertexCount, 0); #endif } #if MULTIPLE_PYRAMIDS for (int i = 0; i < 5; i++) { if (mode == RenderMode.RenderModeHardware) { data.padding = new Vector3(0.5f * -i, 0.5f * -i, 0.5f * -i); data.alpha.W = 0.1f * i; UpdateConstantBuffer(data); int indexCount = triangleIndexBuffer.Description.SizeInBytes / Utilities.SizeOf <int>(); deviceContext.InputAssembler.SetIndexBuffer(triangleIndexBuffer, DXGI.Format.R32_UInt, 0); deviceContext.DrawIndexed(indexCount, 0, 0); } } #endif }