Пример #1
0
        private bool SetShaderParameters(DeviceContext deviceContext, Matrix worldMatrix, Matrix viewMatrix, Matrix projectionMatrix)
        {
            try
            {
                worldMatrix.Transpose();
                viewMatrix.Transpose();
                projectionMatrix.Transpose();

                deviceContext.MapSubresource(ConstantMatrixBuffer, MapMode.WriteDiscard, MapFlags.None, out DataStream mappedResource);

                var matrixBuffer = new WorldViewProjectionMatrixBuffer()
                {
                    world      = worldMatrix,
                    view       = viewMatrix,
                    projection = projectionMatrix
                };

                mappedResource.Write(matrixBuffer);

                deviceContext.UnmapSubresource(ConstantMatrixBuffer, 0);

                var bufferSlotNumber = 0;

                deviceContext.VertexShader.SetConstantBuffer(bufferSlotNumber, ConstantMatrixBuffer);

                return(true);
            }
            catch (Exception ex)
            {
                //Log.WriteToFile(ErrorLevel.Error, "ColourShader.SetShaderParameters", ex, true);

                return(false);
            }
        }
Пример #2
0
        private bool SetShaderParameters(DeviceContext deviceContext, Matrix worldMatrix, Matrix viewMatrix, Matrix projectionMatrix, ShaderResourceView texture)
        {
            try
            {
                worldMatrix.Transpose();
                viewMatrix.Transpose();
                projectionMatrix.Transpose();

                deviceContext.MapSubresource(ConstantMatrixBuffer, MapMode.WriteDiscard, MapFlags.None, out DataStream mappedResource);

                var matrixBuffer = new WorldViewProjectionMatrixBuffer()
                {
                    world      = worldMatrix,
                    view       = viewMatrix,
                    projection = projectionMatrix
                };
                mappedResource.Write(matrixBuffer);

                deviceContext.UnmapSubresource(ConstantMatrixBuffer, 0);

                int bufferPositionNumber = 0;

                deviceContext.VertexShader.SetConstantBuffer(bufferPositionNumber, ConstantMatrixBuffer);

                deviceContext.PixelShader.SetShaderResources(0, texture);

                return(true);
            }
            catch
            {
                return(false);
            }
        }
Пример #3
0
        private bool SetShaderParameters(DeviceContext deviceContext, Matrix worldMatrix, Matrix viewMatrix, Matrix projectionMatrix, ShaderResourceView texture, Vector3 lightDirection, Vector4 diffuseColour)
        {
            try
            {
                worldMatrix.Transpose();
                viewMatrix.Transpose();
                projectionMatrix.Transpose();

                deviceContext.MapSubresource(ConstantMatrixBuffer, MapMode.WriteDiscard, MapFlags.None, out DataStream mappedResource);

                var matrixBuffer = new WorldViewProjectionMatrixBuffer()
                {
                    world      = worldMatrix,
                    view       = viewMatrix,
                    projection = projectionMatrix
                };

                mappedResource.Write(matrixBuffer);

                deviceContext.UnmapSubresource(ConstantMatrixBuffer, 0);

                var bufferPositionNumber = 0;

                deviceContext.VertexShader.SetConstantBuffer(0, ConstantMatrixBuffer);
                deviceContext.PixelShader.SetShaderResource(0, texture);

                deviceContext.MapSubresource(ConstantLightBuffer, MapMode.WriteDiscard, MapFlags.None, out DataStream mappedResourceLight);

                var lightBuffer = new LightBuffer()
                {
                    diffuseColour  = diffuseColour,
                    lightDirection = lightDirection,
                    padding        = 0
                };

                mappedResourceLight.Write(lightBuffer);

                deviceContext.UnmapSubresource(ConstantLightBuffer, 0);

                bufferPositionNumber = 0;

                deviceContext.PixelShader.SetConstantBuffer(bufferPositionNumber, ConstantLightBuffer);

                return(true);
            }
            catch (Exception ex)
            {
                //Log.WriteToFile(ErrorLevel.Error, "LightShader.SetShaderParameters", ex, true);

                return(false);
            }
        }
Пример #4
0
        private bool SetShaderParameters(DeviceContext deviceContext, Matrix worldMatrix, Matrix viewMatrix, Matrix projectionMatrix, Vector4 ambientColour, Vector4 diffuseColour, Vector3 lightDirection, ShaderResourceView texture)
        {
            try
            {
                // Transpose the matrices to prepare them for shader.
                worldMatrix.Transpose();
                viewMatrix.Transpose();
                projectionMatrix.Transpose();

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

                // Copy the matrices into the constant buffer.
                var matrixBuffer = new WorldViewProjectionMatrixBuffer()
                {
                    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.
                int bufferPositionNumber = 0;

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

                // Set shader resource in the pixel shader.
                deviceContext.PixelShader.SetShaderResource(0, texture);

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

                // Copy the lighting variables into the constant buffer.
                var lightBuffer = new LightBuffer()
                {
                    ambientColour  = ambientColour,
                    diffuseColour  = diffuseColour,
                    lightDirection = lightDirection,
                    padding        = 0
                };
                mappedResource.Write(lightBuffer);

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

                // Set the position of the light constant buffer in the pixel shader.
                bufferPositionNumber = 0;

                // Finally set the light constant buffer in the pixel shader with the updated values.
                deviceContext.PixelShader.SetConstantBuffer(bufferPositionNumber, ConstantLightBuffer);

                return(true);
            }
            catch
            {
                return(false);
            }
        }
Пример #5
0
        private bool SetShaderParameters(DeviceContext deviceContext, Matrix worldMatrix, Matrix viewMatrix, Matrix projectionMatrix, ShaderResourceView texture, Vector4 pixelColor)
        {
            try
            {
                #region Matrix Constant Buffer
                // Transpose the matrices to prepare them for shader.
                worldMatrix.Transpose();
                viewMatrix.Transpose();
                projectionMatrix.Transpose();

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

                // Copy the matrices into the constant buffer.
                var matrixBuffer = new WorldViewProjectionMatrixBuffer()
                {
                    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;

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

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

                #region Pixel Constant Shader
                // Lock the pixel constant buffer so it can be written to.
                deviceContext.MapSubresource(ConstantPixelBuffer, MapMode.WriteDiscard, SharpDX.Direct3D11.MapFlags.None, out mappedResource);

                // Copy the matrices into the constant buffer.
                var pixelBuffer = new PixelBuffer()
                {
                    pixelColour = pixelColor
                };
                mappedResource.Write(pixelBuffer);

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

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

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

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