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/ScopedAutoreleasePool.h"
18 #include "utils/SystemUtils.h"
19 #include "utils/WGPUHelpers.h"
20
21 WGPUDevice device;
22 WGPUQueue queue;
23 WGPUSwapChain swapchain;
24 WGPURenderPipeline pipeline;
25
26 WGPUTextureFormat swapChainFormat;
27
init()28 void init() {
29 device = CreateCppDawnDevice().Release();
30 queue = wgpuDeviceGetQueue(device);
31
32 {
33 WGPUSwapChainDescriptor descriptor = {};
34 descriptor.implementation = GetSwapChainImplementation();
35 swapchain = wgpuDeviceCreateSwapChain(device, nullptr, &descriptor);
36 }
37 swapChainFormat = static_cast<WGPUTextureFormat>(GetPreferredSwapChainTextureFormat());
38 wgpuSwapChainConfigure(swapchain, swapChainFormat, WGPUTextureUsage_RenderAttachment, 640, 480);
39
40 const char* vs = R"(
41 [[stage(vertex)]] fn main(
42 [[builtin(vertex_index)]] VertexIndex : u32
43 ) -> [[builtin(position)]] vec4<f32> {
44 var pos = array<vec2<f32>, 3>(
45 vec2<f32>( 0.0, 0.5),
46 vec2<f32>(-0.5, -0.5),
47 vec2<f32>( 0.5, -0.5)
48 );
49 return vec4<f32>(pos[VertexIndex], 0.0, 1.0);
50 })";
51 WGPUShaderModule vsModule = utils::CreateShaderModule(device, vs).Release();
52
53 const char* fs = R"(
54 [[stage(fragment)]] fn main() -> [[location(0)]] vec4<f32> {
55 return vec4<f32>(1.0, 0.0, 0.0, 1.0);
56 })";
57 WGPUShaderModule fsModule = utils::CreateShaderModule(device, fs).Release();
58
59 {
60 WGPURenderPipelineDescriptor descriptor = {};
61
62 // Fragment state
63 WGPUBlendState blend = {};
64 blend.color.operation = WGPUBlendOperation_Add;
65 blend.color.srcFactor = WGPUBlendFactor_One;
66 blend.color.dstFactor = WGPUBlendFactor_One;
67 blend.alpha.operation = WGPUBlendOperation_Add;
68 blend.alpha.srcFactor = WGPUBlendFactor_One;
69 blend.alpha.dstFactor = WGPUBlendFactor_One;
70
71 WGPUColorTargetState colorTarget = {};
72 colorTarget.format = swapChainFormat;
73 colorTarget.blend = &blend;
74 colorTarget.writeMask = WGPUColorWriteMask_All;
75
76 WGPUFragmentState fragment = {};
77 fragment.module = fsModule;
78 fragment.entryPoint = "main";
79 fragment.targetCount = 1;
80 fragment.targets = &colorTarget;
81 descriptor.fragment = &fragment;
82
83 // Other state
84 descriptor.layout = nullptr;
85 descriptor.depthStencil = nullptr;
86
87 descriptor.vertex.module = vsModule;
88 descriptor.vertex.entryPoint = "main";
89 descriptor.vertex.bufferCount = 0;
90 descriptor.vertex.buffers = nullptr;
91
92 descriptor.multisample.count = 1;
93 descriptor.multisample.mask = 0xFFFFFFFF;
94 descriptor.multisample.alphaToCoverageEnabled = false;
95
96 descriptor.primitive.frontFace = WGPUFrontFace_CCW;
97 descriptor.primitive.cullMode = WGPUCullMode_None;
98 descriptor.primitive.topology = WGPUPrimitiveTopology_TriangleList;
99 descriptor.primitive.stripIndexFormat = WGPUIndexFormat_Undefined;
100
101 pipeline = wgpuDeviceCreateRenderPipeline(device, &descriptor);
102 }
103
104 wgpuShaderModuleRelease(vsModule);
105 wgpuShaderModuleRelease(fsModule);
106 }
107
frame()108 void frame() {
109 WGPUTextureView backbufferView = wgpuSwapChainGetCurrentTextureView(swapchain);
110 WGPURenderPassDescriptor renderpassInfo = {};
111 WGPURenderPassColorAttachment colorAttachment = {};
112 {
113 colorAttachment.view = backbufferView;
114 colorAttachment.resolveTarget = nullptr;
115 colorAttachment.clearColor = {0.0f, 0.0f, 0.0f, 0.0f};
116 colorAttachment.loadOp = WGPULoadOp_Clear;
117 colorAttachment.storeOp = WGPUStoreOp_Store;
118 renderpassInfo.colorAttachmentCount = 1;
119 renderpassInfo.colorAttachments = &colorAttachment;
120 renderpassInfo.depthStencilAttachment = nullptr;
121 }
122 WGPUCommandBuffer commands;
123 {
124 WGPUCommandEncoder encoder = wgpuDeviceCreateCommandEncoder(device, nullptr);
125
126 WGPURenderPassEncoder pass = wgpuCommandEncoderBeginRenderPass(encoder, &renderpassInfo);
127 wgpuRenderPassEncoderSetPipeline(pass, pipeline);
128 wgpuRenderPassEncoderDraw(pass, 3, 1, 0, 0);
129 wgpuRenderPassEncoderEndPass(pass);
130 wgpuRenderPassEncoderRelease(pass);
131
132 commands = wgpuCommandEncoderFinish(encoder, nullptr);
133 wgpuCommandEncoderRelease(encoder);
134 }
135
136 wgpuQueueSubmit(queue, 1, &commands);
137 wgpuCommandBufferRelease(commands);
138 wgpuSwapChainPresent(swapchain);
139 wgpuTextureViewRelease(backbufferView);
140
141 DoFlush();
142 }
143
main(int argc,const char * argv[])144 int main(int argc, const char* argv[]) {
145 if (!InitSample(argc, argv)) {
146 return 1;
147 }
148 init();
149
150 while (!ShouldQuit()) {
151 utils::ScopedAutoreleasePool pool;
152 frame();
153 utils::USleep(16000);
154 }
155
156 // TODO release stuff
157 }
158