示例#1
0
        private bool SetShaderParameters(DeviceContext deviceContext, Matrix worldMatrix, Matrix viewMatrix, Matrix projectionMatrix, ShaderResourceView[] textures, float fogStart, float fogEnd)
        {
            try
            {
                #region Set Per Frame Shader Resources
                // Transpose the matrices to prepare them for shader.
                worldMatrix.Transpose();
                viewMatrix.Transpose();
                projectionMatrix.Transpose();

                // Lock the constant buffer so it can be written to.
                DataStream mappedResource;
                deviceContext.MapSubresource(ConstantPerFrameBuffer, MapMode.WriteDiscard, SharpDX.Direct3D11.MapFlags.None, out mappedResource);

                // Copy the matrices into the constant buffer.
                var matrixBuffer = new PerFrameBuffer()
                {
                    world = worldMatrix,
                    view = viewMatrix,
                    projection = projectionMatrix
                };

                mappedResource.Write(matrixBuffer);

                // Unlock the constant buffer.
                deviceContext.UnmapSubresource(ConstantPerFrameBuffer, 0);

                // Set the position of the constant buffer in the vertex shader.
                var bufferNumber = 0;

                // Finally set the constant buffer in the vertex shader with the updated values.
                deviceContext.VertexShader.SetConstantBuffer(bufferNumber, ConstantPerFrameBuffer);

                // Set shader resource in the pixel shader.
                deviceContext.PixelShader.SetShaderResources(0, textures);
                #endregion

                #region Set Fog Shader Resources
                // Lock the constant buffer so it can be written to.
                deviceContext.MapSubresource(ConstantFogBuffer, MapMode.WriteDiscard, SharpDX.Direct3D11.MapFlags.None, out mappedResource);

                // Copy the matrices into the constant buffer.
                var fogBuffer = new FogBuffer()
                {
                    fogStart = fogStart,
                    fogEnd = fogEnd
                };

                mappedResource.Write(fogBuffer);

                // Unlock the constant buffer.
                deviceContext.UnmapSubresource(ConstantFogBuffer, 0);

                // Set the position of the constant buffer in the vertex shader.
                bufferNumber = 1;

                // Finally set the constant buffer in the vertex shader with the updated values.
                deviceContext.VertexShader.SetConstantBuffer(bufferNumber, ConstantFogBuffer);
                #endregion

                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }
示例#2
0
        private bool SetShaderParameters(DeviceContext deviceContext, Matrix worldMatrix, Matrix viewMatrix, Matrix projectionMatrix, ShaderResourceView[] textures, float fogStart, float fogEnd)
        {
            try
            {
                #region Set Per Frame Shader Resources
                // Transpose the matrices to prepare them for shader.
                worldMatrix.Transpose();
                viewMatrix.Transpose();
                projectionMatrix.Transpose();

                // Lock the constant buffer so it can be written to.
                DataStream mappedResource;
                deviceContext.MapSubresource(ConstantPerFrameBuffer, MapMode.WriteDiscard, SharpDX.Direct3D11.MapFlags.None, out mappedResource);

                // Copy the matrices into the constant buffer.
                var matrixBuffer = new PerFrameBuffer()
                {
                    world      = worldMatrix,
                    view       = viewMatrix,
                    projection = projectionMatrix
                };

                mappedResource.Write(matrixBuffer);

                // Unlock the constant buffer.
                deviceContext.UnmapSubresource(ConstantPerFrameBuffer, 0);

                // Set the position of the constant buffer in the vertex shader.
                var bufferNumber = 0;

                // Finally set the constant buffer in the vertex shader with the updated values.
                deviceContext.VertexShader.SetConstantBuffer(bufferNumber, ConstantPerFrameBuffer);

                // Set shader resource in the pixel shader.
                deviceContext.PixelShader.SetShaderResources(0, textures);
                #endregion

                #region Set Fog Shader Resources
                // Lock the constant buffer so it can be written to.
                deviceContext.MapSubresource(ConstantFogBuffer, MapMode.WriteDiscard, SharpDX.Direct3D11.MapFlags.None, out mappedResource);

                // Copy the matrices into the constant buffer.
                var fogBuffer = new FogBuffer()
                {
                    fogStart = fogStart,
                    fogEnd   = fogEnd
                };

                mappedResource.Write(fogBuffer);

                // Unlock the constant buffer.
                deviceContext.UnmapSubresource(ConstantFogBuffer, 0);

                // Set the position of the constant buffer in the vertex shader.
                bufferNumber = 1;

                // Finally set the constant buffer in the vertex shader with the updated values.
                deviceContext.VertexShader.SetConstantBuffer(bufferNumber, ConstantFogBuffer);
                #endregion

                return(true);
            }
            catch (Exception)
            {
                return(false);
            }
        }