dawn-cmake/examples/CppHelloTriangle.cpp

190 lines
6.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"
#include "utils/ComboRenderPipelineDescriptor.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"
#include <vector>
2018-07-18 11:45:46 +00:00
dawn::Device device;
2018-07-18 11:45:46 +00:00
dawn::Buffer indexBuffer;
dawn::Buffer vertexBuffer;
2018-07-18 11:45:46 +00:00
dawn::Texture texture;
dawn::Sampler sampler;
2018-07-18 11:45:46 +00:00
dawn::Queue queue;
dawn::SwapChain swapchain;
dawn::TextureView depthStencilView;
dawn::RenderPipeline pipeline;
dawn::BindGroup bindGroup;
void initBuffers() {
static const uint32_t indexData[3] = {
0, 1, 2,
};
indexBuffer =
utils::CreateBufferFromData(device, indexData, sizeof(indexData), dawn::BufferUsage::Index);
static const float vertexData[12] = {
0.0f, 0.5f, 0.0f, 1.0f,
-0.5f, -0.5f, 0.0f, 1.0f,
0.5f, -0.5f, 0.0f, 1.0f,
};
vertexBuffer = utils::CreateBufferFromData(device, vertexData, sizeof(vertexData),
dawn::BufferUsage::Vertex);
}
void initTextures() {
dawn::TextureDescriptor descriptor;
descriptor.dimension = dawn::TextureDimension::e2D;
descriptor.size.width = 1024;
descriptor.size.height = 1024;
descriptor.size.depth = 1;
descriptor.arrayLayerCount = 1;
descriptor.sampleCount = 1;
descriptor.format = dawn::TextureFormat::RGBA8Unorm;
descriptor.mipLevelCount = 1;
descriptor.usage = dawn::TextureUsage::CopyDst | dawn::TextureUsage::Sampled;
texture = device.CreateTexture(&descriptor);
2018-07-18 11:45:46 +00:00
dawn::SamplerDescriptor samplerDesc = utils::GetDefaultSamplerDescriptor();
sampler = device.CreateSampler(&samplerDesc);
// Initialize the texture with arbitrary data until we can load images
std::vector<uint8_t> data(4 * 1024 * 1024, 0);
for (size_t i = 0; i < data.size(); ++i) {
data[i] = static_cast<uint8_t>(i % 253);
}
dawn::Buffer stagingBuffer = utils::CreateBufferFromData(
device, data.data(), static_cast<uint32_t>(data.size()), dawn::BufferUsage::CopySrc);
dawn::BufferCopyView bufferCopyView = utils::CreateBufferCopyView(stagingBuffer, 0, 0, 0);
dawn::TextureCopyView textureCopyView = utils::CreateTextureCopyView(texture, 0, 0, {0, 0, 0});
dawn::Extent3D copySize = {1024, 1024, 1};
dawn::CommandEncoder encoder = device.CreateCommandEncoder();
encoder.CopyBufferToTexture(&bufferCopyView, &textureCopyView, &copySize);
dawn::CommandBuffer copy = encoder.Finish();
queue.Submit(1, &copy);
}
void init() {
device = CreateCppDawnDevice();
queue = device.CreateQueue();
2017-07-28 01:30:57 +00:00
swapchain = GetSwapChain(device);
swapchain.Configure(GetPreferredSwapChainTextureFormat(), dawn::TextureUsage::OutputAttachment,
640, 480);
initBuffers();
initTextures();
dawn::ShaderModule vsModule =
utils::CreateShaderModule(device, utils::SingleShaderStage::Vertex, R"(
#version 450
layout(location = 0) in vec4 pos;
void main() {
gl_Position = pos;
})");
dawn::ShaderModule fsModule =
utils::CreateShaderModule(device, utils::SingleShaderStage::Fragment, R"(
#version 450
layout(set = 0, binding = 0) uniform sampler mySampler;
layout(set = 0, binding = 1) uniform texture2D myTexture;
2017-08-29 17:37:45 +00:00
layout(location = 0) out vec4 fragColor;
void main() {
fragColor = texture(sampler2D(myTexture, mySampler), gl_FragCoord.xy / vec2(640.0, 480.0));
2017-08-29 17:37:45 +00:00
})");
auto bgl = utils::MakeBindGroupLayout(
device, {
{0, dawn::ShaderStage::Fragment, dawn::BindingType::Sampler},
{1, dawn::ShaderStage::Fragment, dawn::BindingType::SampledTexture},
});
2018-07-18 11:45:46 +00:00
dawn::PipelineLayout pl = utils::MakeBasicPipelineLayout(device, &bgl);
2017-07-28 01:30:57 +00:00
depthStencilView = CreateDefaultDepthStencilView(device);
utils::ComboRenderPipelineDescriptor descriptor(device);
descriptor.layout = utils::MakeBasicPipelineLayout(device, &bgl);
descriptor.vertexStage.module = vsModule;
descriptor.cFragmentStage.module = fsModule;
descriptor.cVertexInput.bufferCount = 1;
descriptor.cVertexInput.cBuffers[0].stride = 4 * sizeof(float);
descriptor.cVertexInput.cBuffers[0].attributeCount = 1;
descriptor.cVertexInput.cAttributes[0].format = dawn::VertexFormat::Float4;
descriptor.depthStencilState = &descriptor.cDepthStencilState;
descriptor.cDepthStencilState.format = dawn::TextureFormat::Depth24PlusStencil8;
descriptor.cColorStates[0].format = GetPreferredSwapChainTextureFormat();
pipeline = device.CreateRenderPipeline(&descriptor);
dawn::TextureView view = texture.CreateView();
bindGroup = utils::MakeBindGroup(device, bgl, {
{0, sampler},
{1, view}
});
}
struct {uint32_t a; float b;} s;
void frame() {
s.a = (s.a + 1) % 256;
2017-07-11 01:44:06 +00:00
s.b += 0.02f;
if (s.b >= 1.0f) {s.b = 0.0f;}
2017-07-28 01:30:57 +00:00
dawn::Texture backbuffer = swapchain.GetNextTexture();
utils::ComboRenderPassDescriptor renderPass({backbuffer.CreateView()}, depthStencilView);
2017-07-28 01:30:57 +00:00
static const uint64_t vertexBufferOffsets[1] = {0};
dawn::CommandEncoder encoder = device.CreateCommandEncoder();
{
dawn::RenderPassEncoder pass = encoder.BeginRenderPass(&renderPass);
pass.SetPipeline(pipeline);
pass.SetBindGroup(0, bindGroup, 0, nullptr);
pass.SetVertexBuffers(0, 1, &vertexBuffer, vertexBufferOffsets);
pass.SetIndexBuffer(indexBuffer, 0);
pass.DrawIndexed(3, 1, 0, 0, 0);
pass.EndPass();
}
dawn::CommandBuffer commands = encoder.Finish();
queue.Submit(1, &commands);
2017-07-28 01:30:57 +00:00
swapchain.Present(backbuffer);
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
}