1 // Copyright 2018 The Dawn Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include "utils/ComboRenderPipelineDescriptor.h"
16 
17 #include "utils/WGPUHelpers.h"
18 
19 namespace utils {
20 
ComboVertexStateDescriptor()21     ComboVertexStateDescriptor::ComboVertexStateDescriptor() {
22         wgpu::VertexStateDescriptor* descriptor = this;
23 
24         descriptor->indexFormat = wgpu::IndexFormat::Undefined;
25         descriptor->vertexBufferCount = 0;
26 
27         // Fill the default values for vertexBuffers and vertexAttributes in buffers.
28         wgpu::VertexAttributeDescriptor vertexAttribute;
29         vertexAttribute.shaderLocation = 0;
30         vertexAttribute.offset = 0;
31         vertexAttribute.format = wgpu::VertexFormat::Float;
32         for (uint32_t i = 0; i < kMaxVertexAttributes; ++i) {
33             cAttributes[i] = vertexAttribute;
34         }
35         for (uint32_t i = 0; i < kMaxVertexBuffers; ++i) {
36             cVertexBuffers[i].arrayStride = 0;
37             cVertexBuffers[i].stepMode = wgpu::InputStepMode::Vertex;
38             cVertexBuffers[i].attributeCount = 0;
39             cVertexBuffers[i].attributes = nullptr;
40         }
41         // cVertexBuffers[i].attributes points to somewhere in cAttributes.
42         // cVertexBuffers[0].attributes points to &cAttributes[0] by default. Assuming
43         // cVertexBuffers[0] has two attributes, then cVertexBuffers[1].attributes should point to
44         // &cAttributes[2]. Likewise, if cVertexBuffers[1] has 3 attributes, then
45         // cVertexBuffers[2].attributes should point to &cAttributes[5].
46         cVertexBuffers[0].attributes = &cAttributes[0];
47         descriptor->vertexBuffers = &cVertexBuffers[0];
48     }
49 
ComboRenderPipelineDescriptor(const wgpu::Device & device)50     ComboRenderPipelineDescriptor::ComboRenderPipelineDescriptor(const wgpu::Device& device) {
51         wgpu::RenderPipelineDescriptor* descriptor = this;
52 
53         descriptor->primitiveTopology = wgpu::PrimitiveTopology::TriangleList;
54         descriptor->sampleCount = 1;
55 
56         // Set defaults for the vertex stage descriptor.
57         { vertexStage.entryPoint = "main"; }
58 
59         // Set defaults for the fragment stage desriptor.
60         {
61             descriptor->fragmentStage = &cFragmentStage;
62             cFragmentStage.entryPoint = "main";
63         }
64 
65         // Set defaults for the input state descriptors.
66         descriptor->vertexState = &cVertexState;
67 
68         // Set defaults for the rasterization state descriptor.
69         {
70             cRasterizationState.frontFace = wgpu::FrontFace::CCW;
71             cRasterizationState.cullMode = wgpu::CullMode::None;
72 
73             cRasterizationState.depthBias = 0;
74             cRasterizationState.depthBiasSlopeScale = 0.0;
75             cRasterizationState.depthBiasClamp = 0.0;
76             descriptor->rasterizationState = &cRasterizationState;
77         }
78 
79         // Set defaults for the color state descriptors.
80         {
81             descriptor->colorStateCount = 1;
82             descriptor->colorStates = cColorStates.data();
83 
84             wgpu::BlendDescriptor blend;
85             blend.operation = wgpu::BlendOperation::Add;
86             blend.srcFactor = wgpu::BlendFactor::One;
87             blend.dstFactor = wgpu::BlendFactor::Zero;
88             wgpu::ColorStateDescriptor colorStateDescriptor;
89             colorStateDescriptor.format = wgpu::TextureFormat::RGBA8Unorm;
90             colorStateDescriptor.alphaBlend = blend;
91             colorStateDescriptor.colorBlend = blend;
92             colorStateDescriptor.writeMask = wgpu::ColorWriteMask::All;
93             for (uint32_t i = 0; i < kMaxColorAttachments; ++i) {
94                 cColorStates[i] = colorStateDescriptor;
95             }
96         }
97 
98         // Set defaults for the depth stencil state descriptors.
99         {
100             wgpu::StencilStateFaceDescriptor stencilFace;
101             stencilFace.compare = wgpu::CompareFunction::Always;
102             stencilFace.failOp = wgpu::StencilOperation::Keep;
103             stencilFace.depthFailOp = wgpu::StencilOperation::Keep;
104             stencilFace.passOp = wgpu::StencilOperation::Keep;
105 
106             cDepthStencilState.format = wgpu::TextureFormat::Depth24PlusStencil8;
107             cDepthStencilState.depthWriteEnabled = false;
108             cDepthStencilState.depthCompare = wgpu::CompareFunction::Always;
109             cDepthStencilState.stencilBack = stencilFace;
110             cDepthStencilState.stencilFront = stencilFace;
111             cDepthStencilState.stencilReadMask = 0xff;
112             cDepthStencilState.stencilWriteMask = 0xff;
113             descriptor->depthStencilState = nullptr;
114         }
115     }
116 
117 }  // namespace utils
118