async Task Init() { var gpu = new Gpu(); #if DEBUG gpu.EnableD3D12DebugLayer(); #endif Device = await(await gpu.RequestAdapterAsync()).RequestDeviceAsync(); Windows.Storage.Streams.Buffer verticeCpuBuffer = new Windows.Storage.Streams.Buffer((uint)Buffer.ByteLength(Cube.CubeVertexArray)); verticeCpuBuffer.Length = verticeCpuBuffer.Capacity; using (var verticeCpuStream = verticeCpuBuffer.AsStream()) { byte[] vertexBufferBytes = new byte[Buffer.ByteLength(Cube.CubeVertexArray)]; Buffer.BlockCopy(Cube.CubeVertexArray, 0, vertexBufferBytes, 0, Buffer.ByteLength(Cube.CubeVertexArray)); await verticeCpuStream.WriteAsync(vertexBufferBytes, 0, Buffer.ByteLength(Cube.CubeVertexArray)); } VerticesBuffer = Device.CreateBuffer(new GpuBufferDescriptor((ulong)Buffer.ByteLength(Cube.CubeVertexArray), GpuBufferUsageFlags.Vertex) { MappedAtCreation = true }); verticeCpuBuffer.CopyTo(VerticesBuffer.GetMappedRange()); VerticesBuffer.Unmap(); string shaderCode; using (var shaderFileStream = typeof(MainPage).Assembly.GetManifestResourceStream("InstancedCube.shader.hlsl")) using (var shaderStreamReader = new StreamReader(shaderFileStream)) { shaderCode = shaderStreamReader.ReadToEnd(); } var shader = Device.CreateShaderModule(new GpuShaderModuleDescriptor(GpuShaderSourceType.Hlsl, shaderCode)); var vertexState = new GpuVertexState(shader, "VSMain") { VertexBuffers = new GpuVertexBufferLayout[] { new GpuVertexBufferLayout(Cube.CubeVertexSize, new GpuVertexAttribute[] { new GpuVertexAttribute() { ShaderLocation = 0, Format = GpuVertexFormat.Float4, Offset = Cube.CubePositionOffset }, new GpuVertexAttribute() { ShaderLocation = 1, Format = GpuVertexFormat.Float4, Offset = Cube.CubeColorOffset } }) } }; var fragmentState = new GpuFragmentState(shader, "PSMain", new GpuColorTargetState[] { new GpuColorTargetState { Format = GpuTextureFormat.BGRA8UNorm, Blend = null, WriteMask = GpuColorWriteFlags.All } }); var primitiveState = new GpuPrimitiveState { Topology = GpuPrimitiveTopology.TriangleList, FrontFace = GpuFrontFace.Ccw, CullMode = GpuCullMode.Back, StripIndexFormat = null }; var depthState = new GpuDepthStencilState(GpuTextureFormat.Depth24PlusStencil8) { DepthWriteEnabled = true, DepthCompare = GpuCompareFunction.Less, }; var uniformBindGroupLayout = Device.CreateBindGroupLayout(new GpuBindGroupLayoutDescriptor(new GpuBindGroupLayoutEntry[] { new GpuBindGroupLayoutEntry() { Binding = 0, Visibility = GpuShaderStageFlags.Vertex, Buffer = new GpuBufferBindingLayout { Type = GpuBufferBindingType.Uniform, HasDynamicOffset = false, MinBindingSize = UniformBufferSize } } })); var pipelineLayout = Device.CreatePipelineLayout(new GpuPipelineLayoutDescriptor() { BindGroupLayouts = new GpuBindGroupLayout[] { uniformBindGroupLayout } }); Pipeline = Device.CreateRenderPipeline(new GpuRenderPipelineDescriptor(vertexState) { Fragment = fragmentState, Primitive = primitiveState, DepthStencilState = depthState, Layout = pipelineLayout }); UniformBuffer = Device.CreateBuffer(new GpuBufferDescriptor(UniformBufferSize, GpuBufferUsageFlags.Uniform | GpuBufferUsageFlags.CopyDst)); UniformCpuBuffer = new Windows.Storage.Streams.Buffer(UniformBufferSize); UniformCpuBuffer.Length = UniformCpuBuffer.Capacity; UniformBindGroup = Device.CreateBindGroup(new GpuBindGroupDescriptor(uniformBindGroupLayout, new GpuBindGroupEntry[] { new GpuBindGroupEntry(0, new GpuBufferBinding(UniformBuffer, UniformBufferSize)) })); MvpMatrices = new Matrix4x4[NumInstances]; }