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
|
|
|
|
2018-12-10 19:47:22 +00:00
|
|
|
#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"
|
2017-06-19 17:09:41 +00:00
|
|
|
|
2017-04-20 18:38:20 +00:00
|
|
|
#include <vector>
|
|
|
|
|
2018-07-18 11:45:46 +00:00
|
|
|
dawn::Device device;
|
2017-04-20 18:38:20 +00:00
|
|
|
|
2018-07-18 11:45:46 +00:00
|
|
|
dawn::Buffer indexBuffer;
|
|
|
|
dawn::Buffer vertexBuffer;
|
2017-04-20 18:38:20 +00:00
|
|
|
|
2018-07-18 11:45:46 +00:00
|
|
|
dawn::Texture texture;
|
|
|
|
dawn::Sampler sampler;
|
2017-04-20 18:38:20 +00:00
|
|
|
|
2018-07-18 11:45:46 +00:00
|
|
|
dawn::Queue queue;
|
|
|
|
dawn::SwapChain swapchain;
|
|
|
|
dawn::TextureView depthStencilView;
|
|
|
|
dawn::RenderPipeline pipeline;
|
|
|
|
dawn::BindGroup bindGroup;
|
2017-04-20 18:38:20 +00:00
|
|
|
|
|
|
|
void initBuffers() {
|
|
|
|
static const uint32_t indexData[3] = {
|
|
|
|
0, 1, 2,
|
|
|
|
};
|
2018-07-18 11:45:46 +00:00
|
|
|
indexBuffer = utils::CreateBufferFromData(device, indexData, sizeof(indexData), dawn::BufferUsageBit::Index);
|
2017-04-20 18:38:20 +00:00
|
|
|
|
|
|
|
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,
|
|
|
|
};
|
2018-07-18 11:45:46 +00:00
|
|
|
vertexBuffer = utils::CreateBufferFromData(device, vertexData, sizeof(vertexData), dawn::BufferUsageBit::Vertex);
|
2017-04-20 18:38:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void initTextures() {
|
2018-08-27 00:44:48 +00:00
|
|
|
dawn::TextureDescriptor descriptor;
|
|
|
|
descriptor.dimension = dawn::TextureDimension::e2D;
|
2018-09-18 12:49:22 +00:00
|
|
|
descriptor.size.width = 1024;
|
|
|
|
descriptor.size.height = 1024;
|
|
|
|
descriptor.size.depth = 1;
|
2019-02-21 00:45:19 +00:00
|
|
|
descriptor.arrayLayerCount = 1;
|
2018-12-12 09:27:16 +00:00
|
|
|
descriptor.sampleCount = 1;
|
2018-08-27 00:44:48 +00:00
|
|
|
descriptor.format = dawn::TextureFormat::R8G8B8A8Unorm;
|
2019-02-21 00:45:19 +00:00
|
|
|
descriptor.mipLevelCount = 1;
|
2018-08-27 00:44:48 +00:00
|
|
|
descriptor.usage = dawn::TextureUsageBit::TransferDst | dawn::TextureUsageBit::Sampled;
|
|
|
|
texture = device.CreateTexture(&descriptor);
|
2017-04-20 18:38:20 +00:00
|
|
|
|
2018-07-18 11:45:46 +00:00
|
|
|
dawn::SamplerDescriptor samplerDesc = utils::GetDefaultSamplerDescriptor();
|
2018-05-22 18:50:59 +00:00
|
|
|
sampler = device.CreateSampler(&samplerDesc);
|
2017-04-20 18:38:20 +00:00
|
|
|
|
|
|
|
// 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) {
|
2017-07-22 00:00:22 +00:00
|
|
|
data[i] = static_cast<uint8_t>(i % 253);
|
2017-04-20 18:38:20 +00:00
|
|
|
}
|
|
|
|
|
2018-07-18 11:45:46 +00:00
|
|
|
dawn::Buffer stagingBuffer = utils::CreateBufferFromData(device, data.data(), static_cast<uint32_t>(data.size()), dawn::BufferUsageBit::TransferSrc);
|
2018-11-28 17:54:13 +00:00
|
|
|
dawn::BufferCopyView bufferCopyView = utils::CreateBufferCopyView(stagingBuffer, 0, 0, 0);
|
2018-12-12 09:27:46 +00:00
|
|
|
dawn::TextureCopyView textureCopyView = utils::CreateTextureCopyView(texture, 0, 0, {0, 0, 0});
|
2018-11-28 17:54:13 +00:00
|
|
|
dawn::Extent3D copySize = {1024, 1024, 1};
|
2017-04-20 18:38:20 +00:00
|
|
|
|
2019-02-15 12:54:08 +00:00
|
|
|
dawn::CommandEncoder encoder = device.CreateCommandEncoder();
|
|
|
|
encoder.CopyBufferToTexture(&bufferCopyView, &textureCopyView, ©Size);
|
|
|
|
|
|
|
|
dawn::CommandBuffer copy = encoder.Finish();
|
2017-04-20 18:38:20 +00:00
|
|
|
queue.Submit(1, ©);
|
|
|
|
}
|
|
|
|
|
|
|
|
void init() {
|
2018-07-18 12:06:10 +00:00
|
|
|
device = CreateCppDawnDevice();
|
2017-04-20 18:38:20 +00:00
|
|
|
|
2018-06-15 00:26:27 +00:00
|
|
|
queue = device.CreateQueue();
|
2017-07-28 01:30:57 +00:00
|
|
|
swapchain = GetSwapChain(device);
|
2017-09-21 16:54:53 +00:00
|
|
|
swapchain.Configure(GetPreferredSwapChainTextureFormat(),
|
2018-07-18 11:45:46 +00:00
|
|
|
dawn::TextureUsageBit::OutputAttachment, 640, 480);
|
2017-04-20 18:38:20 +00:00
|
|
|
|
|
|
|
initBuffers();
|
|
|
|
initTextures();
|
|
|
|
|
2018-07-18 11:45:46 +00:00
|
|
|
dawn::ShaderModule vsModule = utils::CreateShaderModule(device, dawn::ShaderStage::Vertex, R"(
|
2017-04-20 18:38:20 +00:00
|
|
|
#version 450
|
|
|
|
layout(location = 0) in vec4 pos;
|
|
|
|
void main() {
|
|
|
|
gl_Position = pos;
|
|
|
|
})"
|
|
|
|
);
|
|
|
|
|
2018-07-18 11:45:46 +00:00
|
|
|
dawn::ShaderModule fsModule = utils::CreateShaderModule(device, dawn::ShaderStage::Fragment, R"(
|
2017-04-20 18:38:20 +00:00
|
|
|
#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;
|
2017-04-20 18:38:20 +00:00
|
|
|
void main() {
|
|
|
|
fragColor = texture(sampler2D(myTexture, mySampler), gl_FragCoord.xy / vec2(640.0, 480.0));
|
2017-08-29 17:37:45 +00:00
|
|
|
})");
|
2017-04-20 18:38:20 +00:00
|
|
|
|
2018-07-10 19:23:50 +00:00
|
|
|
auto bgl = utils::MakeBindGroupLayout(
|
|
|
|
device, {
|
2018-07-18 11:45:46 +00:00
|
|
|
{0, dawn::ShaderStageBit::Fragment, dawn::BindingType::Sampler},
|
|
|
|
{1, dawn::ShaderStageBit::Fragment, dawn::BindingType::SampledTexture},
|
2018-07-10 19:23:50 +00:00
|
|
|
});
|
2017-04-20 18:38:20 +00:00
|
|
|
|
2018-07-18 11:45:46 +00:00
|
|
|
dawn::PipelineLayout pl = utils::MakeBasicPipelineLayout(device, &bgl);
|
2017-04-20 18:38:20 +00:00
|
|
|
|
2017-07-28 01:30:57 +00:00
|
|
|
depthStencilView = CreateDefaultDepthStencilView(device);
|
|
|
|
|
2018-12-10 19:47:22 +00:00
|
|
|
utils::ComboRenderPipelineDescriptor descriptor(device);
|
|
|
|
descriptor.layout = utils::MakeBasicPipelineLayout(device, &bgl);
|
|
|
|
descriptor.cVertexStage.module = vsModule;
|
|
|
|
descriptor.cFragmentStage.module = fsModule;
|
2019-03-27 18:08:50 +00:00
|
|
|
descriptor.cInputState.numAttributes = 1;
|
2019-03-28 05:09:01 +00:00
|
|
|
descriptor.cInputState.cAttributes[0].format = dawn::VertexFormat::Float4;
|
2019-03-27 18:08:50 +00:00
|
|
|
descriptor.cInputState.numInputs = 1;
|
2019-03-28 05:09:01 +00:00
|
|
|
descriptor.cInputState.cInputs[0].stride = 4 * sizeof(float);
|
2019-02-15 02:20:57 +00:00
|
|
|
descriptor.depthStencilState = &descriptor.cDepthStencilState;
|
|
|
|
descriptor.cDepthStencilState.format = dawn::TextureFormat::D32FloatS8Uint;
|
2019-02-20 13:00:36 +00:00
|
|
|
descriptor.cColorStates[0]->format = GetPreferredSwapChainTextureFormat();
|
2018-12-10 19:47:22 +00:00
|
|
|
|
|
|
|
pipeline = device.CreateRenderPipeline(&descriptor);
|
2017-04-20 18:38:20 +00:00
|
|
|
|
2019-04-09 16:57:00 +00:00
|
|
|
dawn::TextureView view = texture.CreateDefaultView();
|
2017-04-20 18:38:20 +00:00
|
|
|
|
2018-12-05 07:18:30 +00:00
|
|
|
bindGroup = utils::MakeBindGroup(device, bgl, {
|
|
|
|
{0, sampler},
|
|
|
|
{1, view}
|
|
|
|
});
|
2017-04-20 18:38:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
2017-04-20 18:38:20 +00:00
|
|
|
if (s.b >= 1.0f) {s.b = 0.0f;}
|
2017-07-28 01:30:57 +00:00
|
|
|
|
2019-02-27 09:21:56 +00:00
|
|
|
dawn::Texture backbuffer = swapchain.GetNextTexture();
|
2019-04-09 16:57:00 +00:00
|
|
|
utils::ComboRenderPassDescriptor renderPass({backbuffer.CreateDefaultView()},
|
2019-02-27 09:21:56 +00:00
|
|
|
depthStencilView);
|
2017-07-28 01:30:57 +00:00
|
|
|
|
2019-04-05 20:51:29 +00:00
|
|
|
static const uint64_t vertexBufferOffsets[1] = {0};
|
2019-02-15 12:54:08 +00:00
|
|
|
dawn::CommandEncoder encoder = device.CreateCommandEncoder();
|
2018-09-21 00:24:37 +00:00
|
|
|
{
|
2019-02-27 09:21:56 +00:00
|
|
|
dawn::RenderPassEncoder pass = encoder.BeginRenderPass(&renderPass);
|
2018-12-21 10:40:26 +00:00
|
|
|
pass.SetPipeline(pipeline);
|
2019-03-18 06:01:37 +00:00
|
|
|
pass.SetBindGroup(0, bindGroup, 0, nullptr);
|
2018-09-21 00:24:37 +00:00
|
|
|
pass.SetVertexBuffers(0, 1, &vertexBuffer, vertexBufferOffsets);
|
|
|
|
pass.SetIndexBuffer(indexBuffer, 0);
|
2018-12-13 01:05:26 +00:00
|
|
|
pass.DrawIndexed(3, 1, 0, 0, 0);
|
2018-09-21 00:24:37 +00:00
|
|
|
pass.EndPass();
|
|
|
|
}
|
2017-04-20 18:38:20 +00:00
|
|
|
|
2019-02-15 12:54:08 +00:00
|
|
|
dawn::CommandBuffer commands = encoder.Finish();
|
2017-04-20 18:38:20 +00:00
|
|
|
queue.Submit(1, &commands);
|
2017-07-28 01:30:57 +00:00
|
|
|
swapchain.Present(backbuffer);
|
|
|
|
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()) {
|
|
|
|
frame();
|
2017-07-17 21:13:57 +00:00
|
|
|
utils::USleep(16000);
|
2017-04-20 18:38:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO release stuff
|
|
|
|
}
|