private static void InitializeRuntime(GameStartInfo gameStartInfo) { if (gameStartInfo.Adapter == null) { var adapters = GpuAdapter.EnumerateGraphicsAdapter(); LogEmitter.Assert(adapters.Count > 0, LogLevel.Error, "[Initialize Graphics Device Failed without Support Adapter] from [GameSystems]"); GpuDevice = new GpuDevice(adapters[0]); } else { GpuDevice = new GpuDevice(gameStartInfo.Adapter); } EngineWindow = new EngineWindow( gameStartInfo.WindowName, gameStartInfo.IconName, gameStartInfo.WindowSize); EngineWindow.Show(); PresentRender = new PresentRender(GpuDevice, EngineWindow.Handle, EngineWindow.Size); //init resize event EngineWindow.OnSizeChangeEvent += (sender, eventArg) => { PresentRender.ReSize(eventArg.After); VisualGuiSystem.Area = new Rectangle <int>(0, 0, eventArg.After.Width, eventArg.After.Height); }; }
public FontClass(string name, byte[] data, GpuDevice device) { Name = name; mFontData = data; mGpuDevice = device; mFonts = new Dictionary <int, Font>(); }
public void TestKernelNoTile() { var blockCount = new d3(4); var threadCount = new d3(2); var arrayOut = new GpuArrayWrite <int>(blockCount.Volume() * threadCount.Volume()); GpuDevice.RunGpuKernel( blockCount, threadCount, TestKernels.IntKernelWrap(arrayOut, blockCount, threadCount)); }
public Image(Size <int> size, PixelFormat pixelFormat, GpuDevice device) { mDevice = device; //do not need to create texture, because the size is zero if (size.Width == 0 || size.Height == 0) { return; } mTexture = new GpuTexture2D(size, (GpuPixelFormat)pixelFormat, device, new GpuResourceInfo(GpuBindUsage.ShaderResource | GpuBindUsage.RenderTarget)); }
public VisualGuiSystem(GpuDevice device, Rectangle <int> area) : base("VisualGuiSystem") { RequireComponents.AddRequireComponentType <TransformGuiComponent>(); RequireComponents.AddRequireComponentType <VisualGuiComponent>(); Area = area; mRender = new GuiRender(device); mCanvas = new Image( new Size <int>(Area.Right - Area.Left, Area.Bottom - Area.Top), PixelFormat.RedBlueGreenAlpha8bit, mRender.Device); }
public BezierRender(GpuDevice device) { //bezier render is a simple render for rendering quadratic bezier curve 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 rasterizer state mRasterizerState = new GpuRasterizerState(mDevice, GpuFillMode.Solid, GpuCullMode.None); //initalize render component InitializeFillComponent(); InitializeDrawComponent(); //init input layout //Position : float3 //Texcoord : float2 //Color : float4 mInputLayout = new GpuInputLayout(mDevice, new InputElement[] { new InputElement("POSITION", 0, 12), new InputElement("TEXCOORD", 0, 8), new InputElement("COLOR", 0, 16) }, mFillBezierVertexShader); //init constant buffer mTransformBuffer = new GpuBuffer( Utility.SizeOf <TransformMatrix>(), Utility.SizeOf <TransformMatrix>(), mDevice, GpuResourceInfo.ConstantBuffer()); MSAAStatus = true; }
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 mTransforms = new Stack <Matrix4x4>(); //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.GuiRenderShader, "vs_main")); //init pixel shader, we will choose the best pixel shader for different draw command mPixelShader = new GpuPixelShader(mDevice, GpuPixelShader.Compile(Properties.Resources.GuiRenderShader, "ps_main")); //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); InitializeSquareBuffer(); InitializeConstantBuffer(); InitializeRectangleBuffer(); }
private static void InitializeRuntime(GameStartInfo gameStartInfo) { if (gameStartInfo.Adapter == null) { var adapters = GpuAdapter.EnumerateGraphicsAdapter(); LogEmitter.Assert(adapters.Count > 0, LogLevel.Error, "[Initialize Graphics Device Failed without Support Adapter] from [GameSystems]"); GpuDevice = new GpuDevice(adapters[0]); } else { GpuDevice = new GpuDevice(gameStartInfo.Adapter); } EngineWindow = new EngineWindow( gameStartInfo.Window.Name, gameStartInfo.Window.Icon, gameStartInfo.Window.Size); EngineWindow.Show(); PresentRender = new PresentRender(GpuDevice, EngineWindow.Handle, EngineWindow.Size); }
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 Configure(GpuDevice device, Settings settings, GpuBindGroupLayout bindGroupLayout, GpuBindGroupLayout dynamicBindGroupLayout, GpuBindGroupLayout timeBindGroupLayout, GpuRenderPipeline pipeline, GpuRenderPipeline dynamicPipeline, GpuBuffer vertexBuffer, GpuTextureFormat swapChainFormat) { Device = device; Settings = settings; Pipeline = pipeline; DynamicPipeline = dynamicPipeline; VertexBuffer = vertexBuffer; SwapChainFormat = swapChainFormat; UniformBuffer = Device.CreateBuffer(new GpuBufferDescriptor(Settings.NumTriangles * AlignedUniformBytes + sizeof(float), GpuBufferUsageFlags.Uniform | GpuBufferUsageFlags.CopyDst)); var uniformCpuBuffer = new Windows.Storage.Streams.Buffer(Settings.NumTriangles * AlignedUniformBytes) { Length = Settings.NumTriangles * AlignedUniformBytes }; using (var uniformCpuStream = uniformCpuBuffer.AsStream()) using (var uniformCpuWriter = new BinaryWriter(uniformCpuStream)) { var rand = new Random(); for (var i = 0; i < Settings.NumTriangles; ++i) { uniformCpuWriter.Seek((int)(i * AlignedUniformBytes), SeekOrigin.Begin); float scale = (float)(rand.NextDouble() * 0.2 + 0.2); //scale = 5; float offsetX = (float)(0.9 * 2 * (rand.NextDouble() - 0.5)); float offsetY = (float)(0.9 * 2 * (rand.NextDouble() - 0.5)); float scalar = (float)(rand.NextDouble() * 1.5 + 0.5); float scalarOffset = (float)(rand.NextDouble() * 10); uniformCpuWriter.Write(scale); //Scale uniformCpuWriter.Write(offsetX); //offsetX uniformCpuWriter.Write(offsetY); //offsetY uniformCpuWriter.Write(scalar); //scalar uniformCpuWriter.Write(scalarOffset); //scalar offset } } BindGroups = new GpuBindGroup[Settings.NumTriangles]; for (var i = 0; i < Settings.NumTriangles; ++i) { BindGroups[i] = Device.CreateBindGroup(new GpuBindGroupDescriptor(bindGroupLayout, new GpuBindGroupEntry[] { new GpuBindGroupEntry(0, new GpuBufferBinding(UniformBuffer, 6 * sizeof(float)) { Offset = (UInt64)(i * AlignedUniformBytes) }) })); } DynamicBindGroup = Device.CreateBindGroup(new GpuBindGroupDescriptor(dynamicBindGroupLayout, new GpuBindGroupEntry[] { new GpuBindGroupEntry(0, new GpuBufferBinding(UniformBuffer, 6 * sizeof(float))) })); TimeBindGroup = Device.CreateBindGroup(new GpuBindGroupDescriptor(timeBindGroupLayout, new GpuBindGroupEntry[] { new GpuBindGroupEntry(0, new GpuBufferBinding(UniformBuffer, sizeof(float)) { Offset = TimeOffset }) })); Device.DefaultQueue.WriteBuffer(UniformBuffer, 0, uniformCpuBuffer); var renderBundleEncoder = Device.CreateRenderBundleEncoder(new GpuRenderBundleEncoderDescriptor(new GpuTextureFormat[] { SwapChainFormat })); RecordRenderPass(renderBundleEncoder); RenderBundle = renderBundleEncoder.Finish(); UniformTimeCpuBuffer = new Windows.Storage.Streams.Buffer(sizeof(float)) { Length = sizeof(float) }; }
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); }