From 9895c273d6daf05592478148947fae8d314ec53c Mon Sep 17 00:00:00 2001 From: Corentin Wallez Date: Thu, 18 Mar 2021 16:46:58 +0000 Subject: [PATCH] Update samples to the new RenderPipelineDescriptor Also does some updates for the new BindGroupLayoutEntry and VertexFormats that were missed previously. Bug: chromium:1177501 Change-Id: Icb336590673158538a1586a1f8d5ace398fb381e Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/45282 Reviewed-by: Brandon Jones Reviewed-by: Austin Eng Commit-Queue: Corentin Wallez --- examples/Animometer.cpp | 12 ++-- examples/CHelloTriangle.cpp | 77 +++++++++------------ examples/ComputeBoids.cpp | 48 ++++++------- examples/CppHelloTriangle.cpp | 25 ++++--- examples/CubeReflection.cpp | 114 ++++++++++++++++++------------- examples/ManualSwapChainTest.cpp | 11 ++- 6 files changed, 147 insertions(+), 140 deletions(-) diff --git a/examples/Animometer.cpp b/examples/Animometer.cpp index 35b0d9277c..4af226119f 100644 --- a/examples/Animometer.cpp +++ b/examples/Animometer.cpp @@ -113,15 +113,15 @@ void init() { })"); wgpu::BindGroupLayout bgl = utils::MakeBindGroupLayout( - device, {{0, wgpu::ShaderStage::Vertex, wgpu::BindingType::UniformBuffer, true}}); + device, {{0, wgpu::ShaderStage::Vertex, wgpu::BufferBindingType::Uniform, true}}); - utils::ComboRenderPipelineDescriptor descriptor(device); + utils::ComboRenderPipelineDescriptor2 descriptor; descriptor.layout = utils::MakeBasicPipelineLayout(device, &bgl); - descriptor.vertexStage.module = vsModule; - descriptor.cFragmentStage.module = fsModule; - descriptor.cColorStates[0].format = GetPreferredSwapChainTextureFormat(); + descriptor.vertex.module = vsModule; + descriptor.cFragment.module = fsModule; + descriptor.cTargets[0].format = GetPreferredSwapChainTextureFormat(); - pipeline = device.CreateRenderPipeline(&descriptor); + pipeline = device.CreateRenderPipeline2(&descriptor); shaderData.resize(kNumTriangles); for (auto& data : shaderData) { diff --git a/examples/CHelloTriangle.cpp b/examples/CHelloTriangle.cpp index 107ba59cc0..17c53f7c95 100644 --- a/examples/CHelloTriangle.cpp +++ b/examples/CHelloTriangle.cpp @@ -59,57 +59,48 @@ void init() { WGPUShaderModule fsModule = utils::CreateShaderModuleFromWGSL(device, fs).Release(); { - WGPURenderPipelineDescriptor descriptor = {}; + WGPURenderPipelineDescriptor2 descriptor = {}; - descriptor.vertexStage.module = vsModule; - descriptor.vertexStage.entryPoint = "main"; + // Fragment state + WGPUBlendState blend = {}; + blend.color.operation = WGPUBlendOperation_Add; + blend.color.srcFactor = WGPUBlendFactor_One; + blend.color.dstFactor = WGPUBlendFactor_One; + blend.alpha.operation = WGPUBlendOperation_Add; + blend.alpha.srcFactor = WGPUBlendFactor_One; + blend.alpha.dstFactor = WGPUBlendFactor_One; - WGPUProgrammableStageDescriptor fragmentStage = {}; - fragmentStage.module = fsModule; - fragmentStage.entryPoint = "main"; - descriptor.fragmentStage = &fragmentStage; + WGPUColorTargetState colorTarget = {}; + colorTarget.format = swapChainFormat; + colorTarget.blend = &blend; + colorTarget.writeMask = WGPUColorWriteMask_All; - descriptor.sampleCount = 1; + WGPUFragmentState fragment = {}; + fragment.module = fsModule; + fragment.entryPoint = "main"; + fragment.targetCount = 1; + fragment.targets = &colorTarget; + descriptor.fragment = &fragment; - WGPUBlendDescriptor blendDescriptor = {}; - blendDescriptor.operation = WGPUBlendOperation_Add; - blendDescriptor.srcFactor = WGPUBlendFactor_One; - blendDescriptor.dstFactor = WGPUBlendFactor_One; - WGPUColorStateDescriptor colorStateDescriptor = {}; - colorStateDescriptor.format = swapChainFormat; - colorStateDescriptor.alphaBlend = blendDescriptor; - colorStateDescriptor.colorBlend = blendDescriptor; - colorStateDescriptor.writeMask = WGPUColorWriteMask_All; + // Other state + descriptor.layout = nullptr; + descriptor.depthStencil = nullptr; - descriptor.colorStateCount = 1; - descriptor.colorStates = &colorStateDescriptor; + descriptor.vertex.module = vsModule; + descriptor.vertex.entryPoint = "main"; + descriptor.vertex.bufferCount = 0; + descriptor.vertex.buffers = nullptr; - WGPUPipelineLayoutDescriptor pl = {}; - pl.bindGroupLayoutCount = 0; - pl.bindGroupLayouts = nullptr; - descriptor.layout = wgpuDeviceCreatePipelineLayout(device, &pl); + descriptor.multisample.count = 1; + descriptor.multisample.mask = 0xFFFFFFFF; + descriptor.multisample.alphaToCoverageEnabled = false; - WGPUVertexStateDescriptor vertexState = {}; - vertexState.indexFormat = WGPUIndexFormat_Undefined; - vertexState.vertexBufferCount = 0; - vertexState.vertexBuffers = nullptr; - descriptor.vertexState = &vertexState; + descriptor.primitive.frontFace = WGPUFrontFace_CCW; + descriptor.primitive.cullMode = WGPUCullMode_None; + descriptor.primitive.topology = WGPUPrimitiveTopology_TriangleList; + descriptor.primitive.stripIndexFormat = WGPUIndexFormat_Undefined; - WGPURasterizationStateDescriptor rasterizationState = {}; - rasterizationState.frontFace = WGPUFrontFace_CCW; - rasterizationState.cullMode = WGPUCullMode_None; - rasterizationState.depthBias = 0; - rasterizationState.depthBiasSlopeScale = 0.0; - rasterizationState.depthBiasClamp = 0.0; - descriptor.rasterizationState = &rasterizationState; - - descriptor.primitiveTopology = WGPUPrimitiveTopology_TriangleList; - descriptor.sampleMask = 0xFFFFFFFF; - descriptor.alphaToCoverageEnabled = false; - - descriptor.depthStencilState = nullptr; - - pipeline = wgpuDeviceCreateRenderPipeline(device, &descriptor); + pipeline = wgpuDeviceCreateRenderPipeline2(device, &descriptor); } wgpuShaderModuleRelease(vsModule); diff --git a/examples/ComputeBoids.cpp b/examples/ComputeBoids.cpp index 476077290c..484105ffbe 100644 --- a/examples/ComputeBoids.cpp +++ b/examples/ComputeBoids.cpp @@ -123,29 +123,29 @@ void initRender() { depthStencilView = CreateDefaultDepthStencilView(device); - utils::ComboRenderPipelineDescriptor descriptor(device); - descriptor.vertexStage.module = vsModule; - descriptor.cFragmentStage.module = fsModule; + utils::ComboRenderPipelineDescriptor2 descriptor; - descriptor.cVertexState.vertexBufferCount = 2; - descriptor.cVertexState.cVertexBuffers[0].arrayStride = sizeof(Particle); - descriptor.cVertexState.cVertexBuffers[0].stepMode = wgpu::InputStepMode::Instance; - descriptor.cVertexState.cVertexBuffers[0].attributeCount = 2; - descriptor.cVertexState.cAttributes[0].offset = offsetof(Particle, pos); - descriptor.cVertexState.cAttributes[0].format = wgpu::VertexFormat::Float2; - descriptor.cVertexState.cAttributes[1].shaderLocation = 1; - descriptor.cVertexState.cAttributes[1].offset = offsetof(Particle, vel); - descriptor.cVertexState.cAttributes[1].format = wgpu::VertexFormat::Float2; - descriptor.cVertexState.cVertexBuffers[1].arrayStride = sizeof(glm::vec2); - descriptor.cVertexState.cVertexBuffers[1].attributeCount = 1; - descriptor.cVertexState.cVertexBuffers[1].attributes = &descriptor.cVertexState.cAttributes[2]; - descriptor.cVertexState.cAttributes[2].shaderLocation = 2; - descriptor.cVertexState.cAttributes[2].format = wgpu::VertexFormat::Float2; - descriptor.depthStencilState = &descriptor.cDepthStencilState; - descriptor.cDepthStencilState.format = wgpu::TextureFormat::Depth24PlusStencil8; - descriptor.cColorStates[0].format = GetPreferredSwapChainTextureFormat(); + descriptor.vertex.module = vsModule; + descriptor.vertex.bufferCount = 2; + descriptor.cBuffers[0].arrayStride = sizeof(Particle); + descriptor.cBuffers[0].stepMode = wgpu::InputStepMode::Instance; + descriptor.cBuffers[0].attributeCount = 2; + descriptor.cAttributes[0].offset = offsetof(Particle, pos); + descriptor.cAttributes[0].format = wgpu::VertexFormat::Float32x2; + descriptor.cAttributes[1].shaderLocation = 1; + descriptor.cAttributes[1].offset = offsetof(Particle, vel); + descriptor.cAttributes[1].format = wgpu::VertexFormat::Float32x2; + descriptor.cBuffers[1].arrayStride = sizeof(glm::vec2); + descriptor.cBuffers[1].attributeCount = 1; + descriptor.cBuffers[1].attributes = &descriptor.cAttributes[2]; + descriptor.cAttributes[2].shaderLocation = 2; + descriptor.cAttributes[2].format = wgpu::VertexFormat::Float32x2; - renderPipeline = device.CreateRenderPipeline(&descriptor); + descriptor.cFragment.module = fsModule; + descriptor.EnableDepthStencil(wgpu::TextureFormat::Depth24PlusStencil8); + descriptor.cTargets[0].format = GetPreferredSwapChainTextureFormat(); + + renderPipeline = device.CreateRenderPipeline2(&descriptor); } void initSim() { @@ -247,9 +247,9 @@ void initSim() { auto bgl = utils::MakeBindGroupLayout( device, { - {0, wgpu::ShaderStage::Compute, wgpu::BindingType::UniformBuffer}, - {1, wgpu::ShaderStage::Compute, wgpu::BindingType::StorageBuffer}, - {2, wgpu::ShaderStage::Compute, wgpu::BindingType::StorageBuffer}, + {0, wgpu::ShaderStage::Compute, wgpu::BufferBindingType::Uniform}, + {1, wgpu::ShaderStage::Compute, wgpu::BufferBindingType::Storage}, + {2, wgpu::ShaderStage::Compute, wgpu::BufferBindingType::Storage}, }); wgpu::PipelineLayout pl = utils::MakeBasicPipelineLayout(device, &bgl); diff --git a/examples/CppHelloTriangle.cpp b/examples/CppHelloTriangle.cpp index e961f730c7..dde6162a0e 100644 --- a/examples/CppHelloTriangle.cpp +++ b/examples/CppHelloTriangle.cpp @@ -116,27 +116,26 @@ void init() { auto bgl = utils::MakeBindGroupLayout( device, { - {0, wgpu::ShaderStage::Fragment, wgpu::BindingType::Sampler}, - {1, wgpu::ShaderStage::Fragment, wgpu::BindingType::SampledTexture}, + {0, wgpu::ShaderStage::Fragment, wgpu::SamplerBindingType::Filtering}, + {1, wgpu::ShaderStage::Fragment, wgpu::TextureSampleType::Float}, }); wgpu::PipelineLayout pl = utils::MakeBasicPipelineLayout(device, &bgl); depthStencilView = CreateDefaultDepthStencilView(device); - utils::ComboRenderPipelineDescriptor descriptor(device); + utils::ComboRenderPipelineDescriptor2 descriptor; descriptor.layout = utils::MakeBasicPipelineLayout(device, &bgl); - descriptor.vertexStage.module = vsModule; - descriptor.cFragmentStage.module = fsModule; - descriptor.cVertexState.vertexBufferCount = 1; - descriptor.cVertexState.cVertexBuffers[0].arrayStride = 4 * sizeof(float); - descriptor.cVertexState.cVertexBuffers[0].attributeCount = 1; - descriptor.cVertexState.cAttributes[0].format = wgpu::VertexFormat::Float4; - descriptor.depthStencilState = &descriptor.cDepthStencilState; - descriptor.cDepthStencilState.format = wgpu::TextureFormat::Depth24PlusStencil8; - descriptor.cColorStates[0].format = GetPreferredSwapChainTextureFormat(); + descriptor.vertex.module = vsModule; + descriptor.vertex.bufferCount = 1; + descriptor.cBuffers[0].arrayStride = 4 * sizeof(float); + descriptor.cBuffers[0].attributeCount = 1; + descriptor.cAttributes[0].format = wgpu::VertexFormat::Float32x4; + descriptor.cFragment.module = fsModule; + descriptor.cTargets[0].format = GetPreferredSwapChainTextureFormat(); + descriptor.EnableDepthStencil(wgpu::TextureFormat::Depth24PlusStencil8); - pipeline = device.CreateRenderPipeline(&descriptor); + pipeline = device.CreateRenderPipeline2(&descriptor); wgpu::TextureView view = texture.CreateView(); diff --git a/examples/CubeReflection.cpp b/examples/CubeReflection.cpp index 88d5cb41b9..da6e5442f5 100644 --- a/examples/CubeReflection.cpp +++ b/examples/CubeReflection.cpp @@ -143,20 +143,23 @@ void init() { return; })"); - utils::ComboVertexStateDescriptor vertexState; - vertexState.cVertexBuffers[0].attributeCount = 2; - vertexState.cAttributes[0].format = wgpu::VertexFormat::Float3; - vertexState.cAttributes[1].shaderLocation = 1; - vertexState.cAttributes[1].offset = 3 * sizeof(float); - vertexState.cAttributes[1].format = wgpu::VertexFormat::Float3; + wgpu::VertexAttribute attributes[2]; + attributes[0].shaderLocation = 0; + attributes[0].offset = 0; + attributes[0].format = wgpu::VertexFormat::Float32x3; + attributes[1].shaderLocation = 1; + attributes[1].offset = 3 * sizeof(float); + attributes[1].format = wgpu::VertexFormat::Float32x3; - vertexState.vertexBufferCount = 1; - vertexState.cVertexBuffers[0].arrayStride = 6 * sizeof(float); + wgpu::VertexBufferLayout vertexBufferLayout; + vertexBufferLayout.attributeCount = 2; + vertexBufferLayout.attributes = attributes; + vertexBufferLayout.arrayStride = 6 * sizeof(float); auto bgl = utils::MakeBindGroupLayout( device, { - {0, wgpu::ShaderStage::Vertex, wgpu::BindingType::UniformBuffer}, - {1, wgpu::ShaderStage::Vertex, wgpu::BindingType::UniformBuffer}, + {0, wgpu::ShaderStage::Vertex, wgpu::BufferBindingType::Uniform}, + {1, wgpu::ShaderStage::Vertex, wgpu::BufferBindingType::Uniform}, }); wgpu::PipelineLayout pl = utils::MakeBasicPipelineLayout(device, &bgl); @@ -184,49 +187,64 @@ void init() { depthStencilView = CreateDefaultDepthStencilView(device); - utils::ComboRenderPipelineDescriptor descriptor(device); - descriptor.layout = pl; - descriptor.vertexStage.module = vsModule; - descriptor.cFragmentStage.module = fsModule; - descriptor.vertexState = &vertexState; - descriptor.depthStencilState = &descriptor.cDepthStencilState; - descriptor.cDepthStencilState.format = wgpu::TextureFormat::Depth24PlusStencil8; - descriptor.cColorStates[0].format = GetPreferredSwapChainTextureFormat(); - descriptor.cDepthStencilState.depthWriteEnabled = true; - descriptor.cDepthStencilState.depthCompare = wgpu::CompareFunction::Less; + { + utils::ComboRenderPipelineDescriptor2 descriptor; + descriptor.vertex.module = vsModule; + descriptor.vertex.bufferCount = 1; + descriptor.vertex.buffers = &vertexBufferLayout; - pipeline = device.CreateRenderPipeline(&descriptor); + descriptor.layout = pl; + descriptor.cFragment.module = fsModule; + descriptor.cTargets[0].format = GetPreferredSwapChainTextureFormat(); - utils::ComboRenderPipelineDescriptor pDescriptor(device); - pDescriptor.layout = pl; - pDescriptor.vertexStage.module = vsModule; - pDescriptor.cFragmentStage.module = fsModule; - pDescriptor.vertexState = &vertexState; - pDescriptor.depthStencilState = &pDescriptor.cDepthStencilState; - pDescriptor.cDepthStencilState.format = wgpu::TextureFormat::Depth24PlusStencil8; - pDescriptor.cColorStates[0].format = GetPreferredSwapChainTextureFormat(); - pDescriptor.cDepthStencilState.stencilFront.passOp = wgpu::StencilOperation::Replace; - pDescriptor.cDepthStencilState.stencilBack.passOp = wgpu::StencilOperation::Replace; - pDescriptor.cDepthStencilState.depthCompare = wgpu::CompareFunction::Less; + wgpu::DepthStencilState* depthStencil = + descriptor.EnableDepthStencil(wgpu::TextureFormat::Depth24PlusStencil8); + depthStencil->depthWriteEnabled = true; + depthStencil->depthCompare = wgpu::CompareFunction::Less; - planePipeline = device.CreateRenderPipeline(&pDescriptor); + pipeline = device.CreateRenderPipeline2(&descriptor); + } - utils::ComboRenderPipelineDescriptor rfDescriptor(device); - rfDescriptor.layout = pl; - rfDescriptor.vertexStage.module = vsModule; - rfDescriptor.cFragmentStage.module = fsReflectionModule; - rfDescriptor.vertexState = &vertexState; - rfDescriptor.depthStencilState = &rfDescriptor.cDepthStencilState; - rfDescriptor.cDepthStencilState.format = wgpu::TextureFormat::Depth24PlusStencil8; - rfDescriptor.cColorStates[0].format = GetPreferredSwapChainTextureFormat(); - rfDescriptor.cDepthStencilState.stencilFront.compare = wgpu::CompareFunction::Equal; - rfDescriptor.cDepthStencilState.stencilBack.compare = wgpu::CompareFunction::Equal; - rfDescriptor.cDepthStencilState.stencilFront.passOp = wgpu::StencilOperation::Replace; - rfDescriptor.cDepthStencilState.stencilBack.passOp = wgpu::StencilOperation::Replace; - rfDescriptor.cDepthStencilState.depthWriteEnabled = true; - rfDescriptor.cDepthStencilState.depthCompare = wgpu::CompareFunction::Less; + { + utils::ComboRenderPipelineDescriptor2 descriptor; + descriptor.vertex.module = vsModule; + descriptor.vertex.bufferCount = 1; + descriptor.vertex.buffers = &vertexBufferLayout; - reflectionPipeline = device.CreateRenderPipeline(&rfDescriptor); + descriptor.layout = pl; + descriptor.cFragment.module = fsModule; + descriptor.cTargets[0].format = GetPreferredSwapChainTextureFormat(); + + wgpu::DepthStencilState* depthStencil = + descriptor.EnableDepthStencil(wgpu::TextureFormat::Depth24PlusStencil8); + depthStencil->stencilFront.passOp = wgpu::StencilOperation::Replace; + depthStencil->stencilBack.passOp = wgpu::StencilOperation::Replace; + depthStencil->depthCompare = wgpu::CompareFunction::Less; + + planePipeline = device.CreateRenderPipeline2(&descriptor); + } + + { + utils::ComboRenderPipelineDescriptor2 descriptor; + descriptor.vertex.module = vsModule; + descriptor.vertex.bufferCount = 1; + descriptor.vertex.buffers = &vertexBufferLayout; + + descriptor.layout = pl; + descriptor.cFragment.module = fsReflectionModule; + descriptor.cTargets[0].format = GetPreferredSwapChainTextureFormat(); + + wgpu::DepthStencilState* depthStencil = + descriptor.EnableDepthStencil(wgpu::TextureFormat::Depth24PlusStencil8); + depthStencil->stencilFront.compare = wgpu::CompareFunction::Equal; + depthStencil->stencilBack.compare = wgpu::CompareFunction::Equal; + depthStencil->stencilFront.passOp = wgpu::StencilOperation::Replace; + depthStencil->stencilBack.passOp = wgpu::StencilOperation::Replace; + depthStencil->depthWriteEnabled = true; + depthStencil->depthCompare = wgpu::CompareFunction::Less; + + reflectionPipeline = device.CreateRenderPipeline2(&descriptor); + } cameraData.proj = glm::perspective(glm::radians(45.0f), 1.f, 1.0f, 100.0f); } diff --git a/examples/ManualSwapChainTest.cpp b/examples/ManualSwapChainTest.cpp index 771bb95f76..94be77daae 100644 --- a/examples/ManualSwapChainTest.cpp +++ b/examples/ManualSwapChainTest.cpp @@ -311,8 +311,8 @@ int main(int argc, const char* argv[]) { queue = device.GetQueue(); // The hacky pipeline to render a triangle. - utils::ComboRenderPipelineDescriptor pipelineDesc(device); - pipelineDesc.vertexStage.module = utils::CreateShaderModuleFromWGSL(device, R"( + utils::ComboRenderPipelineDescriptor2 pipelineDesc; + pipelineDesc.vertex.module = utils::CreateShaderModuleFromWGSL(device, R"( [[builtin(vertex_index)]] var VertexIndex : u32; [[builtin(position)]] var Position : vec4; const pos : array, 3> = array, 3>( @@ -324,16 +324,15 @@ int main(int argc, const char* argv[]) { Position = vec4(pos[VertexIndex], 0.0, 1.0); return; })"); - pipelineDesc.cFragmentStage.module = utils::CreateShaderModuleFromWGSL(device, R"( + pipelineDesc.cFragment.module = utils::CreateShaderModuleFromWGSL(device, R"( [[location(0)]] var fragColor : vec4; [[stage(fragment)]] fn main() -> void { fragColor = vec4(1.0, 0.0, 0.0, 1.0); return; })"); - pipelineDesc.colorStateCount = 1; // BGRA shouldn't be hardcoded. Consider having a map[format -> pipeline]. - pipelineDesc.cColorStates[0].format = wgpu::TextureFormat::BGRA8Unorm; - trianglePipeline = device.CreateRenderPipeline(&pipelineDesc); + pipelineDesc.cTargets[0].format = wgpu::TextureFormat::BGRA8Unorm; + trianglePipeline = device.CreateRenderPipeline2(&pipelineDesc); // Craete the first window, since the example exits when there are no windows. AddWindow();