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/DawnHelpers.h" 18 19 namespace utils { 20 ComboVertexInputDescriptor()21 ComboVertexInputDescriptor::ComboVertexInputDescriptor() { 22 dawn::VertexInputDescriptor* descriptor = this; 23 24 descriptor->indexFormat = dawn::IndexFormat::Uint32; 25 descriptor->bufferCount = 0; 26 27 // Fill the default values for vertexBuffers and vertexAttributes in buffers. 28 dawn::VertexAttributeDescriptor vertexAttribute; 29 vertexAttribute.shaderLocation = 0; 30 vertexAttribute.offset = 0; 31 vertexAttribute.format = dawn::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 cBuffers[i].stride = 0; 37 cBuffers[i].stepMode = dawn::InputStepMode::Vertex; 38 cBuffers[i].attributeCount = 0; 39 cBuffers[i].attributes = nullptr; 40 } 41 // cBuffers[i].attributes points to somewhere in cAttributes. cBuffers[0].attributes 42 // points to &cAttributes[0] by default. Assuming cBuffers[0] has two attributes, then 43 // cBuffers[1].attributes should point to &cAttributes[2]. Likewise, if cBuffers[1] 44 // has 3 attributes, then cBuffers[2].attributes should point to &cAttributes[5]. 45 cBuffers[0].attributes = &cAttributes[0]; 46 descriptor->buffers = &cBuffers[0]; 47 } 48 ComboRenderPipelineDescriptor(const dawn::Device & device)49 ComboRenderPipelineDescriptor::ComboRenderPipelineDescriptor(const dawn::Device& device) { 50 dawn::RenderPipelineDescriptor* descriptor = this; 51 52 descriptor->primitiveTopology = dawn::PrimitiveTopology::TriangleList; 53 descriptor->sampleCount = 1; 54 55 // Set defaults for the vertex stage descriptor. 56 { 57 descriptor->vertexStage = &cVertexStage; 58 cVertexStage.entryPoint = "main"; 59 } 60 61 // Set defaults for the fragment stage desriptor. 62 { 63 descriptor->fragmentStage = &cFragmentStage; 64 cFragmentStage.entryPoint = "main"; 65 } 66 67 // Set defaults for the input state descriptors. 68 descriptor->vertexInput = &cVertexInput; 69 70 // Set defaults for the rasterization state descriptor. 71 { 72 cRasterizationState.frontFace = dawn::FrontFace::CCW; 73 cRasterizationState.cullMode = dawn::CullMode::None; 74 75 cRasterizationState.depthBias = 0; 76 cRasterizationState.depthBiasSlopeScale = 0.0; 77 cRasterizationState.depthBiasClamp = 0.0; 78 descriptor->rasterizationState = &cRasterizationState; 79 } 80 81 // Set defaults for the color state descriptors. 82 { 83 descriptor->colorStateCount = 1; 84 descriptor->colorStates = &cColorStates[0]; 85 86 dawn::BlendDescriptor blend; 87 blend.operation = dawn::BlendOperation::Add; 88 blend.srcFactor = dawn::BlendFactor::One; 89 blend.dstFactor = dawn::BlendFactor::Zero; 90 dawn::ColorStateDescriptor colorStateDescriptor; 91 colorStateDescriptor.format = dawn::TextureFormat::RGBA8Unorm; 92 colorStateDescriptor.alphaBlend = blend; 93 colorStateDescriptor.colorBlend = blend; 94 colorStateDescriptor.writeMask = dawn::ColorWriteMask::All; 95 for (uint32_t i = 0; i < kMaxColorAttachments; ++i) { 96 mColorStates[i] = colorStateDescriptor; 97 cColorStates[i] = &mColorStates[i]; 98 } 99 } 100 101 // Set defaults for the depth stencil state descriptors. 102 { 103 dawn::StencilStateFaceDescriptor stencilFace; 104 stencilFace.compare = dawn::CompareFunction::Always; 105 stencilFace.failOp = dawn::StencilOperation::Keep; 106 stencilFace.depthFailOp = dawn::StencilOperation::Keep; 107 stencilFace.passOp = dawn::StencilOperation::Keep; 108 109 cDepthStencilState.format = dawn::TextureFormat::Depth24PlusStencil8; 110 cDepthStencilState.depthWriteEnabled = false; 111 cDepthStencilState.depthCompare = dawn::CompareFunction::Always; 112 cDepthStencilState.stencilBack = stencilFace; 113 cDepthStencilState.stencilFront = stencilFace; 114 cDepthStencilState.stencilReadMask = 0xff; 115 cDepthStencilState.stencilWriteMask = 0xff; 116 descriptor->depthStencilState = nullptr; 117 } 118 119 descriptor->layout = utils::MakeBasicPipelineLayout(device, nullptr); 120 } 121 122 } // namespace utils 123