private bool SetShaderParameters(DeviceContext deviceContext, Matrix worldMatrix, Matrix viewMatrix, Matrix projectionMatrix, Matrix reflectionMatrix, ShaderResourceView reflectionTexture, ShaderResourceView refractionTexture, ShaderResourceView normalTexture, float waterTranslation, float reflectRefractScale) { 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, MapFlags.None, out mappedResource); // Copy the passed in matrices into the constant buffer. DMatrixBuffer matrixBuffer = new DMatrixBuffer() { 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); #endregion #region Set Reflection Vertex Shader // 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. DReflectionBuffer reflectionBuffer = new DReflectionBuffer() { reflection = reflectionMatrix }; mappedResource.Write(reflectionBuffer); // Unlock the constant buffer. deviceContext.UnmapSubresource(ConstantReflectionBuffer, 0); // Set the position of the constant buffer in the vertex shader. bufferPositionNumber = 1; // Finally set the constant buffer in the vertex shader with the updated values. deviceContext.VertexShader.SetConstantBuffer(bufferPositionNumber, ConstantReflectionBuffer); #endregion #region Set Water Pixel Shader // Lock the water constant buffer so it can be written to. deviceContext.MapSubresource(ConstantWaterBuffer, MapMode.WriteDiscard, SharpDX.Direct3D11.MapFlags.None, out mappedResource); // Copy the matrices into the constant buffer. DWaterBuffer waterBuffer = new DWaterBuffer() { waterTranslation = waterTranslation, reflectRefractScale = reflectRefractScale, padding = Vector2.Zero }; mappedResource.Write(waterBuffer); // Unlock the constant buffer. deviceContext.UnmapSubresource(ConstantWaterBuffer, 0); // Set the position of the constant buffer in the vertex shader. bufferPositionNumber = 0; // Finally set the constant buffer in the vertex shader with the updated values. deviceContext.PixelShader.SetConstantBuffer(bufferPositionNumber, ConstantWaterBuffer); #endregion #region Set Pixel Shader Resources // Set the reflection texture resource in the pixel shader. deviceContext.PixelShader.SetShaderResources(0, reflectionTexture); // Set the refraction texture resource in the pixel shader. deviceContext.PixelShader.SetShaderResources(1, refractionTexture); // Set the normal map texture resource in the pixel shader. deviceContext.PixelShader.SetShaderResources(2, normalTexture); #endregion return(true); } catch (Exception) { return(false); } }
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 3 matrixes first as DirectX 11 requires all input matrices to be transposed for memory alignment reasons. worldMatrix.Transpose(); viewMatrix.Transpose(); projectionMatrix.Transpose(); // Lock the constant buffer so it can be written to. DataStream mappedResource; deviceContext.MapSubresource(ConstantMatrixBuffer, MapMode.WriteDiscard, MapFlags.None, out mappedResource); // Copy the passed in matrices into the constant buffer. DMatrixBuffer matrixBuffer = new DMatrixBuffer() { 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); #endregion #region Set Reflection Shader Resources // Transpose the reflection matrix first as DirectX 11 requires all input matrices to be transposed for memory alignment reasons. 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. DReflectionBuffer reflectionBuffer = new DReflectionBuffer() { reflection = reflectionMatrix }; mappedResource.Write(reflectionBuffer); // Unlock the constant buffer. deviceContext.UnmapSubresource(ConstantReflectionBuffer, 0); // Set the position of the constant buffer in the vertex shader. bufferPositionNumber = 1; // Finally set the constant buffer in the vertex shader with the updated values. deviceContext.VertexShader.SetConstantBuffer(bufferPositionNumber, ConstantReflectionBuffer); // Set shader resources in the pixel shader. deviceContext.PixelShader.SetShaderResources(bufferPositionNumber, reflectionTexture); #endregion return(true); } catch (Exception) { return(false); } }