async Task Init() { var gpu = new Gpu(); #if DEBUG gpu.EnableD3D12DebugLayer(); #endif var adapter = await gpu.RequestAdapterAsync(); Device = await adapter.RequestDeviceAsync(); TimeBindGroupLayout = Device.CreateBindGroupLayout(new GpuBindGroupLayoutDescriptor(new GpuBindGroupLayoutEntry[] { new GpuBindGroupLayoutEntry() { Binding = 0, Visibility = GpuShaderStageFlags.Vertex, Buffer = new GpuBufferBindingLayout() { Type = GpuBufferBindingType.Uniform, MinBindingSize = sizeof(float) } } })); BindGroupLayout = Device.CreateBindGroupLayout(new GpuBindGroupLayoutDescriptor(new GpuBindGroupLayoutEntry[] { new GpuBindGroupLayoutEntry() { Binding = 0, Visibility = GpuShaderStageFlags.Vertex, Buffer = new GpuBufferBindingLayout() { Type = GpuBufferBindingType.Uniform, MinBindingSize = 20 } } })); DynamicBindGroupLayout = Device.CreateBindGroupLayout(new GpuBindGroupLayoutDescriptor(new GpuBindGroupLayoutEntry[] { new GpuBindGroupLayoutEntry() { Binding = 0, Visibility = GpuShaderStageFlags.Vertex, Buffer = new GpuBufferBindingLayout() { Type = GpuBufferBindingType.Uniform, HasDynamicOffset = true, MinBindingSize = 20 } } })); var pipelineLayout = Device.CreatePipelineLayout(new GpuPipelineLayoutDescriptor() { BindGroupLayouts = new GpuBindGroupLayout[] { TimeBindGroupLayout, BindGroupLayout } }); var dynamicPipelineLayout = Device.CreatePipelineLayout(new GpuPipelineLayoutDescriptor() { BindGroupLayouts = new GpuBindGroupLayout[] { TimeBindGroupLayout, DynamicBindGroupLayout } }); string shaderCode; using (var shaderFileStream = typeof(MainPage).Assembly.GetManifestResourceStream("Animometer.shader.hlsl")) using (var shaderStreamReader = new StreamReader(shaderFileStream)) { shaderCode = shaderStreamReader.ReadToEnd(); } var shader = Device.CreateShaderModule(new GpuShaderModuleDescriptor(GpuShaderSourceType.Hlsl, shaderCode)); var pipelineDescriptor = new GpuRenderPipelineDescriptor(new GpuVertexState(shader, "VSMain") { VertexBuffers = new GpuVertexBufferLayout[] { new GpuVertexBufferLayout(2 * Vec4Size, new GpuVertexAttribute[] { new GpuVertexAttribute() { ShaderLocation = 0, Offset = 0, Format = GpuVertexFormat.Float4 }, new GpuVertexAttribute() { ShaderLocation = 1, Offset = Vec4Size, Format = GpuVertexFormat.Float4 } }) { StepMode = GpuInputStepMode.Vertex } } }) { Fragment = new GpuFragmentState(shader, "PSMain", new GpuColorTargetState[] { new GpuColorTargetState() { Format = SwapChainFormat } }), Primitive = new GpuPrimitiveState() { Topology = GpuPrimitiveTopology.TriangleList, FrontFace = GpuFrontFace.Ccw, CullMode = GpuCullMode.None } }; pipelineDescriptor.Layout = pipelineLayout; Pipeline = Device.CreateRenderPipeline(pipelineDescriptor); pipelineDescriptor.Layout = dynamicPipelineLayout; DynamicPipeline = Device.CreateRenderPipeline(pipelineDescriptor); VertexBuffer = Device.CreateBuffer(new GpuBufferDescriptor(2 * 3 * Vec4Size, GpuBufferUsageFlags.Vertex) { MappedAtCreation = true }); float[] vertexData = new float[] { 0, 0.1f, 0, 1, 1, 0, 0, 1, -0.1f, -0.1f, 0, 1, 0, 1, 0, 1, 0.1f, -0.1f, 0, 1, 0, 0, 1, 1 }; Windows.Storage.Streams.Buffer verticeCpuBuffer = new Windows.Storage.Streams.Buffer((uint)Buffer.ByteLength(vertexData)); verticeCpuBuffer.Length = verticeCpuBuffer.Capacity; using (var verticeCpuStream = verticeCpuBuffer.AsStream()) { byte[] vertexBufferBytes = new byte[Buffer.ByteLength(vertexData)]; Buffer.BlockCopy(vertexData, 0, vertexBufferBytes, 0, Buffer.ByteLength(vertexData)); await verticeCpuStream.WriteAsync(vertexBufferBytes, 0, Buffer.ByteLength(vertexData)); } verticeCpuBuffer.CopyTo(VertexBuffer.GetMappedRange()); VertexBuffer.Unmap(); }
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("RotatingCube.shader.hlsl")) using (var shaderStreamReader = new StreamReader(shaderFileStream)) { shaderCode = shaderStreamReader.ReadToEnd(); } var shader = Device.CreateShaderModule(new GpuShaderModuleDescriptor(GpuShaderSourceType.Hlsl, shaderCode)); //var shader = Device.CreateShaderModule(new GpuShaderModuleDescriptor(GpuShaderSourceType.Hlsl, )) 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(4 * 4 * sizeof(float)); UniformCpuBuffer.Length = UniformCpuBuffer.Capacity; UniformBindGroup = Device.CreateBindGroup(new GpuBindGroupDescriptor(uniformBindGroupLayout, new GpuBindGroupEntry[] { new GpuBindGroupEntry(0, new GpuBufferBinding(UniformBuffer, UniformBufferSize)) })); }
async Task Init() { var adapter = await Gpu.RequestAdapterAsync(); Device = await adapter.RequestDeviceAsync(); GpuShaderModule computeShader; using (var shaderFileStream = typeof(MainWindow).Assembly.GetManifestResourceStream("ComputeBoidsWpf.compute.hlsl")) using (var shaderStreamReader = new StreamReader(shaderFileStream)) { var shaderCode = await shaderStreamReader.ReadToEndAsync(); computeShader = Device.CreateShaderModule(new GpuShaderModuleDescriptor(GpuShaderSourceType.Hlsl, shaderCode)); } GpuShaderModule drawShader; using (var shaderFileStream = typeof(MainWindow).Assembly.GetManifestResourceStream("ComputeBoidsWpf.draw.hlsl")) using (var shaderStreamReader = new StreamReader(shaderFileStream)) { var shaderCode = await shaderStreamReader.ReadToEndAsync(); drawShader = Device.CreateShaderModule(new GpuShaderModuleDescriptor(GpuShaderSourceType.Hlsl, shaderCode)); } RenderPipeline = Device.CreateRenderPipeline(new GpuRenderPipelineDescriptor(new GpuVertexState(drawShader, "VSMain") { VertexBuffers = new GpuVertexBufferLayout[] { new GpuVertexBufferLayout(4 * 4, new GpuVertexAttribute[] { new GpuVertexAttribute() { Format = GpuVertexFormat.Float2, Offset = 0, ShaderLocation = 0 }, new GpuVertexAttribute() { Format = GpuVertexFormat.Float2, Offset = 2 * 4, ShaderLocation = 1 } }) { StepMode = GpuInputStepMode.Instance }, new GpuVertexBufferLayout(2 * 4, new GpuVertexAttribute[] { new GpuVertexAttribute() { Format = GpuVertexFormat.Float2, Offset = 0, ShaderLocation = 2 } }) } }) { Fragment = new GpuFragmentState(drawShader, "PSMain", new GpuColorTargetState[] { new GpuColorTargetState { Format = GpuTextureFormat.BGRA8UNorm, Blend = null, WriteMask = GpuColorWriteFlags.All } }), Primitive = new GpuPrimitiveState { Topology = GpuPrimitiveTopology.TriangleList, CullMode = GpuCullMode.None, FrontFace = GpuFrontFace.Ccw }, DepthStencilState = new GpuDepthStencilState(GpuTextureFormat.Depth24PlusStencil8) { DepthWriteEnabled = true, DepthCompare = GpuCompareFunction.Less, } }); var computeBindGroupLayout = Device.CreateBindGroupLayout(new GpuBindGroupLayoutDescriptor(new GpuBindGroupLayoutEntry[] { new GpuBindGroupLayoutEntry() { Binding = 0, Visibility = GpuShaderStageFlags.Compute, Buffer = new GpuBufferBindingLayout() { Type = GpuBufferBindingType.Uniform, HasDynamicOffset = false, MinBindingSize = (ulong)(SimParamData.Length * sizeof(float)) } }, new GpuBindGroupLayoutEntry() { Binding = 1, Visibility = GpuShaderStageFlags.Compute, Buffer = new GpuBufferBindingLayout() { Type = GpuBufferBindingType.ReadOnlyStorage, HasDynamicOffset = false, MinBindingSize = NumParticles * 16 } }, new GpuBindGroupLayoutEntry() { Binding = 2, Visibility = GpuShaderStageFlags.Compute, Buffer = new GpuBufferBindingLayout() { Type = GpuBufferBindingType.Storage, HasDynamicOffset = false, MinBindingSize = NumParticles * 16 } } })); ComputePipeline = Device.CreateComputePipeline(new GpuComputePipelineDescriptor(new GpuProgrammableStage(computeShader, "main")) { Layout = Device.CreatePipelineLayout(new GpuPipelineLayoutDescriptor() { BindGroupLayouts = new GpuBindGroupLayout[] { computeBindGroupLayout } }), }); VerticesBuffer = Device.CreateBuffer(new GpuBufferDescriptor((ulong)(sizeof(float) * VertexBufferData.Length), GpuBufferUsageFlags.Vertex) { MappedAtCreation = true }); using (var stream = VerticesBuffer.GetMappedRange().AsStream()) using (var binaryWriter = new BinaryWriter(stream)) { for (int i = 0; i < VertexBufferData.Length; ++i) { binaryWriter.Write(VertexBufferData[i]); } } VerticesBuffer.Unmap(); var simParamBuffer = Device.CreateBuffer(new GpuBufferDescriptor((ulong)(sizeof(float) * SimParamData.Length), GpuBufferUsageFlags.Uniform) { MappedAtCreation = true }); using (var stream = simParamBuffer.GetMappedRange().AsStream()) using (var writer = new BinaryWriter(stream)) { for (int i = 0; i < SimParamData.Length; ++i) { writer.Write(SimParamData[i]); } } simParamBuffer.Unmap(); float[] initialParticleData = new float[NumParticles * 4]; Random random = new Random(); for (var i = 0; i < NumParticles; ++i) { initialParticleData[4 * i + 0] = (float)(2 * (random.NextDouble() - 0.5f)); initialParticleData[4 * i + 1] = (float)(2 * (random.NextDouble() - 0.5f)); initialParticleData[4 * i + 2] = (float)(2 * (random.NextDouble() - 0.5f) * 0.1); initialParticleData[4 * i + 3] = (float)(2 * (random.NextDouble() - 0.5f) * 0.1); } Windows.Storage.Streams.Buffer initialParticleDataBuffer = new Windows.Storage.Streams.Buffer((uint)(sizeof(float) * initialParticleData.Length)) { Length = (uint)(sizeof(float) * initialParticleData.Length) }; using (var stream = initialParticleDataBuffer.AsStream()) using (var writer = new BinaryWriter(stream)) { for (int i = 0; i < initialParticleData.Length; ++i) { writer.Write(initialParticleData[i]); } } ParticleBuffers = new GpuBuffer[2]; for (int i = 0; i < 2; ++i) { ParticleBuffers[i] = Device.CreateBuffer(new GpuBufferDescriptor(initialParticleDataBuffer.Length, GpuBufferUsageFlags.Vertex | GpuBufferUsageFlags.Storage) { MappedAtCreation = true }); initialParticleDataBuffer.CopyTo(ParticleBuffers[i].GetMappedRange()); ParticleBuffers[i].Unmap(); } ParticleBindGroups = new GpuBindGroup[2]; for (var i = 0; i < 2; ++i) { ParticleBindGroups[i] = Device.CreateBindGroup(new GpuBindGroupDescriptor(computeBindGroupLayout, new GpuBindGroupEntry[] { new GpuBindGroupEntry(0, new GpuBufferBinding(simParamBuffer, simParamBuffer.Size)), new GpuBindGroupEntry(1, new GpuBufferBinding(ParticleBuffers[i], ParticleBuffers[i].Size)), new GpuBindGroupEntry(2, new GpuBufferBinding(ParticleBuffers[(i + 1) % 2], ParticleBuffers[(i + 1) % 2].Size)) })); } T = 0; }
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("TexturedCube.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.Float2, Offset = Cube.CubeUVOffset } }) } }; 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 } }, new GpuBindGroupLayoutEntry() { Binding = 1, Visibility = GpuShaderStageFlags.Fragment, Sampler = new GpuSamplerBindingLayout() { Type = GpuSamplerBindingType.Filtering } }, new GpuBindGroupLayoutEntry() { Binding = 2, Visibility = GpuShaderStageFlags.Fragment, Texture = new GpuTextureBindingLayout() { SampleType = GpuTextureSampleType.Float, ViewDimension = GpuTextureViewDimension._2D, Multisampled = false } } })); 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(4 * 4 * sizeof(float)); UniformCpuBuffer.Length = UniformCpuBuffer.Capacity; var imgDecoder = await BitmapDecoder.CreateAsync(typeof(MainPage).Assembly.GetManifestResourceStream("TexturedCube.Di_3d.png").AsRandomAccessStream()); var imageBitmap = await imgDecoder.GetSoftwareBitmapAsync(); var cubeTexture = Device.CreateTexture(new GpuTextureDescriptor(new GpuExtend3DDict { Width = (uint)imageBitmap.PixelWidth, Height = (uint)imageBitmap.PixelHeight, Depth = 1 }, GpuTextureFormat.BGRA8UNorm, GpuTextureUsageFlags.Sampled | GpuTextureUsageFlags.CopyDst)); Device.DefaultQueue.CopyImageBitmapToTexture(new GpuImageCopyImageBitmap(imageBitmap), new GpuImageCopyTexture(cubeTexture), new GpuExtend3DDict { Width = (uint)imageBitmap.PixelWidth, Height = (uint)imageBitmap.PixelHeight, Depth = 1 }); var sampler = Device.CreateSampler(new GpuSamplerDescriptor() { MagFilter = GpuFilterMode.Linear, MinFilter = GpuFilterMode.Linear }); UniformBindGroup = Device.CreateBindGroup(new GpuBindGroupDescriptor(uniformBindGroupLayout, new GpuBindGroupEntry[] { new GpuBindGroupEntry(0, new GpuBufferBinding(UniformBuffer, UniformBufferSize)), new GpuBindGroupEntry(1, sampler), new GpuBindGroupEntry(2, cubeTexture.CreateView()) })); }