private void CreateConstantBuffer() { triangleConstantBuffers = new ID3D12Resource[3]; TriangleColors[] bufferData = new TriangleColors[] { new TriangleColors() { A = new Vector4(0.0f, 0.2f, 1.0f, 1.0f), B = new Vector4(0.0f, 0.2f, 1.0f, 1.0f), C = new Vector4(0.0f, 0.2f, 1.0f, 1.0f), }, new TriangleColors() { A = new Vector4(1.0f, 1.0f, 0.0f, 1.0f), B = new Vector4(0.0f, 1.0f, 0.0f, 1.0f), C = new Vector4(0.0f, 1.0f, 1.0f, 1.0f), }, new TriangleColors() { A = new Vector4(1.0f, 1.0f, 0.0f, 1.0f), B = new Vector4(1.0f, 0.0f, 0.0f, 1.0f), C = new Vector4(1.0f, 0.0f, 1.0f, 1.0f), }, }; HeapProperties upload_properties = new HeapProperties(HeapType.Upload, CpuPageProperty.Unknown, MemoryPool.Unknown, 0, 0); for (int i = 0; i < triangleConstantBuffers.Length; i++) { triangleConstantBuffers[i] = CreateBuffer(device, Unsafe.SizeOf <TriangleColors>(), ResourceFlags.None, ResourceStates.GenericRead, upload_properties); IntPtr data = triangleConstantBuffers[i].Map(0); Helpers.CopyMemory <TriangleColors>(data, new ReadOnlySpan <TriangleColors>(bufferData).Slice(i, 1)); triangleConstantBuffers[i].Unmap(0); } }
public void FillBeziers(int count, Position <float>[] controls, Color <float>[] colors, Matrix4x4[] transforms) { Utility.Assert(controls.Length >= count * 3 && colors.Length >= count * 3); Utility.Assert(Mode == RenderMode.Fill); if (count == 0) { return; } TrianglePoints[] trianglePointsCanvas = new TrianglePoints[count]; TrianglePoints[] trianglePoints = new TrianglePoints[count]; TriangleColors[] triangleColors = new TriangleColors[count]; var size = new Vector4(mCanvasSize.Width * 0.5f, mCanvasSize.Height * 0.5f, 0, 0); //create triangle points struct for (int index = 0; index < count; index++) { //get transform matrix var transformMatrix = (transforms == null ? Matrix4x4.Identity : transforms[index]) * Transform * mProjection; //get control points for bezier_i var points = new Position <float>[] { controls[index * 3 + 0], controls[index * 3 + 1], controls[index * 3 + 2] }; Vector4 FromColor(Color <float> color) => new Vector4(color.Red, color.Green, color.Blue, color.Alpha); //create data for bezier_i in shader TrianglePoints subTrianglePoints = new TrianglePoints() { Position0 = Vector4.Transform(new Vector3(points[0].X, points[0].Y, 0), transformMatrix), Position1 = Vector4.Transform(new Vector3(points[1].X, points[1].Y, 0), transformMatrix), Position2 = Vector4.Transform(new Vector3(points[2].X, points[2].Y, 0), transformMatrix), }; TriangleColors subTriangleColors = new TriangleColors() { Color0 = FromColor(colors[index * 3 + 0]), Color1 = FromColor(colors[index * 3 + 1]), Color2 = FromColor(colors[index * 3 + 2]) }; TrianglePoints subTrianglePointsCanvas = new TrianglePoints() { Position0 = (subTrianglePoints.Position0 + new Vector4(1)) * size, Position1 = (subTrianglePoints.Position1 + new Vector4(1)) * size, Position2 = (subTrianglePoints.Position2 + new Vector4(1)) * size }; subTrianglePointsCanvas.Position0.Y = mCanvasSize.Height - subTrianglePointsCanvas.Position0.Y; subTrianglePointsCanvas.Position1.Y = mCanvasSize.Height - subTrianglePointsCanvas.Position1.Y; subTrianglePointsCanvas.Position2.Y = mCanvasSize.Height - subTrianglePointsCanvas.Position2.Y; trianglePointsCanvas[index] = subTrianglePointsCanvas; trianglePoints[index] = subTrianglePoints; triangleColors[index] = subTriangleColors; } if (mTrianglePointsBufferArray == null || mTrianglePointsBufferArray.ElementCount != count) { Utility.Dispose(ref mTrianglePointsBufferArray); Utility.Dispose(ref mTriangleColorsBufferArray); Utility.Dispose(ref mTrianglePointsCanvasBufferArray); Utility.Dispose(ref mTrianglePointsBufferArrayUsage); Utility.Dispose(ref mTriangleColorsBufferArrayUsage); Utility.Dispose(ref mTrianglePointsCanvasBufferArrayUsage); mTrianglePointsBufferArray = new GpuBufferArray( Utility.SizeOf <TrianglePoints>(), count, mDevice, GpuResourceInfo.BufferArray()); mTriangleColorsBufferArray = new GpuBufferArray( Utility.SizeOf <TrianglePoints>(), count, mDevice, GpuResourceInfo.BufferArray()); mTrianglePointsCanvasBufferArray = new GpuBufferArray( Utility.SizeOf <TrianglePoints>(), count, mDevice, GpuResourceInfo.BufferArray()); mTrianglePointsBufferArrayUsage = new GpuResourceUsage(mDevice, mTrianglePointsBufferArray); mTriangleColorsBufferArrayUsage = new GpuResourceUsage(mDevice, mTriangleColorsBufferArray); mTrianglePointsCanvasBufferArrayUsage = new GpuResourceUsage(mDevice, mTrianglePointsCanvasBufferArray); } mTrianglePointsBufferArray.Update(trianglePoints); mTriangleColorsBufferArray.Update(triangleColors); mTrianglePointsCanvasBufferArray.Update(trianglePointsCanvas); //change shader we use mDevice.SetVertexShader(mFillBeziersVertexShader); mDevice.SetPixelShader(mFillBeziersPixelShader); //set vertex buffer mDevice.SetVertexBuffer(mFillVertexBuffer); mDevice.SetIndexBuffer(mFillIndexBuffer); //set constant buffer and resource mDevice.SetResourceUsage(mTrianglePointsBufferArrayUsage, 0); mDevice.SetResourceUsage(mTriangleColorsBufferArrayUsage, 1); mDevice.SetResourceUsage(mTrianglePointsCanvasBufferArrayUsage, 2); //drwa indexed instanced mDevice.DrawIndexedInstanced(3, count); //reset the shader mDevice.SetVertexShader(mFillBezierVertexShader); mDevice.SetPixelShader(mFillBezierPixelShader); }
public void FillBezier(Position <float>[] controls, Color <float>[] colors) { Utility.Assert(controls.Length >= 3 && colors.Length >= 3); Utility.Assert(Mode == RenderMode.Fill); Vertex[] vertices = new Vertex[3]; for (int i = 0; i < 3; i++) { vertices[i] = new Vertex() { Position = new Vector3(controls[i].X, controls[i].Y, 0), Color = new Vector4(colors[i].Red, colors[i].Green, colors[i].Blue, colors[i].Alpha) }; } vertices[0].TexCoord = new Vector2(0, 0); vertices[1].TexCoord = new Vector2(0.5f, 0); vertices[2].TexCoord = new Vector2(1, 1); TransformMatrix transform = new TransformMatrix() { World = Transform, Projection = mProjection }; var transformMatrix = transform.World * transform.Projection; TrianglePoints trianglePoints = new TrianglePoints() { Position0 = Vector4.Transform(vertices[0].Position, transformMatrix), Position1 = Vector4.Transform(vertices[1].Position, transformMatrix), Position2 = Vector4.Transform(vertices[2].Position, transformMatrix), }; TriangleColors triangleColors = new TriangleColors() { Color0 = vertices[0].Color, Color1 = vertices[1].Color, Color2 = vertices[2].Color }; var size = new Vector4(mCanvasSize.Width * 0.5f, mCanvasSize.Height * 0.5f, 0, 0); trianglePoints.Position0 = (trianglePoints.Position0 + new Vector4(1)) * size; trianglePoints.Position1 = (trianglePoints.Position1 + new Vector4(1)) * size; trianglePoints.Position2 = (trianglePoints.Position2 + new Vector4(1)) * size; trianglePoints.Position0.Y = mCanvasSize.Height - trianglePoints.Position0.Y; trianglePoints.Position1.Y = mCanvasSize.Height - trianglePoints.Position1.Y; trianglePoints.Position2.Y = mCanvasSize.Height - trianglePoints.Position2.Y; //update data to gpu mFillVertexBuffer.Update(vertices); mTransformBuffer.Update(transform); mTrianglePointsBuffer.Update(trianglePoints); mTriangleColorsBuffer.Update(triangleColors); mDevice.SetVertexBuffer(mFillVertexBuffer); mDevice.SetIndexBuffer(mFillIndexBuffer); mDevice.SetBuffer(mTransformBuffer, 0); mDevice.SetBuffer(mTrianglePointsBuffer, 1); mDevice.SetBuffer(mTriangleColorsBuffer, 2); mDevice.DrawIndexed(3); }