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 MatrixBuffer() { 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); } }
public override void SetSceneVariables(DeviceContext deviceContext, Matrix WorldMatrix, Camera camera) { DataStream mappedResource; #region Constant Matrix Buffer Matrix tMatrix = WorldMatrix; Matrix vMatrix = camera.ViewMatrix; Matrix cMatrix = camera.ProjectionMatrix; vMatrix.Transpose(); cMatrix.Transpose(); tMatrix.Transpose(); deviceContext.MapSubresource(ConstantMatrixBuffer, MapMode.WriteDiscard, MapFlags.None, out mappedResource); MatrixBuffer matrixBuffer = new MatrixBuffer() { world = tMatrix, view = vMatrix, projection = cMatrix }; mappedResource.Write(matrixBuffer); deviceContext.UnmapSubresource(ConstantMatrixBuffer, 0); int bufferSlotNumber = 0; deviceContext.VertexShader.SetConstantBuffer(bufferSlotNumber, ConstantMatrixBuffer); #endregion }
public void SetShaderParam(D3DDevice device, Vector3 lightDirection, ShaderResourceView texture, Vector4 ambientColor, Vector4 diffuseColour, ref Matrix world, Matrix view, Matrix proj) { LightBuffer lightBuffer = new LightBuffer() { ambientColor = ambientColor, diffuseColor = diffuseColour, lightDirection = lightDirection, padding = 0 }; MatrixBuffer matrixBuffer = new MatrixBuffer() { world = world, view = view, proj = proj }; device.ImmediateContext.MapSubresource(mMatrixConstantBuffer, MapMode.WriteDiscard, MapFlags.None, out mMappedResourceMatrix); mMappedResourceMatrix.Write(matrixBuffer); device.ImmediateContext.UnmapSubresource(mMatrixConstantBuffer, 0); device.ImmediateContext.VertexShader.SetConstantBuffer(1, mMatrixConstantBuffer); device.ImmediateContext.MapSubresource(mLightConstantBuffer, MapMode.WriteDiscard, MapFlags.None, out mMappedResourceLight); mMappedResourceLight.Write(lightBuffer); device.ImmediateContext.UnmapSubresource(mLightConstantBuffer, 0); device.ImmediateContext.PixelShader.SetConstantBuffer(0, mLightConstantBuffer); device.ImmediateContext.PixelShader.SetShaderResource(0, texture); }
private void SetShaderParameters(DeviceContext deviceContext, Matrix world, Matrix view, Matrix projection, float flags, Vector2 brightnessCoeffs) { try { // Transpose the matrices to prepare them for shader. world.Transpose(); view.Transpose(); projection.Transpose(); // Lock the 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 MatrixBuffer() { world = world, view = view, projection = projection }; 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); // Lock the settings constant buffer so it can be written to. deviceContext.MapSubresource(ConstantSettingsBuffer, MapMode.WriteDiscard, MapFlags.None, out mappedResource); // Copy the values into the settings buffer. var settingsBuffer = new SettingsBuffer() { flags = flags, brightnessCoeffs = brightnessCoeffs }; mappedResource.Write(settingsBuffer); // Unlock the constant buffer deviceContext.UnmapSubresource(ConstantSettingsBuffer, 0); // Set the position of the constant buffer in the pixel shader. bufferNumber = 0; // Finally set the constant buffer in the pixel shader with the updated values. deviceContext.PixelShader.SetConstantBuffer(bufferNumber, ConstantSettingsBuffer); } catch (Exception ex) { throw new Exception("Could not set shader parameters: " + ex.Message); } }
unsafe void UpdateMatrixBuffer() { var mb = new MatrixBuffer() { View = viewMatrix, World = worldMatrix, Text = textMatrix }; deviceContext.UpdateSubresource(matrixBuffer, 0, null, (IntPtr)(&mb), 1, 0); deviceContext.VertexShader.SetConstantBuffer(0, matrixBuffer); }
public void Render(DeviceContext deviceContext, int indexCount, Matrix worldMatrix, Matrix viewMatrix, Matrix projectionMatrix, Texture texture, Vector2 translation) { worldMatrix.Transpose(); viewMatrix.Transpose(); projectionMatrix.Transpose(); // Lock the constant memory buffer so it can be written to. DataStream mappedResource; deviceContext.MapSubresource(ConstantMatrixBuffer, MapMode.WriteDiscard, SharpDX.Direct3D11.MapFlags.None, out mappedResource); // Copy the transposed matrices (because they are stored in column-major order on the GPU by default) 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); deviceContext.MapSubresource(ConstantTranslationBuffer, MapMode.WriteDiscard, SharpDX.Direct3D11.MapFlags.None, out mappedResource); mappedResource.Write(new TranslationBuffer { translation = translation }); deviceContext.UnmapSubresource(ConstantMatrixBuffer, 0); // Set the position of the constant buffer in the vertex shader. const int bufferNumber = 0; // Finally set the constant buffer in the vertex shader with the updated values. deviceContext.VertexShader.SetConstantBuffer(bufferNumber, ConstantMatrixBuffer); deviceContext.PixelShader.SetConstantBuffer(0, ConstantTranslationBuffer); // Set shader resource in the pixel shader. deviceContext.PixelShader.SetShaderResource(0, texture.TextureResource); // Set the vertex input layout. deviceContext.InputAssembler.InputLayout = Layout; // Set the vertex and pixel shaders that will be used to render this triangle. deviceContext.VertexShader.Set(VertexShader); deviceContext.PixelShader.Set(PixelShader); // Set the sampler state in the pixel shader. deviceContext.PixelShader.SetSampler(0, SamplerState); // Render the triangle. deviceContext.DrawIndexed(indexCount, 0, 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 MatrixBuffer() { 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); } }
public virtual void SetSceneVariables(ID3D11DeviceContext context, Matrix4x4 WorldMatrix, Camera camera) { Matrix4x4 tMatrix = Matrix4x4.Transpose(WorldMatrix); MatrixBuffer matrixBuffer = new MatrixBuffer() { world = tMatrix, view = camera.ViewMatrixTransposed, projection = camera.ProjectionMatrixTransposed }; ConstantBufferFactory.UpdateVertexBuffer(context, ConstantMatrixBuffer, 0, matrixBuffer); }
public void UpdateMatrixBuffer(DeviceContext deviceContext, Buffer constantMatrixBuffer, Matrix worldMatrix, Matrix viewMatrix, Matrix projectionMatrix) { worldMatrix.Transpose(); viewMatrix.Transpose(); projectionMatrix.Transpose(); var matrixBuffer = new MatrixBuffer { world = worldMatrix, view = viewMatrix, projection = projectionMatrix }; UpdateBuffer(deviceContext, constantMatrixBuffer, matrixBuffer); }
private bool SetShaderParameters(DeviceContext deviceContext, Matrix worldMatrix, Matrix viewMatrix, Matrix projectionMatrix, ShaderResourceView[] textures) { 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 resource in the pixel shader. deviceContext.PixelShader.SetShaderResources(0, textures); #endregion return(true); } catch (Exception) { return(false); } }
public virtual void SetSceneVariables(DeviceContext context, Matrix WorldMatrix, Camera camera) { Matrix tMatrix = WorldMatrix; Matrix vMatrix = camera.ViewMatrix; Matrix cMatrix = camera.ProjectionMatrix; vMatrix.Transpose(); cMatrix.Transpose(); tMatrix.Transpose(); MatrixBuffer matrixBuffer = new MatrixBuffer() { world = tMatrix, view = vMatrix, projection = cMatrix }; ConstantBufferFactory.UpdateVertexBuffer(context, ConstantMatrixBuffer, 0, matrixBuffer); }
//we should only update the index of the changed datas for index buffer,matrixbuffer and color buffer inside a burst job to avoid overhead int UpdateBuffers(int renderIndex) { SpriteSheetManager.ReleaseBuffer(renderIndex); RenderInformation renderInformation = SpriteSheetManager.renderInformation[renderIndex]; int instanceCount = EntityManager.GetBuffer <SpriteIndexBuffer>(renderInformation.bufferEntity).Length; if (instanceCount > 0) { int stride = instanceCount >= 16 ? 16 : 16 * SpriteSheetCache.GetLenght(renderInformation.material); if (renderInformation.updateUvs) { SpriteSheetManager.ReleaseUvBuffer(renderIndex); renderInformation.uvBuffer = new ComputeBuffer(instanceCount, stride); renderInformation.uvBuffer.SetData(EntityManager.GetBuffer <UvBuffer>(renderInformation.bufferEntity).Reinterpret <float4>().AsNativeArray()); renderInformation.material.SetBuffer("uvBuffer", renderInformation.uvBuffer); renderInformation.updateUvs = false; } renderInformation.indexBuffer = new ComputeBuffer(instanceCount, sizeof(int)); renderInformation.indexBuffer.SetData(EntityManager.GetBuffer <SpriteIndexBuffer>(renderInformation.bufferEntity).Reinterpret <int>().AsNativeArray()); renderInformation.material.SetBuffer("indexBuffer", renderInformation.indexBuffer); renderInformation.matrixBuffer = new ComputeBuffer(instanceCount, MatrixBuffer.SizeOf()); renderInformation.matrixBuffer.SetData(MatrixBuffer.GetMatrixBuffer(renderInformation.bufferEntity).AsNativeArray()); renderInformation.material.SetBuffer("matrixBuffer", renderInformation.matrixBuffer); renderInformation.args[1] = (uint)instanceCount; renderInformation.argsBuffer.SetData(renderInformation.args); renderInformation.colorsBuffer = new ComputeBuffer(instanceCount, 16); renderInformation.colorsBuffer.SetData(EntityManager.GetBuffer <SpriteColorBuffer>(renderInformation.bufferEntity).Reinterpret <float4>().AsNativeArray()); renderInformation.material.SetBuffer("colorsBuffer", renderInformation.colorsBuffer); } return(instanceCount); }
private bool SetShaderParameters(DeviceContext deviceContext, Matrix worldMatrix, Matrix viewMatrix, Matrix projectionMatrix, ShaderResourceView texture) { try { // 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.SetShaderResource(0, texture); return true; } catch (Exception) { return false; } }
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 void timer1_Tick(object sender, EventArgs e) { MatrixBuffer zBuffer = new MatrixBuffer(figures); pictureBoxMain.Image = zBuffer.render(pictureBoxMain.Size, new Math3D.Point3D(tX.Value, tY.Value, tZ.Value)); }
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); } }
public void Render(DeviceContext deviceContext, int indexCount, Matrix worldMatrix, Matrix viewMatrix, Matrix projectionMatrix, ShaderResourceView texture) { worldMatrix.Transpose(); viewMatrix.Transpose(); projectionMatrix.Transpose(); // Lock the constant memory buffer so it can be written to. DataStream mappedResource; deviceContext.MapSubresource(ConstantMatrixBuffer, MapMode.WriteDiscard, SharpDX.Direct3D11.MapFlags.None, out mappedResource); // Copy the transposed matrices (because they are stored in column-major order on the GPU by default) 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. const int 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); // Set the vertex input layout. deviceContext.InputAssembler.InputLayout = Layout; // Set the vertex and pixel shaders that will be used to render this triangle. deviceContext.VertexShader.Set(VertexShader); deviceContext.PixelShader.Set(PixelShader); // Set the sampler state in the pixel shader. deviceContext.PixelShader.SetSampler(0, SamplerState); // Render the triangle. deviceContext.DrawIndexed(indexCount, 0, 0); }
public void Render(DeviceContext deviceContext, int indexCount, Matrix worldMatrix, Matrix viewMatrix, Matrix projectionMatrix, Light light, ShaderResourceView[] textures, Vector4 clippingPlane) { worldMatrix.Transpose(); viewMatrix.Transpose(); projectionMatrix.Transpose(); // Lock the constant memory buffer so it can be written to. DataStream mappedResource; deviceContext.MapSubresource(ConstantMatrixBuffer, MapMode.WriteDiscard, MapFlags.None, out mappedResource); // Copy the transposed matrices (because they are stored in column-major order on the GPU by default) 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); deviceContext.MapSubresource(ConstantLightBuffer, MapMode.WriteDiscard, MapFlags.None, out mappedResource); mappedResource.Write( new LightBuffer { ambiantColor = light.AmbiantColor, diffuseColor = light.Color, direction = light.Direction }); deviceContext.UnmapSubresource(ConstantLightBuffer, 0); deviceContext.MapSubresource(ConstantClippingPlaneBuffer, MapMode.WriteDiscard, MapFlags.None, out mappedResource); mappedResource.Write( new ClippingPlaneBuffer {plane = clippingPlane}); deviceContext.UnmapSubresource(ConstantClippingPlaneBuffer, 0); // Finally set the constant buffers in the vertex shader with the updated values. deviceContext.VertexShader.SetConstantBuffer(0, ConstantMatrixBuffer); deviceContext.VertexShader.SetConstantBuffer(1, ConstantClippingPlaneBuffer); deviceContext.PixelShader.SetConstantBuffer(0, ConstantLightBuffer); deviceContext.PixelShader.SetShaderResources(0, textures); // Set the vertex input layout. deviceContext.InputAssembler.InputLayout = Layout; // Set the vertex and pixel shaders that will be used to render this triangle. deviceContext.VertexShader.Set(VertexShader); deviceContext.PixelShader.Set(PixelShader); deviceContext.PixelShader.SetSampler(0, SamplerState); // Render the triangles. deviceContext.DrawIndexed(indexCount, 0, 0); }
unsafe void UpdateMatrixBuffer() { var mb = new MatrixBuffer() { View = viewMatrix, World = worldMatrix, Text = textMatrix}; deviceContext.UpdateSubresource(matrixBuffer, 0, null, (IntPtr)(&mb), 1, 0); deviceContext.VertexShader.SetConstantBuffer(0, matrixBuffer); }
private void SetShaderParameters(DrawEventArgs args) { Geometry shape = core.Actor.Shape; if (core.UseTurntable) { float t = (float)args.TotalTime.TotalSeconds * core.TurntableSpeed; core.Actor.Rotation = Quaternion.RotationAxis(VSTools.UnitY, t); } // Update matrices in the constant buffer Matrix modelMatrix = Matrix.Scaling(core.Actor.LocalScale) * Matrix.RotationQuaternion(core.Actor.Rotation) * Matrix.Translation(core.Actor.Position); MatrixBuffer projectionModel = new MatrixBuffer { World = Matrix.Transpose((Camera as VSViewerCamera).World), View = Matrix.Transpose((Camera.View)), Projection = Matrix.Transpose(Camera.Projection) }; m_matrixBuffer.Value = projectionModel; // Set Vertex shader resources Device.ImmediateContext.VertexShader.SetConstantBuffer(0, m_matrixBuffer.Buffer); Device.ImmediateContext.UpdateSubresource(shape.instancedVertices, vertexBuffer); // Set pixel shader resources Device.ImmediateContext.PixelShader.SetShaderResource(0, m_textureResourceView); }
public void Render(DeviceContext deviceContext, int indexCount, Matrix worldMatrix, Matrix viewMatrix, Matrix projectionMatrix, Texture texture, Light light, Camera camera) { worldMatrix.Transpose(); viewMatrix.Transpose(); projectionMatrix.Transpose(); // Lock the constant memory buffer so it can be written to. DataStream mappedResource; deviceContext.MapSubresource(ConstantMatrixBuffer, MapMode.WriteDiscard, MapFlags.None, out mappedResource); // Copy the transposed matrices (because they are stored in column-major order on the GPU by default) 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); deviceContext.MapSubresource(ConstantLightBuffer, MapMode.WriteDiscard, MapFlags.None, out mappedResource); mappedResource.Write( new LightBuffer { ambiantColor = light.AmbiantColor, diffuseColor = light.Color, direction = light.Direction, specularPower = light.SpecularPower, specularColor = light.SpecularColor }); deviceContext.UnmapSubresource(ConstantLightBuffer, 0); deviceContext.MapSubresource(ConstantCameraBuffer, MapMode.WriteDiscard, MapFlags.None, out mappedResource); mappedResource.Write( new CameraBuffer { cameraPosition = camera.Position }); deviceContext.UnmapSubresource(ConstantCameraBuffer, 0); // Finally set the constant buffers in the vertex shader with the updated values. deviceContext.VertexShader.SetConstantBuffer(0, ConstantMatrixBuffer); deviceContext.VertexShader.SetConstantBuffer(1, ConstantCameraBuffer); deviceContext.PixelShader.SetConstantBuffer(0, ConstantLightBuffer); // Set shader resource in the pixel shader. deviceContext.PixelShader.SetShaderResource(0, texture.TextureResource); // Set the vertex input layout. deviceContext.InputAssembler.InputLayout = Layout; // Set the vertex and pixel shaders that will be used to render this triangle. deviceContext.VertexShader.Set(VertexShader); deviceContext.PixelShader.Set(PixelShader); // Set the sampler state in the pixel shader. deviceContext.PixelShader.SetSampler(0, SamplerState); // Render the triangle. deviceContext.DrawIndexed(indexCount, 0, 0); }
public void Apply() { Transformations = MatrixBuffer.ToArray(); Textures = TextureBuffer.ToArray(); ChunkManager.PullingShaderData = true; }
public void Init() { _form = new TestForm { ShowTestPanel = true, ClientSize = new Size(1280, 800) }; _form.WindowState = FormWindowState.Minimized; _form.Show(); _form.WindowState = FormWindowState.Normal; _graphics = new GorgonGraphics(); _swap = _graphics.Output.CreateSwapChain("Screen", new GorgonSwapChainSettings() { Window = _form.panelDisplay, DepthStencilFormat = BufferFormat.D24_UIntNormal_S8_UInt }); _swap.AfterSwapChainResized += (sender, args) => { var currentMatrix = new MatrixBuffer(); _graphics.Rasterizer.SetViewport(_swap.Viewport); _aspect = (_swap.Settings.VideoMode.Width) / (float)(_swap.Settings.VideoMode.Height); currentMatrix.Projection = Matrix.PerspectiveFovLH(100.39f.Radians(), _aspect, 0.1f, 1000.0f); currentMatrix.View = Matrix.LookAtLH(new Vector3(0, 0, -0.75f), new Vector3(0, 0, 1.0f), Vector3.UnitY); _graphics.Output.SetRenderTarget(_swap, _swap.DepthStencilBuffer); pvw = currentMatrix.View * currentMatrix.Projection; }; _swap.AfterStateTransition += (sender, args) => { var currentMatrix = new MatrixBuffer(); _graphics.Rasterizer.SetViewport(_swap.Viewport); _aspect = (_swap.Settings.VideoMode.Width) / (float)(_swap.Settings.VideoMode.Height); currentMatrix.Projection = Matrix.PerspectiveFovLH(100.39f.Radians(), _aspect, 0.1f, 1000.0f); currentMatrix.View = Matrix.LookAtLH(new Vector3(0, 0, -0.75f), new Vector3(0, 0, 1.0f), Vector3.UnitY); _graphics.Output.SetRenderTarget(_swap, _swap.DepthStencilBuffer); pvw = currentMatrix.View * currentMatrix.Projection; }; var button = new Button() { Text = "3D", Location = new Point(90, 3) }; button.Click += (sender, args) => { _3d = !_3d; Matrix currentMatrix = Matrix.LookAtLH(new Vector3(0, 0, _camPos), new Vector3(0, 0, 1.0f), Vector3.UnitY); Matrix projection = Matrix.PerspectiveFovLH(100.39f.Radians(), _aspect, 0.1f, 1000.0f); pvw = currentMatrix * projection; }; _form.panelInput.Controls.Add(button); _sprite = new vertex[Count * 4]; for (int i = 0; i < Count; i++) { _balls[i].Scale = 1.0f; _balls[i].ScaleDelta = (GorgonRandom.RandomSingle() * 1.5f) + 0.25f; _balls[i].AlphaBounce = _balls[i].ScaleBouce = false; _balls[i].XBounce = GorgonRandom.RandomInt32(0, 100) > 50; _balls[i].YBounce = GorgonRandom.RandomInt32(0, 100) > 50; _balls[i].ZBounce = GorgonRandom.RandomInt32(0, 100) > 50; _balls[i].Velocity = new Vector3((GorgonRandom.RandomSingle() * 0.5f), (GorgonRandom.RandomSingle() * 0.5f), (GorgonRandom.RandomSingle() * 0.5f)); _balls[i].Angle = 0.0f; _balls[i].AngleDelta = 1.0f; _balls[i].Color = new Vector4(1.0f); _balls[i].AlphaDelta = GorgonRandom.RandomSingle() * 0.5f; _balls[i].Checkered = true; // GorgonRandom.RandomInt32(0, 100) > 50; _balls[i].Position = new Vector3((GorgonRandom.RandomSingle() * 2.0f) - 1.0f, (GorgonRandom.RandomSingle() * 2.0f) - 1.0f, GorgonRandom.RandomSingle()); } _vs = _graphics.Shaders.CreateShader <GorgonVertexShader>("TestVShader", "VS", _shader, null, true); _ps = _graphics.Shaders.CreateShader <GorgonPixelShader>("TestPShader", "PS", _shader, null, true); _layout = _graphics.Input.CreateInputLayout("Input", typeof(vertex), _vs); int vertexSize = _layout.Size; int index = 0; var indices = new int[Count * 6 * sizeof(int)]; for (int i = 0; i < indices.Length; i += 6) { indices[i] = index; indices[i + 1] = index + 1; indices[i + 2] = index + 2; indices[i + 3] = index + 1; indices[i + 4] = index + 3; indices[i + 5] = index + 2; index += 4; } _vertices = _graphics.Buffers.CreateVertexBuffer("Vertex", new GorgonBufferSettings() { SizeInBytes = 4 * vertexSize * Count, Usage = BufferUsage.Dynamic }); _index = _graphics.Buffers.CreateIndexBuffer("Index", indices, BufferUsage.Immutable); _texture = _graphics.Textures.FromFile <GorgonTexture2D>("Balls", @"..\..\..\..\Resources\BallDemo\BallDemo.png", new GorgonCodecPNG()); _texture2 = _graphics.Textures.FromFile <GorgonTexture2D>("VBBack", @"..\..\..\..\Resources\Images\VBback.jpg", new GorgonCodecJPEG()); var matrix = new MatrixBuffer(); _aspect = _swap.Settings.VideoMode.Width / (float)(_swap.Settings.VideoMode.Height); matrix.Projection = Matrix.PerspectiveFovLH(100.39f.Radians(), _aspect, 0.1f, 1000.0f); matrix.View = Matrix.LookAtLH(new Vector3(0, 0, _camPos), new Vector3(0, 0, 1.0f), Vector3.UnitY); matrix.Array = new Vector4[3]; matrix.Array[0] = new Vector4(1.0f, 1.0f, 1.0f, 1.0f); matrix.Array[1] = new Vector4(1.0f, 1.0f, 1.0f, 1.0f); matrix.Array[2] = new Vector4(1.0f, 1.0f, 1.0f, 1.0f); matrix.valueType = new TEMPGUY { value2 = matrix.View, tempArray = new Vector4[3] }; _depthStateAlpha.IsDepthEnabled = false; _depthStateAlpha.IsDepthWriteEnabled = false; _graphics.Input.Layout = _layout; _graphics.Shaders.VertexShader.Current = _vs; _graphics.Shaders.PixelShader.Current = _ps; _graphics.Shaders.PixelShader.TextureSamplers.SetRange(0, new[] { GorgonTextureSamplerStates.LinearFilter, GorgonTextureSamplerStates.LinearFilter }); _graphics.Rasterizer.SetViewport(_swap.Viewport); _graphics.Output.DepthStencilState.States = _depthStateAlpha; _graphics.Output.SetRenderTarget(_swap, _swap.DepthStencilBuffer); _graphics.Input.VertexBuffers[0] = new GorgonVertexBufferBinding(_vertices, vertexSize); _graphics.Input.IndexBuffer = _index; _graphics.Shaders.PixelShader.Resources.SetRange(0, new GorgonShaderView[] { _texture, _texture2 }); _graphics.Output.BlendingState.States = GorgonBlendStates.ModulatedBlending; pvw = matrix.valueType.value2 * matrix.Projection; _tempStream = new GorgonDataStream(_sprite.Length * vertexSize); }