public void Draw(Image texture, Rectangle area, float opacity = 1.0f) { //init the data we need uo upload Transform transform = new Transform(); RenderConfig renderConfig = new RenderConfig(); transform.World = Matrix4x4.CreateTranslation(new System.Numerics.Vector3(area.Left, area.Top, 0)); transform.World = Matrix4x4.CreateScale(new System.Numerics.Vector3(area.Right - area.Left, area.Bottom - area.Top, 1)) * transform.World; transform.Projection = Matrix4x4.CreateOrthographicOffCenter(0, mSwapChain.Size.Width, 0, mSwapChain.Size.Height, 0, 1); renderConfig.Opacity = new System.Numerics.Vector4(opacity); //upload it to gpu memory mTransformBuffer.Update(transform); mRenderConfigBuffer.Update(renderConfig); //set resource to shader mDevice.SetBuffer(mTransformBuffer, mTransformBufferSlot, GpuShaderType.VertexShader); mDevice.SetBuffer(mRenderConfigBuffer, mRenderConfigBufferSlot, GpuShaderType.PixelShader); mDevice.SetSamplerState(mGpuSamplerState, mSamplerStateSlot, GpuShaderType.PixelShader); mDevice.SetResourceUsage(texture.GpuResourceUsage, mTextureSlot, GpuShaderType.PixelShader); //draw mDevice.DrawIndexed(6, 0, 0); }
private void DrawImage(Rectanglef rectangle, Image image, Colorf color, GuiRenderMode mode) { //fill rectangle with texture //the result color's alpha is equal texture.alpha * opacity //do not render image if (image.GpuTexture == null) { return; } var transform = new GuiTransform(); var renderConfig = new GuiRenderConfig() { Color = color, Config = new Vector4((int)mode) }; //1.scale the rectangle transform.World = Matrix4x4.CreateScale(rectangle.Right - rectangle.Left, rectangle.Bottom - rectangle.Top, 1.0f); //2.translate it transform.World *= Matrix4x4.CreateTranslation(rectangle.Left, rectangle.Top, 0.0f); //3.keep transform matrix data transform.World *= Transform; //set projection matrix transform.Project = mProject; mTransformBuffer.Update(transform); mRenderConfigBuffer.Update(renderConfig); mDevice.SetVertexBuffer(mSquareVertexBuffer); mDevice.SetIndexBuffer(mSquareIndexBuffer); mDevice.SetBuffer(mTransformBuffer, mTransformBufferSlot, GpuShaderType.All); mDevice.SetBuffer(mRenderConfigBuffer, mRenderConfigBufferSlot, GpuShaderType.All); mDevice.SetResourceUsage(image.GpuResourceUsage, mTextureSlot, GpuShaderType.All); mDevice.DrawIndexed(6, 0, 0); }
public virtual void DrawImage(Rectangle <float> rectangle, Image image, Color <float> color) { //fill rectangle with texture //the result color's alpha is equal texture.alpha * opacity MatrixData matrixData = new MatrixData(); RenderConfig renderConfig = new RenderConfig() { Color = color }; //1.scale the rectangle matrixData.World = Matrix4x4.CreateScale(rectangle.Right - rectangle.Left, rectangle.Bottom - rectangle.Top, 1.0f); //2.translate it matrixData.World *= Matrix4x4.CreateTranslation(rectangle.Left, rectangle.Top, 0.0f); //3.keep transform matrix data matrixData.World *= Transform; //set projection matrix matrixData.Project = mProject; mMatrixDataBuffer.Update(matrixData); mRenderConfigBuffer.Update(renderConfig); mDevice.SetPixelShader(mTexturePixelShader); mDevice.SetVertexBuffer(mSquareVertexBuffer); mDevice.SetIndexBuffer(mSquareIndexBuffer); mDevice.SetSamplerState(mSamplerState, mSamplerStateSlot, GpuShaderType.PixelShader); mDevice.SetBuffer(mMatrixDataBuffer, mMatrixDataBufferSlot, GpuShaderType.VertexShader); mDevice.SetBuffer(mRenderConfigBuffer, mRenderConfigBufferSlot, GpuShaderType.PixelShader); mDevice.SetResourceUsage(image.GpuResourceUsage, mTextureSlot, GpuShaderType.PixelShader); mDevice.DrawIndexed(6, 0, 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); }