private bool SetShaderParameters(DeviceContext deviceContext, Matrix worldMatrix, Matrix viewMatrix, Matrix projectionMatrix, ShaderResourceView[] textures, Vector3 lightDirection, Vector4 diffuseColor, Vector3 cameraPosition, Vector4 specularColor, float specularPower) { try { #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. 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 resource in the pixel shader. deviceContext.PixelShader.SetShaderResources(0, textures); #endregion #region Constant Light Buffer // Lock the light constant buffer so it can be written to. deviceContext.MapSubresource(ConstantLightBuffer, MapMode.WriteDiscard, SharpDX.Direct3D11.MapFlags.None, out mappedResource); // Copy the lighting variables into the constant buffer. var lightBuffer = new LightBuffer() { diffuseColor = diffuseColor, lightDirection = lightDirection, specularColor = specularColor, specularPower = specularPower, }; mappedResource.Write(lightBuffer); // Unlock the constant buffer. deviceContext.UnmapSubresource(ConstantLightBuffer, 0); // Set the position of the light constant buffer in the pixel shader. bufferNumber = 0; // Finally set the light constant buffer in the pixel shader with the updated values. deviceContext.PixelShader.SetConstantBuffer(bufferNumber, ConstantLightBuffer); #endregion #region Constant Camera Buffer // Lock the camera constant buffer so it can be written to. deviceContext.MapSubresource(ConstantCameraBuffer, MapMode.WriteDiscard, SharpDX.Direct3D11.MapFlags.None, out mappedResource); // Copy the lighting variables into the constant buffer. var cameraBuffer = new CameraBuffer() { cameraPosition = cameraPosition, padding = 0.0f }; mappedResource.Write(cameraBuffer); // Unlock the constant buffer. deviceContext.UnmapSubresource(ConstantCameraBuffer, 0); // Set the position of the light constant buffer in the pixel shader. bufferNumber = 1; // Now set the camera constant buffer in the vertex shader with the updated values. deviceContext.VertexShader.SetConstantBuffer(bufferNumber, ConstantCameraBuffer); #endregion return true; } catch (Exception) { return false; } }
private bool SetShaderParameters(DeviceContext deviceContext, Matrix worldMatrix, Matrix viewMatrix, Matrix projectionMatrix, ShaderResourceView texture, Vector3 lightDirection, Vector4 ambientColor, Vector4 diffuseColor, Vector3 cameraPosition, Vector4 specularColor, float specularPower) { try { DataStream mappedResource; #region Constant Matrix Buffer // 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. 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 resource in the pixel shader. deviceContext.PixelShader.SetShaderResource(0, texture); #endregion #region Constant Camera Buffer // Lock the camera constant buffer so it can be written to. deviceContext.MapSubresource(ConstantCameraBuffer, MapMode.WriteDiscard, SharpDX.Direct3D11.MapFlags.None, out mappedResource); // Copy the lighting variables into the constant buffer. var cameraBuffer = new CameraBuffer() { cameraPosition = cameraPosition, padding = 0.0f }; mappedResource.Write(cameraBuffer); // Unlock the constant buffer. deviceContext.UnmapSubresource(ConstantCameraBuffer, 0); // Set the position of the light constant buffer in the pixel shader. bufferNumber = 1; // Now set the camera constant buffer in the vertex shader with the updated values. deviceContext.VertexShader.SetConstantBuffer(bufferNumber, ConstantCameraBuffer); #endregion #region Constant Light Buffer // Lock the light constant buffer so it can be written to. deviceContext.MapSubresource(ConstantLightBuffer, MapMode.WriteDiscard, SharpDX.Direct3D11.MapFlags.None, out mappedResource); // Copy the lighting variables into the constant buffer. var lightBuffer = new LightBuffer() { ambientColor = ambientColor, diffuseColor = diffuseColor, lightDirection = lightDirection, specularColor = specularColor, specularPower = specularPower }; mappedResource.Write(lightBuffer); // Unlock the constant buffer. deviceContext.UnmapSubresource(ConstantLightBuffer, 0); // Set the position of the light constant buffer in the pixel shader. bufferNumber = 0; // Finally set the light constant buffer in the pixel shader with the updated values. deviceContext.PixelShader.SetConstantBuffer(bufferNumber, ConstantLightBuffer); #endregion return(true); } catch (Exception) { return(false); } }