private bool SetShaderParameters(DeviceContext deviceContext, Matrix worldMatrix, Matrix viewMatrix, Matrix projectionMatrix, ShaderResourceView[] textures, Vector4 clipPlane) { 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); // Set shader resource in the pixel shader. deviceContext.PixelShader.SetShaderResources(0, textures); #endregion #region Set Clip Plane Shader Resources // Lock the constant buffer so it can be written to. deviceContext.MapSubresource(ConstantClipPlaneBuffer, MapMode.WriteDiscard, SharpDX.Direct3D11.MapFlags.None, out mappedResource); // Copy the matrices into the constant buffer. DClipPlaneBuffer clipPlaneBuffer = new DClipPlaneBuffer() { clipPlane = clipPlane }; mappedResource.Write(clipPlaneBuffer); // Unlock the constant buffer. deviceContext.UnmapSubresource(ConstantClipPlaneBuffer, 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, ConstantClipPlaneBuffer); #endregion return(true); } catch { return(false); } }
private bool SetShaderParameters(DeviceContext deviceContext, Matrix worldMatrix, Matrix viewMatrix, Matrix projectionMatrix, ShaderResourceView texture, Vector3 lightDirection, Vector4 ambientColor, Vector4 diffuseColor, Vector4 clipPlane) { try { DataStream mappedResource; #region Pixel Shader Resources // Set shader resource in the pixel shader. deviceContext.PixelShader.SetShaderResource(0, texture); #endregion #region Constant Matrix 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, 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 Constant Light Buffer // Now for the Lighting Shader. // 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. DLightBuffer lightBuffer = new DLightBuffer() { ambientColor = ambientColor, diffuseColor = diffuseColor, lightDirection = lightDirection, }; 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); #endregion #region Constant Clip Plane Vertex Shader // Lock the camera constant buffer so it can be written to. deviceContext.MapSubresource(ConstantClipPlaneBuffer, MapMode.WriteDiscard, SharpDX.Direct3D11.MapFlags.None, out mappedResource); // Copy the lighting variables into the constant buffer. DClipPlaneBuffer clipPlaneBuffer = new DClipPlaneBuffer() { clipPlane = clipPlane }; mappedResource.Write(clipPlaneBuffer); // Unlock the constant buffer. deviceContext.UnmapSubresource(ConstantClipPlaneBuffer, 0); // Set the position of the light constant buffer in the pixel shader. bufferPositionNumber = 1; // Now set the camera constant buffer in the vertex shader with the updated values. deviceContext.VertexShader.SetConstantBuffer(bufferPositionNumber, ConstantClipPlaneBuffer); #endregion return(true); } catch (Exception) { return(false); } }