2018-07-18 09:40:26 +00:00
|
|
|
// Copyright 2017 The Dawn Authors
|
2017-04-20 18:38:20 +00:00
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2017-06-19 17:15:13 +00:00
|
|
|
#include "SampleUtils.h"
|
2017-04-20 18:38:20 +00:00
|
|
|
|
2021-06-24 19:21:31 +00:00
|
|
|
#include "utils/ScopedAutoreleasePool.h"
|
2017-07-17 21:13:57 +00:00
|
|
|
#include "utils/SystemUtils.h"
|
2019-10-25 11:36:47 +00:00
|
|
|
#include "utils/WGPUHelpers.h"
|
2017-06-19 17:09:41 +00:00
|
|
|
|
2019-10-25 11:36:47 +00:00
|
|
|
WGPUDevice device;
|
|
|
|
WGPUQueue queue;
|
|
|
|
WGPUSwapChain swapchain;
|
|
|
|
WGPURenderPipeline pipeline;
|
2017-04-20 18:38:20 +00:00
|
|
|
|
2019-10-25 11:36:47 +00:00
|
|
|
WGPUTextureFormat swapChainFormat;
|
2018-01-19 17:51:18 +00:00
|
|
|
|
2017-04-20 18:38:20 +00:00
|
|
|
void init() {
|
2018-07-18 12:06:10 +00:00
|
|
|
device = CreateCppDawnDevice().Release();
|
2021-02-04 15:33:42 +00:00
|
|
|
queue = wgpuDeviceGetQueue(device);
|
2017-04-20 18:38:20 +00:00
|
|
|
|
2017-07-28 01:30:57 +00:00
|
|
|
{
|
2020-02-28 22:29:15 +00:00
|
|
|
WGPUSwapChainDescriptor descriptor = {};
|
2019-02-15 11:15:58 +00:00
|
|
|
descriptor.implementation = GetSwapChainImplementation();
|
2020-01-23 17:20:38 +00:00
|
|
|
swapchain = wgpuDeviceCreateSwapChain(device, nullptr, &descriptor);
|
2017-07-28 01:30:57 +00:00
|
|
|
}
|
2019-10-25 11:36:47 +00:00
|
|
|
swapChainFormat = static_cast<WGPUTextureFormat>(GetPreferredSwapChainTextureFormat());
|
2020-10-27 15:35:56 +00:00
|
|
|
wgpuSwapChainConfigure(swapchain, swapChainFormat, WGPUTextureUsage_RenderAttachment, 640, 480);
|
2017-07-28 01:30:57 +00:00
|
|
|
|
2021-06-17 11:25:09 +00:00
|
|
|
const char* vs = R"(
|
2022-01-25 16:29:04 +00:00
|
|
|
@stage(vertex) fn main(
|
|
|
|
@builtin(vertex_index) VertexIndex : u32
|
|
|
|
) -> @builtin(position) vec4<f32> {
|
2021-06-17 21:36:11 +00:00
|
|
|
var pos = array<vec2<f32>, 3>(
|
2021-06-17 11:25:09 +00:00
|
|
|
vec2<f32>( 0.0, 0.5),
|
|
|
|
vec2<f32>(-0.5, -0.5),
|
|
|
|
vec2<f32>( 0.5, -0.5)
|
|
|
|
);
|
|
|
|
return vec4<f32>(pos[VertexIndex], 0.0, 1.0);
|
|
|
|
})";
|
2021-03-24 15:55:32 +00:00
|
|
|
WGPUShaderModule vsModule = utils::CreateShaderModule(device, vs).Release();
|
2017-04-20 18:38:20 +00:00
|
|
|
|
2021-06-17 11:25:09 +00:00
|
|
|
const char* fs = R"(
|
2022-01-25 16:29:04 +00:00
|
|
|
@stage(fragment) fn main() -> @location(0) vec4<f32> {
|
2021-06-17 11:25:09 +00:00
|
|
|
return vec4<f32>(1.0, 0.0, 0.0, 1.0);
|
|
|
|
})";
|
2021-03-24 15:55:32 +00:00
|
|
|
WGPUShaderModule fsModule = utils::CreateShaderModule(device, fs).Release();
|
2017-04-20 18:38:20 +00:00
|
|
|
|
|
|
|
{
|
2021-05-21 05:01:38 +00:00
|
|
|
WGPURenderPipelineDescriptor descriptor = {};
|
2021-03-18 16:46:58 +00:00
|
|
|
|
|
|
|
// Fragment state
|
|
|
|
WGPUBlendState blend = {};
|
|
|
|
blend.color.operation = WGPUBlendOperation_Add;
|
|
|
|
blend.color.srcFactor = WGPUBlendFactor_One;
|
|
|
|
blend.color.dstFactor = WGPUBlendFactor_One;
|
|
|
|
blend.alpha.operation = WGPUBlendOperation_Add;
|
|
|
|
blend.alpha.srcFactor = WGPUBlendFactor_One;
|
|
|
|
blend.alpha.dstFactor = WGPUBlendFactor_One;
|
|
|
|
|
|
|
|
WGPUColorTargetState colorTarget = {};
|
|
|
|
colorTarget.format = swapChainFormat;
|
|
|
|
colorTarget.blend = &blend;
|
|
|
|
colorTarget.writeMask = WGPUColorWriteMask_All;
|
|
|
|
|
|
|
|
WGPUFragmentState fragment = {};
|
|
|
|
fragment.module = fsModule;
|
|
|
|
fragment.entryPoint = "main";
|
|
|
|
fragment.targetCount = 1;
|
|
|
|
fragment.targets = &colorTarget;
|
|
|
|
descriptor.fragment = &fragment;
|
|
|
|
|
|
|
|
// Other state
|
|
|
|
descriptor.layout = nullptr;
|
|
|
|
descriptor.depthStencil = nullptr;
|
|
|
|
|
|
|
|
descriptor.vertex.module = vsModule;
|
|
|
|
descriptor.vertex.entryPoint = "main";
|
|
|
|
descriptor.vertex.bufferCount = 0;
|
|
|
|
descriptor.vertex.buffers = nullptr;
|
|
|
|
|
|
|
|
descriptor.multisample.count = 1;
|
|
|
|
descriptor.multisample.mask = 0xFFFFFFFF;
|
|
|
|
descriptor.multisample.alphaToCoverageEnabled = false;
|
|
|
|
|
|
|
|
descriptor.primitive.frontFace = WGPUFrontFace_CCW;
|
|
|
|
descriptor.primitive.cullMode = WGPUCullMode_None;
|
|
|
|
descriptor.primitive.topology = WGPUPrimitiveTopology_TriangleList;
|
|
|
|
descriptor.primitive.stripIndexFormat = WGPUIndexFormat_Undefined;
|
|
|
|
|
2021-05-21 05:01:38 +00:00
|
|
|
pipeline = wgpuDeviceCreateRenderPipeline(device, &descriptor);
|
2017-04-20 18:38:20 +00:00
|
|
|
}
|
|
|
|
|
2019-10-25 11:36:47 +00:00
|
|
|
wgpuShaderModuleRelease(vsModule);
|
|
|
|
wgpuShaderModuleRelease(fsModule);
|
2017-04-20 18:38:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void frame() {
|
2019-11-12 18:30:11 +00:00
|
|
|
WGPUTextureView backbufferView = wgpuSwapChainGetCurrentTextureView(swapchain);
|
2020-02-28 22:29:15 +00:00
|
|
|
WGPURenderPassDescriptor renderpassInfo = {};
|
2021-09-10 15:50:19 +00:00
|
|
|
WGPURenderPassColorAttachment colorAttachment = {};
|
2017-07-28 01:30:57 +00:00
|
|
|
{
|
2021-09-14 10:42:22 +00:00
|
|
|
colorAttachment.view = backbufferView;
|
2018-12-19 08:21:13 +00:00
|
|
|
colorAttachment.resolveTarget = nullptr;
|
2020-07-10 20:33:08 +00:00
|
|
|
colorAttachment.clearColor = {0.0f, 0.0f, 0.0f, 0.0f};
|
2019-10-25 11:36:47 +00:00
|
|
|
colorAttachment.loadOp = WGPULoadOp_Clear;
|
|
|
|
colorAttachment.storeOp = WGPUStoreOp_Store;
|
2019-02-27 09:21:56 +00:00
|
|
|
renderpassInfo.colorAttachmentCount = 1;
|
2019-09-20 22:59:47 +00:00
|
|
|
renderpassInfo.colorAttachments = &colorAttachment;
|
2019-02-27 09:21:56 +00:00
|
|
|
renderpassInfo.depthStencilAttachment = nullptr;
|
2017-07-28 01:30:57 +00:00
|
|
|
}
|
2019-10-25 11:36:47 +00:00
|
|
|
WGPUCommandBuffer commands;
|
2017-04-20 18:38:20 +00:00
|
|
|
{
|
2019-10-25 11:36:47 +00:00
|
|
|
WGPUCommandEncoder encoder = wgpuDeviceCreateCommandEncoder(device, nullptr);
|
2018-09-21 00:24:37 +00:00
|
|
|
|
2019-10-25 11:36:47 +00:00
|
|
|
WGPURenderPassEncoder pass = wgpuCommandEncoderBeginRenderPass(encoder, &renderpassInfo);
|
|
|
|
wgpuRenderPassEncoderSetPipeline(pass, pipeline);
|
|
|
|
wgpuRenderPassEncoderDraw(pass, 3, 1, 0, 0);
|
|
|
|
wgpuRenderPassEncoderEndPass(pass);
|
|
|
|
wgpuRenderPassEncoderRelease(pass);
|
2018-09-21 00:24:37 +00:00
|
|
|
|
2019-10-25 11:36:47 +00:00
|
|
|
commands = wgpuCommandEncoderFinish(encoder, nullptr);
|
|
|
|
wgpuCommandEncoderRelease(encoder);
|
2017-04-20 18:38:20 +00:00
|
|
|
}
|
|
|
|
|
2019-10-25 11:36:47 +00:00
|
|
|
wgpuQueueSubmit(queue, 1, &commands);
|
|
|
|
wgpuCommandBufferRelease(commands);
|
2019-11-12 18:30:11 +00:00
|
|
|
wgpuSwapChainPresent(swapchain);
|
2019-10-25 11:36:47 +00:00
|
|
|
wgpuTextureViewRelease(backbufferView);
|
2017-04-20 18:38:20 +00:00
|
|
|
|
2017-07-28 01:30:57 +00:00
|
|
|
DoFlush();
|
2017-04-20 18:38:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, const char* argv[]) {
|
2017-06-19 17:15:13 +00:00
|
|
|
if (!InitSample(argc, argv)) {
|
2017-04-20 18:38:20 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
init();
|
|
|
|
|
|
|
|
while (!ShouldQuit()) {
|
2021-06-24 19:21:31 +00:00
|
|
|
utils::ScopedAutoreleasePool pool;
|
2017-04-20 18:38:20 +00:00
|
|
|
frame();
|
2017-07-17 21:13:57 +00:00
|
|
|
utils::USleep(16000);
|
2017-04-20 18:38:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO release stuff
|
|
|
|
}
|