public override void Initialize(Application application) { ExceptionUtilities.ThrowIfNull(application, nameof(application)); var windowProvider = application.GetService <WindowProvider>(); _window = windowProvider.CreateWindow(); _window.Show(); var graphicsProvider = application.GetService <GraphicsProvider>(); var graphicsAdapter = graphicsProvider.GraphicsAdapters.First(); var graphicsDevice = graphicsAdapter.CreateGraphicsDevice(_window, graphicsContextCount: 2); _graphicsDevice = graphicsDevice; _graphicsDeviceHeap = graphicsDevice.CreateGraphicsHeap(64 * 1024 * 5, GraphicsHeapCpuAccess.None); using (var graphicsStagingHeap = graphicsDevice.CreateGraphicsHeap(64 * 1024 * 5, GraphicsHeapCpuAccess.Write)) using (var vertexStagingBuffer = graphicsStagingHeap.CreateGraphicsBuffer(GraphicsBufferKind.Staging, 64 * 1024, sizeof(byte))) using (var textureStagingBuffer = graphicsStagingHeap.CreateGraphicsBuffer(GraphicsBufferKind.Staging, 64 * 1024 * 4, sizeof(byte))) { var currentGraphicsContext = graphicsDevice.CurrentGraphicsContext; currentGraphicsContext.BeginFrame(); _trianglePrimitive = CreateTrianglePrimitive(currentGraphicsContext, vertexStagingBuffer, textureStagingBuffer); currentGraphicsContext.EndFrame(); graphicsDevice.Signal(currentGraphicsContext.GraphicsFence); graphicsDevice.WaitForIdle(); } base.Initialize(application); }
static GraphicsTexture CreateTexture2D(GraphicsContext graphicsContext, GraphicsHeap graphicsHeap, GraphicsBuffer textureStagingBuffer) { const uint TextureWidth = 256; const uint TextureHeight = 256; const uint TextureSize = TextureWidth * TextureHeight; const uint CellWidth = TextureWidth / 8; const uint CellHeight = TextureHeight / 8; var texture2D = graphicsHeap.CreateGraphicsTexture(GraphicsTextureKind.TwoDimensional, TextureWidth, TextureHeight); var pTextureData = textureStagingBuffer.Map <uint>(); for (uint n = 0; n < TextureSize; n++) { var x = n % TextureWidth; var y = n / TextureWidth; if ((x / CellWidth % 2) == (y / CellHeight % 2)) { pTextureData[n] = 0xFF000000; } else { pTextureData[n] = 0xFFFFFFFF; } } textureStagingBuffer.Unmap(0..(int)TextureSize); graphicsContext.Copy(texture2D, textureStagingBuffer); return(texture2D); }
static GraphicsBuffer CreateConstantBuffer(GraphicsHeap graphicsHeap) { var constantBuffer = graphicsHeap.CreateGraphicsBuffer(GraphicsBufferKind.Constant, 256, 64); var pConstantBuffer = constantBuffer.Map <Matrix4x4>(); pConstantBuffer[0] = Matrix4x4.Identity; constantBuffer.Unmap(0..sizeof(Matrix4x4)); return(constantBuffer); }
static GraphicsBuffer CreateIndexBuffer(GraphicsContext graphicsContext, GraphicsHeap graphicsHeap, GraphicsBuffer indexStagingBuffer) { var indexBuffer = graphicsHeap.CreateGraphicsBuffer(GraphicsBufferKind.Index, sizeof(ushort) * 6, sizeof(ushort)); var pIndexBuffer = indexStagingBuffer.Map <ushort>(); pIndexBuffer[0] = 0; pIndexBuffer[1] = 1; pIndexBuffer[2] = 2; pIndexBuffer[3] = 0; pIndexBuffer[4] = 2; pIndexBuffer[5] = 3; indexStagingBuffer.Unmap(0..(sizeof(ushort) * 6)); graphicsContext.Copy(indexBuffer, indexStagingBuffer); return(indexBuffer); }
static GraphicsBuffer CreateVertexBuffer(GraphicsContext graphicsContext, GraphicsHeap graphicsHeap, GraphicsBuffer vertexStagingBuffer, float aspectRatio) { var vertexBuffer = graphicsHeap.CreateGraphicsBuffer(GraphicsBufferKind.Vertex, (ulong)(sizeof(TextureVertex) * 3), (ulong)sizeof(TextureVertex)); var pVertexBuffer = vertexStagingBuffer.Map <TextureVertex>(); pVertexBuffer[0] = new TextureVertex { Position = new Vector3(0.0f, 0.25f * aspectRatio, 0.0f), UV = new Vector2(0.5f, 1.0f), }; pVertexBuffer[1] = new TextureVertex { Position = new Vector3(0.25f, -0.25f * aspectRatio, 0.0f), UV = new Vector2(1.0f, 0.0f), }; pVertexBuffer[2] = new TextureVertex { Position = new Vector3(-0.25f, -0.25f * aspectRatio, 0.0f), UV = new Vector2(0.0f, 0.0f), }; vertexStagingBuffer.Unmap(0..(sizeof(TextureVertex) * 3)); graphicsContext.Copy(vertexBuffer, vertexStagingBuffer); return(vertexBuffer); }