• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
ComboVertexState()21     ComboVertexState::ComboVertexState() {
22         vertexBufferCount = 0;
23 
24         // Fill the default values for vertexBuffers and vertexAttributes in buffers.
25         wgpu::VertexAttribute vertexAttribute;
26         vertexAttribute.shaderLocation = 0;
27         vertexAttribute.offset = 0;
28         vertexAttribute.format = wgpu::VertexFormat::Float32;
29         for (uint32_t i = 0; i < kMaxVertexAttributes; ++i) {
30             cAttributes[i] = vertexAttribute;
31         }
32         for (uint32_t i = 0; i < kMaxVertexBuffers; ++i) {
33             cVertexBuffers[i].arrayStride = 0;
34             cVertexBuffers[i].stepMode = wgpu::VertexStepMode::Vertex;
35             cVertexBuffers[i].attributeCount = 0;
36             cVertexBuffers[i].attributes = nullptr;
37         }
38         // cVertexBuffers[i].attributes points to somewhere in cAttributes.
39         // cVertexBuffers[0].attributes points to &cAttributes[0] by default. Assuming
40         // cVertexBuffers[0] has two attributes, then cVertexBuffers[1].attributes should point to
41         // &cAttributes[2]. Likewise, if cVertexBuffers[1] has 3 attributes, then
42         // cVertexBuffers[2].attributes should point to &cAttributes[5].
43         cVertexBuffers[0].attributes = &cAttributes[0];
44     }
45 
ComboRenderPipelineDescriptor()46     ComboRenderPipelineDescriptor::ComboRenderPipelineDescriptor() {
47         wgpu::RenderPipelineDescriptor* descriptor = this;
48 
49         // Set defaults for the vertex state.
50         {
51             wgpu::VertexState* vertex = &descriptor->vertex;
52             vertex->module = nullptr;
53             vertex->entryPoint = "main";
54             vertex->bufferCount = 0;
55 
56             // Fill the default values for vertexBuffers and vertexAttributes in buffers.
57             for (uint32_t i = 0; i < kMaxVertexAttributes; ++i) {
58                 cAttributes[i].shaderLocation = 0;
59                 cAttributes[i].offset = 0;
60                 cAttributes[i].format = wgpu::VertexFormat::Float32;
61             }
62             for (uint32_t i = 0; i < kMaxVertexBuffers; ++i) {
63                 cBuffers[i].arrayStride = 0;
64                 cBuffers[i].stepMode = wgpu::VertexStepMode::Vertex;
65                 cBuffers[i].attributeCount = 0;
66                 cBuffers[i].attributes = nullptr;
67             }
68             // cBuffers[i].attributes points to somewhere in cAttributes.
69             // cBuffers[0].attributes points to &cAttributes[0] by default. Assuming
70             // cBuffers[0] has two attributes, then cBuffers[1].attributes should point to
71             // &cAttributes[2]. Likewise, if cBuffers[1] has 3 attributes, then
72             // cBuffers[2].attributes should point to &cAttributes[5].
73             cBuffers[0].attributes = &cAttributes[0];
74             vertex->buffers = &cBuffers[0];
75         }
76 
77         // Set the defaults for the primitive state
78         {
79             wgpu::PrimitiveState* primitive = &descriptor->primitive;
80             primitive->topology = wgpu::PrimitiveTopology::TriangleList;
81             primitive->stripIndexFormat = wgpu::IndexFormat::Undefined;
82             primitive->frontFace = wgpu::FrontFace::CCW;
83             primitive->cullMode = wgpu::CullMode::None;
84         }
85 
86         // Set the defaults for the depth-stencil state
87         {
88             wgpu::StencilFaceState stencilFace;
89             stencilFace.compare = wgpu::CompareFunction::Always;
90             stencilFace.failOp = wgpu::StencilOperation::Keep;
91             stencilFace.depthFailOp = wgpu::StencilOperation::Keep;
92             stencilFace.passOp = wgpu::StencilOperation::Keep;
93 
94             cDepthStencil.format = wgpu::TextureFormat::Depth24PlusStencil8;
95             cDepthStencil.depthWriteEnabled = false;
96             cDepthStencil.depthCompare = wgpu::CompareFunction::Always;
97             cDepthStencil.stencilBack = stencilFace;
98             cDepthStencil.stencilFront = stencilFace;
99             cDepthStencil.stencilReadMask = 0xff;
100             cDepthStencil.stencilWriteMask = 0xff;
101             cDepthStencil.depthBias = 0;
102             cDepthStencil.depthBiasSlopeScale = 0.0;
103             cDepthStencil.depthBiasClamp = 0.0;
104         }
105 
106         // Set the defaults for the multisample state
107         {
108             wgpu::MultisampleState* multisample = &descriptor->multisample;
109             multisample->count = 1;
110             multisample->mask = 0xFFFFFFFF;
111             multisample->alphaToCoverageEnabled = false;
112         }
113 
114         // Set the defaults for the fragment state
115         {
116             cFragment.module = nullptr;
117             cFragment.entryPoint = "main";
118             cFragment.targetCount = 1;
119             cFragment.targets = &cTargets[0];
120             descriptor->fragment = &cFragment;
121 
122             wgpu::BlendComponent blendComponent;
123             blendComponent.srcFactor = wgpu::BlendFactor::One;
124             blendComponent.dstFactor = wgpu::BlendFactor::Zero;
125             blendComponent.operation = wgpu::BlendOperation::Add;
126 
127             for (uint32_t i = 0; i < kMaxColorAttachments; ++i) {
128                 cTargets[i].format = wgpu::TextureFormat::RGBA8Unorm;
129                 cTargets[i].blend = nullptr;
130                 cTargets[i].writeMask = wgpu::ColorWriteMask::All;
131 
132                 cBlends[i].color = blendComponent;
133                 cBlends[i].alpha = blendComponent;
134             }
135         }
136     }
137 
EnableDepthStencil(wgpu::TextureFormat format)138     wgpu::DepthStencilState* ComboRenderPipelineDescriptor::EnableDepthStencil(
139         wgpu::TextureFormat format) {
140         this->depthStencil = &cDepthStencil;
141         cDepthStencil.format = format;
142         return &cDepthStencil;
143     }
144 
145 }  // namespace utils
146