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"
|
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
|
|
|
|
2017-04-20 18:38:20 +00:00
|
|
|
#include <vector>
|
|
|
|
|
2019-10-25 11:36:47 +00:00
|
|
|
wgpu::Device device;
|
2017-04-20 18:38:20 +00:00
|
|
|
|
2019-10-25 11:36:47 +00:00
|
|
|
wgpu::Buffer indexBuffer;
|
|
|
|
wgpu::Buffer vertexBuffer;
|
2017-04-20 18:38:20 +00:00
|
|
|
|
2019-10-25 11:36:47 +00:00
|
|
|
wgpu::Texture texture;
|
|
|
|
wgpu::Sampler sampler;
|
2017-04-20 18:38:20 +00:00
|
|
|
|
2019-10-25 11:36:47 +00:00
|
|
|
wgpu::Queue queue;
|
|
|
|
wgpu::SwapChain swapchain;
|
|
|
|
wgpu::TextureView depthStencilView;
|
|
|
|
wgpu::RenderPipeline pipeline;
|
|
|
|
wgpu::BindGroup bindGroup;
|
2017-04-20 18:38:20 +00:00
|
|
|
|
|
|
|
void initBuffers() {
|
|
|
|
static const uint32_t indexData[3] = {
|
2020-07-10 20:33:08 +00:00
|
|
|
0,
|
|
|
|
1,
|
|
|
|
2,
|
2017-04-20 18:38:20 +00:00
|
|
|
};
|
2019-08-27 08:21:39 +00:00
|
|
|
indexBuffer =
|
2019-10-25 11:36:47 +00:00
|
|
|
utils::CreateBufferFromData(device, indexData, sizeof(indexData), wgpu::BufferUsage::Index);
|
2017-04-20 18:38:20 +00:00
|
|
|
|
|
|
|
static const float vertexData[12] = {
|
2020-07-10 20:33:08 +00:00
|
|
|
0.0f, 0.5f, 0.0f, 1.0f, -0.5f, -0.5f, 0.0f, 1.0f, 0.5f, -0.5f, 0.0f, 1.0f,
|
2017-04-20 18:38:20 +00:00
|
|
|
};
|
2019-08-27 08:21:39 +00:00
|
|
|
vertexBuffer = utils::CreateBufferFromData(device, vertexData, sizeof(vertexData),
|
2019-10-25 11:36:47 +00:00
|
|
|
wgpu::BufferUsage::Vertex);
|
2017-04-20 18:38:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void initTextures() {
|
2019-10-25 11:36:47 +00:00
|
|
|
wgpu::TextureDescriptor descriptor;
|
|
|
|
descriptor.dimension = wgpu::TextureDimension::e2D;
|
2018-09-18 12:49:22 +00:00
|
|
|
descriptor.size.width = 1024;
|
|
|
|
descriptor.size.height = 1024;
|
2021-03-22 21:12:36 +00:00
|
|
|
descriptor.size.depthOrArrayLayers = 1;
|
2018-12-12 09:27:16 +00:00
|
|
|
descriptor.sampleCount = 1;
|
2019-10-25 11:36:47 +00:00
|
|
|
descriptor.format = wgpu::TextureFormat::RGBA8Unorm;
|
2019-02-21 00:45:19 +00:00
|
|
|
descriptor.mipLevelCount = 1;
|
2019-10-25 11:36:47 +00:00
|
|
|
descriptor.usage = wgpu::TextureUsage::CopyDst | wgpu::TextureUsage::Sampled;
|
2018-08-27 00:44:48 +00:00
|
|
|
texture = device.CreateTexture(&descriptor);
|
2017-04-20 18:38:20 +00:00
|
|
|
|
2021-02-05 20:26:54 +00:00
|
|
|
sampler = device.CreateSampler();
|
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
|
|
|
}
|
|
|
|
|
2019-10-25 11:36:47 +00:00
|
|
|
wgpu::Buffer stagingBuffer = utils::CreateBufferFromData(
|
|
|
|
device, data.data(), static_cast<uint32_t>(data.size()), wgpu::BufferUsage::CopySrc);
|
2021-03-04 18:13:45 +00:00
|
|
|
wgpu::ImageCopyBuffer imageCopyBuffer =
|
|
|
|
utils::CreateImageCopyBuffer(stagingBuffer, 0, 4 * 1024);
|
|
|
|
wgpu::ImageCopyTexture imageCopyTexture = utils::CreateImageCopyTexture(texture, 0, {0, 0, 0});
|
2019-10-25 11:36:47 +00:00
|
|
|
wgpu::Extent3D copySize = {1024, 1024, 1};
|
2017-04-20 18:38:20 +00:00
|
|
|
|
2019-10-25 11:36:47 +00:00
|
|
|
wgpu::CommandEncoder encoder = device.CreateCommandEncoder();
|
2021-03-04 18:13:45 +00:00
|
|
|
encoder.CopyBufferToTexture(&imageCopyBuffer, &imageCopyTexture, ©Size);
|
2019-02-15 12:54:08 +00:00
|
|
|
|
2019-10-25 11:36:47 +00:00
|
|
|
wgpu::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
|
|
|
|
2021-02-04 15:33:42 +00:00
|
|
|
queue = device.GetQueue();
|
2017-07-28 01:30:57 +00:00
|
|
|
swapchain = GetSwapChain(device);
|
2020-10-27 15:35:56 +00:00
|
|
|
swapchain.Configure(GetPreferredSwapChainTextureFormat(), wgpu::TextureUsage::RenderAttachment,
|
2019-08-27 08:21:39 +00:00
|
|
|
640, 480);
|
2017-04-20 18:38:20 +00:00
|
|
|
|
|
|
|
initBuffers();
|
|
|
|
initTextures();
|
|
|
|
|
2021-03-24 15:55:32 +00:00
|
|
|
wgpu::ShaderModule vsModule = utils::CreateShaderModule(device, R"(
|
2021-04-13 14:52:44 +00:00
|
|
|
[[stage(vertex)]] fn main([[location(0)]] pos : vec4<f32>)
|
|
|
|
-> [[builtin(position)]] vec4<f32> {
|
|
|
|
return pos;
|
2019-07-12 17:52:22 +00:00
|
|
|
})");
|
2017-04-20 18:38:20 +00:00
|
|
|
|
2021-03-24 15:55:32 +00:00
|
|
|
wgpu::ShaderModule fsModule = utils::CreateShaderModule(device, R"(
|
2021-02-10 20:17:14 +00:00
|
|
|
[[group(0), binding(0)]] var mySampler: sampler;
|
|
|
|
[[group(0), binding(1)]] var myTexture : texture_2d<f32>;
|
2017-04-20 18:38:20 +00:00
|
|
|
|
2021-04-13 14:52:44 +00:00
|
|
|
[[stage(fragment)]] fn main([[builtin(frag_coord)]] FragCoord : vec4<f32>)
|
|
|
|
-> [[location(0)]] vec4<f32> {
|
|
|
|
return textureSample(myTexture, mySampler, FragCoord.xy / vec2<f32>(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, {
|
2021-03-18 16:46:58 +00:00
|
|
|
{0, wgpu::ShaderStage::Fragment, wgpu::SamplerBindingType::Filtering},
|
|
|
|
{1, wgpu::ShaderStage::Fragment, wgpu::TextureSampleType::Float},
|
2018-07-10 19:23:50 +00:00
|
|
|
});
|
2017-04-20 18:38:20 +00:00
|
|
|
|
2019-10-25 11:36:47 +00:00
|
|
|
wgpu::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);
|
|
|
|
|
2021-03-18 16:46:58 +00:00
|
|
|
utils::ComboRenderPipelineDescriptor2 descriptor;
|
2018-12-10 19:47:22 +00:00
|
|
|
descriptor.layout = utils::MakeBasicPipelineLayout(device, &bgl);
|
2021-03-18 16:46:58 +00:00
|
|
|
descriptor.vertex.module = vsModule;
|
|
|
|
descriptor.vertex.bufferCount = 1;
|
|
|
|
descriptor.cBuffers[0].arrayStride = 4 * sizeof(float);
|
|
|
|
descriptor.cBuffers[0].attributeCount = 1;
|
|
|
|
descriptor.cAttributes[0].format = wgpu::VertexFormat::Float32x4;
|
|
|
|
descriptor.cFragment.module = fsModule;
|
|
|
|
descriptor.cTargets[0].format = GetPreferredSwapChainTextureFormat();
|
|
|
|
descriptor.EnableDepthStencil(wgpu::TextureFormat::Depth24PlusStencil8);
|
|
|
|
|
|
|
|
pipeline = device.CreateRenderPipeline2(&descriptor);
|
2017-04-20 18:38:20 +00:00
|
|
|
|
2019-10-25 11:36:47 +00:00
|
|
|
wgpu::TextureView view = texture.CreateView();
|
2017-04-20 18:38:20 +00:00
|
|
|
|
2020-07-10 20:33:08 +00:00
|
|
|
bindGroup = utils::MakeBindGroup(device, bgl, {{0, sampler}, {1, view}});
|
2017-04-20 18:38:20 +00:00
|
|
|
}
|
|
|
|
|
2020-07-10 20:33:08 +00:00
|
|
|
struct {
|
|
|
|
uint32_t a;
|
|
|
|
float b;
|
|
|
|
} s;
|
2017-04-20 18:38:20 +00:00
|
|
|
void frame() {
|
|
|
|
s.a = (s.a + 1) % 256;
|
2017-07-11 01:44:06 +00:00
|
|
|
s.b += 0.02f;
|
2020-07-10 20:33:08 +00:00
|
|
|
if (s.b >= 1.0f) {
|
|
|
|
s.b = 0.0f;
|
|
|
|
}
|
2017-07-28 01:30:57 +00:00
|
|
|
|
2019-11-12 18:30:11 +00:00
|
|
|
wgpu::TextureView backbufferView = swapchain.GetCurrentTextureView();
|
|
|
|
utils::ComboRenderPassDescriptor renderPass({backbufferView}, depthStencilView);
|
2017-07-28 01:30:57 +00:00
|
|
|
|
2019-10-25 11:36:47 +00:00
|
|
|
wgpu::CommandEncoder encoder = device.CreateCommandEncoder();
|
2018-09-21 00:24:37 +00:00
|
|
|
{
|
2019-10-25 11:36:47 +00:00
|
|
|
wgpu::RenderPassEncoder pass = encoder.BeginRenderPass(&renderPass);
|
2018-12-21 10:40:26 +00:00
|
|
|
pass.SetPipeline(pipeline);
|
2019-10-09 16:08:42 +00:00
|
|
|
pass.SetBindGroup(0, bindGroup);
|
2019-10-10 07:29:58 +00:00
|
|
|
pass.SetVertexBuffer(0, vertexBuffer);
|
2020-11-25 08:54:14 +00:00
|
|
|
pass.SetIndexBuffer(indexBuffer, wgpu::IndexFormat::Uint32);
|
2020-03-31 16:21:35 +00:00
|
|
|
pass.DrawIndexed(3);
|
2018-09-21 00:24:37 +00:00
|
|
|
pass.EndPass();
|
|
|
|
}
|
2017-04-20 18:38:20 +00:00
|
|
|
|
2019-10-25 11:36:47 +00:00
|
|
|
wgpu::CommandBuffer commands = encoder.Finish();
|
2017-04-20 18:38:20 +00:00
|
|
|
queue.Submit(1, &commands);
|
2019-11-12 18:30:11 +00:00
|
|
|
swapchain.Present();
|
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()) {
|
|
|
|
frame();
|
2017-07-17 21:13:57 +00:00
|
|
|
utils::USleep(16000);
|
2017-04-20 18:38:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO release stuff
|
|
|
|
}
|