• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2017 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 "SampleUtils.h"
16 
17 #include "utils/DawnHelpers.h"
18 #include "utils/SystemUtils.h"
19 
20 DawnDevice device;
21 DawnQueue queue;
22 DawnSwapChain swapchain;
23 DawnRenderPipeline pipeline;
24 
25 DawnTextureFormat swapChainFormat;
26 
init()27 void init() {
28     device = CreateCppDawnDevice().Release();
29     queue = dawnDeviceCreateQueue(device);
30 
31     {
32         DawnSwapChainDescriptor descriptor;
33         descriptor.nextInChain = nullptr;
34         descriptor.implementation = GetSwapChainImplementation();
35         swapchain = dawnDeviceCreateSwapChain(device, &descriptor);
36     }
37     swapChainFormat = static_cast<DawnTextureFormat>(GetPreferredSwapChainTextureFormat());
38     dawnSwapChainConfigure(swapchain, swapChainFormat, DAWN_TEXTURE_USAGE_BIT_OUTPUT_ATTACHMENT, 640,
39                           480);
40 
41     const char* vs =
42         "#version 450\n"
43         "const vec2 pos[3] = vec2[3](vec2(0.0f, 0.5f), vec2(-0.5f, -0.5f), vec2(0.5f, -0.5f));\n"
44         "void main() {\n"
45         "   gl_Position = vec4(pos[gl_VertexIndex], 0.0, 1.0);\n"
46         "}\n";
47     DawnShaderModule vsModule =
48         utils::CreateShaderModule(dawn::Device(device), utils::ShaderStage::Vertex, vs).Release();
49 
50     const char* fs =
51         "#version 450\n"
52         "layout(location = 0) out vec4 fragColor;"
53         "void main() {\n"
54         "   fragColor = vec4(1.0, 0.0, 0.0, 1.0);\n"
55         "}\n";
56     DawnShaderModule fsModule =
57         utils::CreateShaderModule(device, utils::ShaderStage::Fragment, fs).Release();
58 
59     {
60         DawnRenderPipelineDescriptor descriptor;
61         descriptor.nextInChain = nullptr;
62 
63         DawnPipelineStageDescriptor vertexStage;
64         vertexStage.nextInChain = nullptr;
65         vertexStage.module = vsModule;
66         vertexStage.entryPoint = "main";
67         descriptor.vertexStage = &vertexStage;
68 
69         DawnPipelineStageDescriptor fragmentStage;
70         fragmentStage.nextInChain = nullptr;
71         fragmentStage.module = fsModule;
72         fragmentStage.entryPoint = "main";
73         descriptor.fragmentStage = &fragmentStage;
74 
75         descriptor.sampleCount = 1;
76 
77         DawnBlendDescriptor blendDescriptor;
78         blendDescriptor.operation = DAWN_BLEND_OPERATION_ADD;
79         blendDescriptor.srcFactor = DAWN_BLEND_FACTOR_ONE;
80         blendDescriptor.dstFactor = DAWN_BLEND_FACTOR_ONE;
81         DawnColorStateDescriptor colorStateDescriptor;
82         colorStateDescriptor.nextInChain = nullptr;
83         colorStateDescriptor.format = swapChainFormat;
84         colorStateDescriptor.alphaBlend = blendDescriptor;
85         colorStateDescriptor.colorBlend = blendDescriptor;
86         colorStateDescriptor.writeMask = DAWN_COLOR_WRITE_MASK_ALL;
87 
88         descriptor.colorStateCount = 1;
89         DawnColorStateDescriptor* colorStatesPtr[] = {&colorStateDescriptor};
90         descriptor.colorStates = colorStatesPtr;
91 
92         DawnPipelineLayoutDescriptor pl;
93         pl.nextInChain = nullptr;
94         pl.bindGroupLayoutCount = 0;
95         pl.bindGroupLayouts = nullptr;
96         descriptor.layout = dawnDeviceCreatePipelineLayout(device, &pl);
97 
98         DawnVertexInputDescriptor vertexInput;
99         vertexInput.nextInChain = nullptr;
100         vertexInput.indexFormat = DAWN_INDEX_FORMAT_UINT32;
101         vertexInput.bufferCount = 0;
102         vertexInput.buffers = nullptr;
103         descriptor.vertexInput = &vertexInput;
104 
105         DawnRasterizationStateDescriptor rasterizationState;
106         rasterizationState.nextInChain = nullptr;
107         rasterizationState.frontFace = DAWN_FRONT_FACE_CCW;
108         rasterizationState.cullMode = DAWN_CULL_MODE_NONE;
109         rasterizationState.depthBias = 0;
110         rasterizationState.depthBiasSlopeScale = 0.0;
111         rasterizationState.depthBiasClamp = 0.0;
112         descriptor.rasterizationState = &rasterizationState;
113 
114         descriptor.primitiveTopology = DAWN_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
115         descriptor.sampleMask = 0xFFFFFFFF;
116         descriptor.alphaToCoverageEnabled = false;
117 
118         descriptor.depthStencilState = nullptr;
119 
120         pipeline = dawnDeviceCreateRenderPipeline(device, &descriptor);
121     }
122 
123     dawnShaderModuleRelease(vsModule);
124     dawnShaderModuleRelease(fsModule);
125 }
126 
frame()127 void frame() {
128     DawnTexture backbuffer = dawnSwapChainGetNextTexture(swapchain);
129     DawnTextureView backbufferView;
130     {
131         backbufferView = dawnTextureCreateDefaultView(backbuffer);
132     }
133     DawnRenderPassDescriptor renderpassInfo;
134     DawnRenderPassColorAttachmentDescriptor colorAttachment;
135     DawnRenderPassColorAttachmentDescriptor* colorAttachments = {&colorAttachment};
136     {
137         colorAttachment.attachment = backbufferView;
138         colorAttachment.resolveTarget = nullptr;
139         colorAttachment.clearColor = { 0.0f, 0.0f, 0.0f, 0.0f };
140         colorAttachment.loadOp = DAWN_LOAD_OP_CLEAR;
141         colorAttachment.storeOp = DAWN_STORE_OP_STORE;
142         renderpassInfo.colorAttachmentCount = 1;
143         renderpassInfo.colorAttachments = &colorAttachments;
144         renderpassInfo.depthStencilAttachment = nullptr;
145     }
146     DawnCommandBuffer commands;
147     {
148         DawnCommandEncoder encoder = dawnDeviceCreateCommandEncoder(device, nullptr);
149 
150         DawnRenderPassEncoder pass = dawnCommandEncoderBeginRenderPass(encoder, &renderpassInfo);
151         dawnRenderPassEncoderSetPipeline(pass, pipeline);
152         dawnRenderPassEncoderDraw(pass, 3, 1, 0, 0);
153         dawnRenderPassEncoderEndPass(pass);
154         dawnRenderPassEncoderRelease(pass);
155 
156         commands = dawnCommandEncoderFinish(encoder, nullptr);
157         dawnCommandEncoderRelease(encoder);
158     }
159 
160     dawnQueueSubmit(queue, 1, &commands);
161     dawnCommandBufferRelease(commands);
162     dawnSwapChainPresent(swapchain, backbuffer);
163     dawnTextureViewRelease(backbufferView);
164 
165     DoFlush();
166 }
167 
main(int argc,const char * argv[])168 int main(int argc, const char* argv[]) {
169     if (!InitSample(argc, argv)) {
170         return 1;
171     }
172     init();
173 
174     while (!ShouldQuit()) {
175         frame();
176         utils::USleep(16000);
177     }
178 
179     // TODO release stuff
180 }
181