Exemplo n.º 1
0
        private bool SetShaderParameters(DeviceContext deviceContext, Matrix worldMatrix, Matrix viewMatrix, Matrix projectionMatrix, ShaderResourceView texture, ShaderResourceView reflectionTexture, Matrix reflectionMatrix)
        {
            try
            {
                #region Set Matrix 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(ConstantMatrixBuffer, MapMode.WriteDiscard, SharpDX.Direct3D11.MapFlags.None, out mappedResource);

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

                mappedResource.Write(matrixBuffer);

                // Unlock the constant buffer.
                deviceContext.UnmapSubresource(ConstantMatrixBuffer, 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, ConstantMatrixBuffer);

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

                #region Set Reflection Shader Resources
                // Transpose the reflection matrix to prepare it for the shader.
                reflectionMatrix.Transpose();

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

                // Copy the matrices into the constant buffer.
                var reflectionBuffer = new ReflectionBuffer()
                {
                    reflection = reflectionMatrix
                };

                mappedResource.Write(reflectionBuffer);

                // Unlock the constant buffer.
                deviceContext.UnmapSubresource(ConstantReflectionBuffer, 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.PixelShader.SetConstantBuffer(bufferNumber, ConstantReflectionBuffer);

                // Set shader resources in the pixel shader.
                deviceContext.PixelShader.SetShaderResources(1, reflectionTexture);
                #endregion

                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }
Exemplo n.º 2
0
        private bool SetShaderParameters(DeviceContext deviceContext, Matrix worldMatrix, Matrix viewMatrix, Matrix projectionMatrix, ShaderResourceView texture, ShaderResourceView reflectionTexture, Matrix reflectionMatrix)
        {
            try
            {
                #region Set Matrix 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(ConstantMatrixBuffer, MapMode.WriteDiscard, SharpDX.Direct3D11.MapFlags.None, out mappedResource);

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

                mappedResource.Write(matrixBuffer);

                // Unlock the constant buffer.
                deviceContext.UnmapSubresource(ConstantMatrixBuffer, 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, ConstantMatrixBuffer);

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

                #region Set Reflection Shader Resources
                // Transpose the reflection matrix to prepare it for the shader.
                reflectionMatrix.Transpose();

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

                // Copy the matrices into the constant buffer.
                var reflectionBuffer = new ReflectionBuffer()
                {
                    reflection = reflectionMatrix
                };

                mappedResource.Write(reflectionBuffer);

                // Unlock the constant buffer.
                deviceContext.UnmapSubresource(ConstantReflectionBuffer, 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.PixelShader.SetConstantBuffer(bufferNumber, ConstantReflectionBuffer);

                // Set shader resources in the pixel shader.
                deviceContext.PixelShader.SetShaderResources(1, reflectionTexture);
                #endregion

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