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/SystemUtils.h"
18 #include "utils/WGPUHelpers.h"
19 
20 WGPUDevice device;
21 WGPUQueue queue;
22 WGPUSwapChain swapchain;
23 WGPURenderPipeline pipeline;
24 
25 WGPUTextureFormat swapChainFormat;
26 
init()27 void init() {
28     device = CreateCppDawnDevice().Release();
29     queue = wgpuDeviceCreateQueue(device);
30 
31     {
32         WGPUSwapChainDescriptor descriptor = {};
33         descriptor.implementation = GetSwapChainImplementation();
34         swapchain = wgpuDeviceCreateSwapChain(device, nullptr, &descriptor);
35     }
36     swapChainFormat = static_cast<WGPUTextureFormat>(GetPreferredSwapChainTextureFormat());
37     wgpuSwapChainConfigure(swapchain, swapChainFormat, WGPUTextureUsage_OutputAttachment, 640, 480);
38 
39     const char* vs =
40         "#version 450\n"
41         "const vec2 pos[3] = vec2[3](vec2(0.0f, 0.5f), vec2(-0.5f, -0.5f), vec2(0.5f, -0.5f));\n"
42         "void main() {\n"
43         "   gl_Position = vec4(pos[gl_VertexIndex], 0.0, 1.0);\n"
44         "}\n";
45     WGPUShaderModule vsModule =
46         utils::CreateShaderModule(wgpu::Device(device), utils::SingleShaderStage::Vertex, vs)
47             .Release();
48 
49     const char* fs =
50         "#version 450\n"
51         "layout(location = 0) out vec4 fragColor;\n"
52         "void main() {\n"
53         "   fragColor = vec4(1.0, 0.0, 0.0, 1.0);\n"
54         "}\n";
55     WGPUShaderModule fsModule =
56         utils::CreateShaderModule(device, utils::SingleShaderStage::Fragment, fs).Release();
57 
58     {
59         WGPURenderPipelineDescriptor descriptor = {};
60 
61         descriptor.vertexStage.module = vsModule;
62         descriptor.vertexStage.entryPoint = "main";
63 
64         WGPUProgrammableStageDescriptor fragmentStage = {};
65         fragmentStage.module = fsModule;
66         fragmentStage.entryPoint = "main";
67         descriptor.fragmentStage = &fragmentStage;
68 
69         descriptor.sampleCount = 1;
70 
71         WGPUBlendDescriptor blendDescriptor = {};
72         blendDescriptor.operation = WGPUBlendOperation_Add;
73         blendDescriptor.srcFactor = WGPUBlendFactor_One;
74         blendDescriptor.dstFactor = WGPUBlendFactor_One;
75         WGPUColorStateDescriptor colorStateDescriptor = {};
76         colorStateDescriptor.format = swapChainFormat;
77         colorStateDescriptor.alphaBlend = blendDescriptor;
78         colorStateDescriptor.colorBlend = blendDescriptor;
79         colorStateDescriptor.writeMask = WGPUColorWriteMask_All;
80 
81         descriptor.colorStateCount = 1;
82         descriptor.colorStates = &colorStateDescriptor;
83 
84         WGPUPipelineLayoutDescriptor pl = {};
85         pl.bindGroupLayoutCount = 0;
86         pl.bindGroupLayouts = nullptr;
87         descriptor.layout = wgpuDeviceCreatePipelineLayout(device, &pl);
88 
89         WGPUVertexStateDescriptor vertexState = {};
90         vertexState.indexFormat = WGPUIndexFormat_Uint32;
91         vertexState.vertexBufferCount = 0;
92         vertexState.vertexBuffers = nullptr;
93         descriptor.vertexState = &vertexState;
94 
95         WGPURasterizationStateDescriptor rasterizationState = {};
96         rasterizationState.frontFace = WGPUFrontFace_CCW;
97         rasterizationState.cullMode = WGPUCullMode_None;
98         rasterizationState.depthBias = 0;
99         rasterizationState.depthBiasSlopeScale = 0.0;
100         rasterizationState.depthBiasClamp = 0.0;
101         descriptor.rasterizationState = &rasterizationState;
102 
103         descriptor.primitiveTopology = WGPUPrimitiveTopology_TriangleList;
104         descriptor.sampleMask = 0xFFFFFFFF;
105         descriptor.alphaToCoverageEnabled = false;
106 
107         descriptor.depthStencilState = nullptr;
108 
109         pipeline = wgpuDeviceCreateRenderPipeline(device, &descriptor);
110     }
111 
112     wgpuShaderModuleRelease(vsModule);
113     wgpuShaderModuleRelease(fsModule);
114 }
115 
frame()116 void frame() {
117     WGPUTextureView backbufferView = wgpuSwapChainGetCurrentTextureView(swapchain);
118     WGPURenderPassDescriptor renderpassInfo = {};
119     WGPURenderPassColorAttachmentDescriptor colorAttachment = {};
120     {
121         colorAttachment.attachment = backbufferView;
122         colorAttachment.resolveTarget = nullptr;
123         colorAttachment.clearColor = { 0.0f, 0.0f, 0.0f, 0.0f };
124         colorAttachment.loadOp = WGPULoadOp_Clear;
125         colorAttachment.storeOp = WGPUStoreOp_Store;
126         renderpassInfo.colorAttachmentCount = 1;
127         renderpassInfo.colorAttachments = &colorAttachment;
128         renderpassInfo.depthStencilAttachment = nullptr;
129     }
130     WGPUCommandBuffer commands;
131     {
132         WGPUCommandEncoder encoder = wgpuDeviceCreateCommandEncoder(device, nullptr);
133 
134         WGPURenderPassEncoder pass = wgpuCommandEncoderBeginRenderPass(encoder, &renderpassInfo);
135         wgpuRenderPassEncoderSetPipeline(pass, pipeline);
136         wgpuRenderPassEncoderDraw(pass, 3, 1, 0, 0);
137         wgpuRenderPassEncoderEndPass(pass);
138         wgpuRenderPassEncoderRelease(pass);
139 
140         commands = wgpuCommandEncoderFinish(encoder, nullptr);
141         wgpuCommandEncoderRelease(encoder);
142     }
143 
144     wgpuQueueSubmit(queue, 1, &commands);
145     wgpuCommandBufferRelease(commands);
146     wgpuSwapChainPresent(swapchain);
147     wgpuTextureViewRelease(backbufferView);
148 
149     DoFlush();
150 }
151 
main(int argc,const char * argv[])152 int main(int argc, const char* argv[]) {
153     if (!InitSample(argc, argv)) {
154         return 1;
155     }
156     init();
157 
158     while (!ShouldQuit()) {
159         frame();
160         utils::USleep(16000);
161     }
162 
163     // TODO release stuff
164 }
165