dawn-cmake/examples/CHelloTriangle.cpp

119 lines
4.3 KiB
C++
Raw Normal View History

// Copyright 2017 The Dawn Authors
//
// 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.
#include "SampleUtils.h"
2018-07-18 12:00:56 +00:00
#include "utils/DawnHelpers.h"
2017-07-17 21:13:57 +00:00
#include "utils/SystemUtils.h"
dawnDevice device;
dawnQueue queue;
dawnSwapChain swapchain;
dawnRenderPipeline pipeline;
dawnTextureFormat swapChainFormat;
void init() {
device = CreateCppDawnDevice().Release();
queue = dawnDeviceCreateQueue(device);
2017-07-28 01:30:57 +00:00
{
dawnSwapChainBuilder builder = dawnDeviceCreateSwapChainBuilder(device);
2017-07-28 01:30:57 +00:00
uint64_t swapchainImpl = GetSwapChainImplementation();
dawnSwapChainBuilderSetImplementation(builder, swapchainImpl);
swapchain = dawnSwapChainBuilderGetResult(builder);
dawnSwapChainBuilderRelease(builder);
2017-07-28 01:30:57 +00:00
}
swapChainFormat = static_cast<dawnTextureFormat>(GetPreferredSwapChainTextureFormat());
dawnSwapChainConfigure(swapchain, swapChainFormat, DAWN_TEXTURE_USAGE_BIT_OUTPUT_ATTACHMENT, 640,
480);
2017-07-28 01:30:57 +00:00
const char* vs =
"#version 450\n"
"const vec2 pos[3] = vec2[3](vec2(0.0f, 0.5f), vec2(-0.5f, -0.5f), vec2(0.5f, -0.5f));\n"
"void main() {\n"
" gl_Position = vec4(pos[gl_VertexIndex], 0.0, 1.0);\n"
"}\n";
dawnShaderModule vsModule = utils::CreateShaderModule(dawn::Device(device), dawn::ShaderStage::Vertex, vs).Release();
const char* fs =
"#version 450\n"
2017-08-29 17:37:45 +00:00
"layout(location = 0) out vec4 fragColor;"
"void main() {\n"
" fragColor = vec4(1.0, 0.0, 0.0, 1.0);\n"
"}\n";
dawnShaderModule fsModule = utils::CreateShaderModule(device, dawn::ShaderStage::Fragment, fs).Release();
{
dawnRenderPipelineBuilder builder = dawnDeviceCreateRenderPipelineBuilder(device);
dawnRenderPipelineBuilderSetColorAttachmentFormat(builder, 0, swapChainFormat);
dawnRenderPipelineBuilderSetStage(builder, DAWN_SHADER_STAGE_VERTEX, vsModule, "main");
dawnRenderPipelineBuilderSetStage(builder, DAWN_SHADER_STAGE_FRAGMENT, fsModule, "main");
pipeline = dawnRenderPipelineBuilderGetResult(builder);
dawnRenderPipelineBuilderRelease(builder);
}
dawnShaderModuleRelease(vsModule);
dawnShaderModuleRelease(fsModule);
}
void frame() {
dawnTexture backbuffer = dawnSwapChainGetNextTexture(swapchain);
dawnTextureView backbufferView;
2017-07-28 01:30:57 +00:00
{
dawnTextureViewBuilder builder = dawnTextureCreateTextureViewBuilder(backbuffer);
backbufferView = dawnTextureViewBuilderGetResult(builder);
dawnTextureViewBuilderRelease(builder);
2017-07-28 01:30:57 +00:00
}
dawnRenderPassDescriptor renderpassInfo;
2017-07-28 01:30:57 +00:00
{
dawnRenderPassDescriptorBuilder builder = dawnDeviceCreateRenderPassDescriptorBuilder(device);
dawnRenderPassDescriptorBuilderSetColorAttachment(builder, 0, backbufferView, DAWN_LOAD_OP_CLEAR);
renderpassInfo = dawnRenderPassDescriptorBuilderGetResult(builder);
dawnRenderPassDescriptorBuilderRelease(builder);
2017-07-28 01:30:57 +00:00
}
dawnCommandBuffer commands;
{
dawnCommandBufferBuilder builder = dawnDeviceCreateCommandBufferBuilder(device);
dawnCommandBufferBuilderBeginRenderPass(builder, renderpassInfo);
dawnCommandBufferBuilderSetRenderPipeline(builder, pipeline);
dawnCommandBufferBuilderDrawArrays(builder, 3, 1, 0, 0);
dawnCommandBufferBuilderEndRenderPass(builder);
commands = dawnCommandBufferBuilderGetResult(builder);
dawnCommandBufferBuilderRelease(builder);
}
dawnQueueSubmit(queue, 1, &commands);
dawnCommandBufferRelease(commands);
dawnSwapChainPresent(swapchain, backbuffer);
dawnRenderPassDescriptorRelease(renderpassInfo);
dawnTextureViewRelease(backbufferView);
2017-07-28 01:30:57 +00:00
DoFlush();
}
int main(int argc, const char* argv[]) {
if (!InitSample(argc, argv)) {
return 1;
}
init();
while (!ShouldQuit()) {
frame();
2017-07-17 21:13:57 +00:00
utils::USleep(16000);
}
// TODO release stuff
}