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 InitializeRectangleBuffer() { //init rectangle index data uint[] rectangleIndices = new uint[] { 0, 4, 1, 1, 4, 5, 0, 3, 4, 3, 7, 4, 3, 6, 7, 2, 6, 3, 2, 1, 6, 1, 5, 6 }; //init rectangle vertex and index buffer mRectangleVertexBuffer = new GpuBuffer( Utility.SizeOf <GuiVertex>() * 8, Utility.SizeOf <GuiVertex>() * 1, mDevice, GpuResourceInfo.VertexBuffer()); mRectangleIndexBuffer = new GpuBuffer( Utility.SizeOf <uint>() * 24, Utility.SizeOf <uint>() * 1, mDevice, GpuResourceInfo.IndexBuffer()); mRectangleIndexBuffer.Update(rectangleIndices); }
private void InitializeDrawComponent() { mDrawBeziersVertexShader = new GpuVertexShader(mDevice, GpuVertexShader.Compile(Properties.Resources.DrawBeziersShader, "vs_main")); mDrawBezierVertexShader = new GpuVertexShader(mDevice, GpuVertexShader.Compile(Properties.Resources.DrawBezierShader, "vs_main")); mDrawBeziersPixelShader = new GpuPixelShader(mDevice, GpuPixelShader.Compile(Properties.Resources.DrawBeziersShader, "ps_main")); mDrawBezierPixelShader = new GpuPixelShader(mDevice, GpuPixelShader.Compile(Properties.Resources.DrawBezierShader, "ps_main")); mEquationBuffer = new GpuBuffer( Utility.SizeOf <Equation>(), Utility.SizeOf <Equation>(), mDevice, GpuResourceInfo.ConstantBuffer()); mDrawVertexBuffer = new GpuBuffer( Utility.SizeOf <Vertex>() * 4, Utility.SizeOf <Vertex>(), mDevice, GpuResourceInfo.VertexBuffer()); mDrawIndexBuffer = new GpuBuffer( Utility.SizeOf <uint>() * 6, Utility.SizeOf <uint>(), mDevice, GpuResourceInfo.IndexBuffer()); var indices = new uint[] { 0, 1, 2, 0, 2, 3 }; mDrawIndexBuffer.Update(indices); }
public virtual void DrawLine(Position <float> start, Position <float> end, Color <float> color, float padding = 2.0f) { //draw line with start and end position //padding means the width of line //color.Alpha means the opacity of line //first, we compute the matrix of world transform MatrixData matrixData = new MatrixData(); Vector2 vector = new Vector2(end.X - start.X, end.Y - start.Y); //1.compute the scale component, x-component is euqal the length of line, y-component is equal the padding matrixData.World = Matrix4x4.CreateScale(vector.Length(), padding, 1.0f); //2.compute the angle of rotate, we only rotate it at the z-axis matrixData.World *= Matrix4x4.CreateRotationZ((float)Math.Atan2(vector.Y, vector.X), new Vector3(0, padding * 0.5f, 0)); //3.compute the translation, the position of start, but the y is start.y - padding * 0.5f matrixData.World *= Matrix4x4.CreateTranslation(new Vector3(start.X, start.Y - padding * 0.5f, 0)); //4.keep transform matrix data matrixData.World *= Transform; //set project matrix matrixData.Project = mProject; //second, we set the render config RenderConfig renderConfig = new RenderConfig() { Color = color }; //update buffer mMatrixDataBuffer.Update(matrixData); mRenderConfigBuffer.Update(renderConfig); //set buffer and shader mDevice.SetPixelShader(mColorPixelShader); mDevice.SetVertexBuffer(mSquareVertexBuffer); mDevice.SetIndexBuffer(mSquareIndexBuffer); mDevice.SetBuffer(mMatrixDataBuffer, mMatrixDataBufferSlot, GpuShaderType.VertexShader); mDevice.SetBuffer(mRenderConfigBuffer, mRenderConfigBufferSlot, 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); }
private void InitializeSquareBuffer() { //init render object vertex buffer and index buffer //init square vertex data GuiVertex[] squareVertices = new GuiVertex[] { new GuiVertex() { Position = new Vector3f(0, 0, 0), Texcoord = new Vector2f(0, 0) }, new GuiVertex() { Position = new Vector3f(0, 1, 0), Texcoord = new Vector2f(0, 1) }, new GuiVertex() { Position = new Vector3f(1, 1, 0), Texcoord = new Vector2f(1, 1) }, new GuiVertex() { Position = new Vector3f(1, 0, 0), Texcoord = new Vector2f(1, 0) } }; //init square index data uint[] squareIndices = new uint[] { 0, 1, 2, 0, 2, 3 }; //init square buffer and update mSquareVertexBuffer = new GpuBuffer( Utility.SizeOf <GuiVertex>() * squareVertices.Length, Utility.SizeOf <GuiVertex>() * 1, mDevice, GpuResourceInfo.VertexBuffer()); mSquareIndexBuffer = new GpuBuffer( Utility.SizeOf <uint>() * squareIndices.Length, Utility.SizeOf <uint>() * 1, mDevice, GpuResourceInfo.IndexBuffer()); mSquareVertexBuffer.Update(squareVertices); mSquareIndexBuffer.Update(squareIndices); }
private void InitializeFillComponent() { //compile vertex and pixel shader to render bezier curve mFillBeziersVertexShader = new GpuVertexShader(mDevice, GpuVertexShader.Compile(Properties.Resources.FillBeziersShader, "vs_main")); mFillBezierVertexShader = new GpuVertexShader(mDevice, GpuVertexShader.Compile(Properties.Resources.FillBezierShader, "vs_main")); mFillBeziersPixelShader = new GpuPixelShader(mDevice, GpuPixelShader.Compile(Properties.Resources.FillBeziersShader, "ps_main")); mFillBezierPixelShader = new GpuPixelShader(mDevice, GpuPixelShader.Compile(Properties.Resources.FillBezierShader, "ps_main")); //init vertex and index buffer //vertex data will be made when we draw bezier curve mFillVertexBuffer = new GpuBuffer( Utility.SizeOf <Vertex>() * 3, Utility.SizeOf <Vertex>() * 1, mDevice, GpuResourceInfo.VertexBuffer()); mFillIndexBuffer = new GpuBuffer( Utility.SizeOf <uint>() * 3, Utility.SizeOf <uint>() * 1, mDevice, GpuResourceInfo.IndexBuffer()); uint[] indices = new uint[] { 0, 1, 2 }; mFillIndexBuffer.Update(indices); mTrianglePointsBuffer = new GpuBuffer( Utility.SizeOf <TrianglePoints>(), Utility.SizeOf <TrianglePoints>(), mDevice, GpuResourceInfo.ConstantBuffer()); mTriangleColorsBuffer = new GpuBuffer( Utility.SizeOf <TriangleColors>(), Utility.SizeOf <TriangleColors>(), mDevice, GpuResourceInfo.ConstantBuffer()); }
public virtual void DrawRectangle(Rectanglef rectangle, Colorf color, float padding = 2.0f) { //draw rectangle with color //padding means the width of edge //color.Alpha means the opacity of edge //read rectangle data var outSide = rectangle; //inside rectangle will smaller than outside var inSide = new Rectanglef( outSide.Left + padding, outSide.Top + padding, outSide.Right - padding, outSide.Bottom - padding); //first, we need compute the vertex buffer for our rectangle //we do not need tex coord of vertex //we can compute the index buffer at init the render GuiVertex[] vertics = new GuiVertex[] { new GuiVertex() { Position = new Vector3f(outSide.Left, outSide.Top, 0) }, new GuiVertex() { Position = new Vector3f(outSide.Right, outSide.Top, 0) }, new GuiVertex() { Position = new Vector3f(outSide.Right, outSide.Bottom, 0) }, new GuiVertex() { Position = new Vector3f(outSide.Left, outSide.Bottom, 0) }, new GuiVertex() { Position = new Vector3f(inSide.Left, inSide.Top, 0) }, new GuiVertex() { Position = new Vector3f(inSide.Right, inSide.Top, 0) }, new GuiVertex() { Position = new Vector3f(inSide.Right, inSide.Bottom, 0) }, new GuiVertex() { Position = new Vector3f(inSide.Left, inSide.Bottom, 0) } }; //update vertex buffer mRectangleVertexBuffer.Update(vertics); //second, update constant buffer var transform = new GuiTransform() { World = Transform, Project = mProject }; var renderConfig = new GuiRenderConfig() { Color = color, Config = new Vector4(0) }; mTransformBuffer.Update(transform); mRenderConfigBuffer.Update(renderConfig); //set buffer and shader mDevice.SetVertexBuffer(mRectangleVertexBuffer); mDevice.SetIndexBuffer(mRectangleIndexBuffer); mDevice.SetBuffer(mTransformBuffer, mTransformBufferSlot, GpuShaderType.All); mDevice.SetBuffer(mRenderConfigBuffer, mRenderConfigBufferSlot, GpuShaderType.All); //draw mDevice.DrawIndexed(24, 0, 0); }
public GuiRender(GpuDevice device) { //gui render is a simple render to render gui object //gui render can provide some simple object draw function //we can replace it to our render and we can use it in the gui system render function mDevice = device; //default transform is I Transform = Matrix4x4.Identity; //init blend state mBlendState = new GpuBlendState(mDevice, new RenderTargetBlendDescription() { AlphaBlendOperation = GpuBlendOperation.Add, BlendOperation = GpuBlendOperation.Add, DestinationAlphaBlend = GpuBlendOption.InverseSourceAlpha, DestinationBlend = GpuBlendOption.InverseSourceAlpha, SourceAlphaBlend = GpuBlendOption.SourceAlpha, SourceBlend = GpuBlendOption.SourceAlpha, IsBlendEnable = true }); //init vertex shader, for all draw command we use same vertex shader mVertexShader = new GpuVertexShader(mDevice, GpuVertexShader.Compile(Properties.Resources.GuiRenderCommonVertexShader)); //init pixel shader, we will choose the best pixel shader for different draw command mColorPixelShader = new GpuPixelShader(mDevice, GpuPixelShader.Compile(Properties.Resources.GuiRenderColorPixelShader)); mTexturePixelShader = new GpuPixelShader(mDevice, GpuPixelShader.Compile(Properties.Resources.GuiRenderTexturePixelShader)); //init sampler state mSamplerState = new GpuSamplerState(mDevice); //init input layout //Position : float3 //Texcoord : float2 mInputLayout = new GpuInputLayout(mDevice, new InputElement[] { new InputElement("POSITION", 0, 12), new InputElement("TEXCOORD", 0, 8) }, mVertexShader); //init render object vertex buffer and index buffer //init square vertex data Vertex[] squareVertices = new Vertex[] { new Vertex() { Position = new Vector3(0, 0, 0), TexCoord = new Vector2(0, 0) }, new Vertex() { Position = new Vector3(0, 1, 0), TexCoord = new Vector2(0, 1) }, new Vertex() { Position = new Vector3(1, 1, 0), TexCoord = new Vector2(1, 1) }, new Vertex() { Position = new Vector3(1, 0, 0), TexCoord = new Vector2(1, 0) } }; //init square index data uint[] squareIndices = new uint[] { 0, 1, 2, 0, 2, 3 }; //init square buffer and update mSquareVertexBuffer = new GpuBuffer( Utility.SizeOf <Vertex>() * squareVertices.Length, Utility.SizeOf <Vertex>() * 1, mDevice, GpuResourceInfo.VertexBuffer()); mSquareIndexBuffer = new GpuBuffer( Utility.SizeOf <uint>() * squareIndices.Length, Utility.SizeOf <uint>() * 1, mDevice, GpuResourceInfo.IndexBuffer()); mSquareVertexBuffer.Update(squareVertices); mSquareIndexBuffer.Update(squareIndices); //init rectangle index data uint[] rectangleIndices = new uint[] { 0, 4, 1, 1, 4, 5, 0, 3, 4, 3, 7, 4, 3, 6, 7, 2, 6, 3, 2, 1, 6, 1, 5, 6 }; //init rectangle vertex and index buffer mRectangleVertexBuffer = new GpuBuffer( Utility.SizeOf <Vertex>() * 8, Utility.SizeOf <Vertex>() * 1, mDevice, GpuResourceInfo.VertexBuffer()); mRectangleIndexBuffer = new GpuBuffer( Utility.SizeOf <uint>() * 24, Utility.SizeOf <uint>() * 1, mDevice, GpuResourceInfo.IndexBuffer()); mRectangleIndexBuffer.Update(rectangleIndices); //init shader buffer mMatrixDataBuffer = new GpuBuffer( Utility.SizeOf <MatrixData>(), Utility.SizeOf <MatrixData>(), mDevice, GpuResourceInfo.ConstantBuffer()); mRenderConfigBuffer = new GpuBuffer( Utility.SizeOf <RenderConfig>(), Utility.SizeOf <RenderConfig>(), mDevice, GpuResourceInfo.ConstantBuffer()); }
public virtual void DrawRectangle(Rectangle <float> rectangle, Color <float> color, float padding = 2.0f) { //draw rectangle with color //padding means the width of edge //color.Alpha means the opacity of edge //read rectangle data Rectangle <float> outSide = rectangle; //inside rectangle will smaller than outside Rectangle <float> inSide = new Rectangle <float>( outSide.Left + padding, outSide.Top + padding, outSide.Right - padding, outSide.Bottom - padding); //first, we need compute the vertex buffer for our rectangle //we do not need tex coord of vertex //we can compute the index buffer at init the render Vertex[] vertics = new Vertex[] { new Vertex() { Position = new Vector3(outSide.Left, outSide.Top, 0) }, new Vertex() { Position = new Vector3(outSide.Right, outSide.Top, 0) }, new Vertex() { Position = new Vector3(outSide.Right, outSide.Bottom, 0) }, new Vertex() { Position = new Vector3(outSide.Left, outSide.Bottom, 0) }, new Vertex() { Position = new Vector3(inSide.Left, inSide.Top, 0) }, new Vertex() { Position = new Vector3(inSide.Right, inSide.Top, 0) }, new Vertex() { Position = new Vector3(inSide.Right, inSide.Bottom, 0) }, new Vertex() { Position = new Vector3(inSide.Left, inSide.Bottom, 0) } }; //update vertex buffer mRectangleVertexBuffer.Update(vertics); //second, update constant buffer MatrixData matrixData = new MatrixData() { World = Transform, Project = mProject }; RenderConfig renderConfig = new RenderConfig() { Color = color }; mMatrixDataBuffer.Update(matrixData); mRenderConfigBuffer.Update(renderConfig); //set buffer and shader mDevice.SetPixelShader(mColorPixelShader); mDevice.SetVertexBuffer(mRectangleVertexBuffer); mDevice.SetIndexBuffer(mRectangleIndexBuffer); mDevice.SetBuffer(mMatrixDataBuffer, mMatrixDataBufferSlot, GpuShaderType.VertexShader); mDevice.SetBuffer(mRenderConfigBuffer, mRenderConfigBufferSlot, GpuShaderType.PixelShader); //draw mDevice.DrawIndexed(24, 0, 0); }
public PresentRender(GpuDevice device, IntPtr handle, Size size) { mHandle = handle; mDevice = device; mSwapChain = new GpuSwapChain(handle, size, GpuPixelFormat.R8G8B8A8Unknown, mDevice); mBlendState = new GpuBlendState(mDevice, new RenderTargetBlendDescription() { AlphaBlendOperation = GpuBlendOperation.Add, BlendOperation = GpuBlendOperation.Add, DestinationAlphaBlend = GpuBlendOption.InverseSourceAlpha, DestinationBlend = GpuBlendOption.InverseSourceAlpha, SourceAlphaBlend = GpuBlendOption.SourceAlpha, SourceBlend = GpuBlendOption.SourceAlpha, IsBlendEnable = true }); //compile shader mVertexShader = new GpuVertexShader(mDevice, GpuVertexShader.Compile(Properties.Resources.PresentVertexShader)); mDrawPixelShader = new GpuPixelShader(mDevice, GpuPixelShader.Compile(Properties.Resources.PresentDrawPixelShader)); mMaskPixelShader = new GpuPixelShader(mDevice, GpuPixelShader.Compile(Properties.Resources.PresentMaskPixelShader)); //create input layout, we only need to render texture mInputLayout = new GpuInputLayout(mDevice, new InputElement[] { new InputElement("POSITION", 0, 12), new InputElement("TEXCOORD", 0, 8) }, mVertexShader); //init vertex and index data Vertex[] vertices = new Vertex[] { new Vertex() { Position = new System.Numerics.Vector3(0, 0, 0), TexCoord = new System.Numerics.Vector2(0, 0) }, new Vertex() { Position = new System.Numerics.Vector3(0, 1, 0), TexCoord = new System.Numerics.Vector2(0, 1) }, new Vertex() { Position = new System.Numerics.Vector3(1, 1, 0), TexCoord = new System.Numerics.Vector2(1, 1) }, new Vertex() { Position = new System.Numerics.Vector3(1, 0, 0), TexCoord = new System.Numerics.Vector2(1, 0) } }; uint[] indices = new uint[] { 0, 1, 2, 2, 3, 0 }; //create vertex and index buffer mVertexBuffer = new GpuBuffer( Utility.SizeOf <Vertex>() * vertices.Length, Utility.SizeOf <Vertex>(), mDevice, GpuResourceInfo.VertexBuffer()); mIndexBuffer = new GpuBuffer( Utility.SizeOf <uint>() * indices.Length, Utility.SizeOf <uint>(), mDevice, GpuResourceInfo.IndexBuffer()); mVertexBuffer.Update(vertices); mIndexBuffer.Update(indices); //create constant buffer //transform buffer is used for vertex shader to do transform //render config buffer is used for pixel shader to render with opacity mTransformBuffer = new GpuBuffer( Utility.SizeOf <Transform>(), Utility.SizeOf <Transform>(), mDevice, GpuResourceInfo.ConstantBuffer()); mRenderConfigBuffer = new GpuBuffer( Utility.SizeOf <RenderConfig>(), Utility.SizeOf <RenderConfig>(), mDevice, GpuResourceInfo.ConstantBuffer()); mGpuSamplerState = new GpuSamplerState(mDevice); }
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); }