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;
|
|
|
|
descriptor.size.depth = 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
|
|
|
|
2019-10-25 11:36:47 +00:00
|
|
|
wgpu::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
|
|
|
}
|
|
|
|
|
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);
|
2020-11-06 13:41:50 +00:00
|
|
|
wgpu::BufferCopyView bufferCopyView = utils::CreateBufferCopyView(stagingBuffer, 0, 4 * 1024);
|
2020-06-16 03:05:17 +00:00
|
|
|
wgpu::TextureCopyView textureCopyView = utils::CreateTextureCopyView(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();
|
2019-02-15 12:54:08 +00:00
|
|
|
encoder.CopyBufferToTexture(&bufferCopyView, &textureCopyView, ©Size);
|
|
|
|
|
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();
|
|
|
|
|
2020-11-26 16:39:46 +00:00
|
|
|
wgpu::ShaderModule vsModule = utils::CreateShaderModuleFromWGSL(device, R"(
|
|
|
|
[[builtin(position)]] var<out> Position : vec4<f32>;
|
|
|
|
[[location(0)]] var<in> pos : vec4<f32>;
|
|
|
|
[[stage(vertex)]] fn main() -> void {
|
|
|
|
Position = pos;
|
|
|
|
return;
|
2019-07-12 17:52:22 +00:00
|
|
|
})");
|
2017-04-20 18:38:20 +00:00
|
|
|
|
2020-11-26 16:39:46 +00:00
|
|
|
wgpu::ShaderModule fsModule = utils::CreateShaderModuleFromWGSL(device, R"(
|
|
|
|
[[builtin(frag_coord)]] var<in> FragCoord : vec4<f32>;
|
2021-01-19 14:18:51 +00:00
|
|
|
[[group(0), binding(0)]] var<uniform_constant> mySampler: sampler;
|
|
|
|
[[group(0), binding(1)]] var<uniform_constant> myTexture : texture_2d<f32>;
|
2017-04-20 18:38:20 +00:00
|
|
|
|
2020-11-26 16:39:46 +00:00
|
|
|
[[location(0)]] var<out> FragColor : vec4<f32>;
|
|
|
|
[[stage(fragment)]] fn main() -> void {
|
|
|
|
FragColor = textureSample(myTexture, mySampler, FragCoord.xy / vec2<f32>(640.0, 480.0));
|
|
|
|
return;
|
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, {
|
2019-10-25 11:36:47 +00:00
|
|
|
{0, wgpu::ShaderStage::Fragment, wgpu::BindingType::Sampler},
|
|
|
|
{1, wgpu::ShaderStage::Fragment, wgpu::BindingType::SampledTexture},
|
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);
|
|
|
|
|
2018-12-10 19:47:22 +00:00
|
|
|
utils::ComboRenderPipelineDescriptor descriptor(device);
|
|
|
|
descriptor.layout = utils::MakeBasicPipelineLayout(device, &bgl);
|
2019-09-05 09:35:07 +00:00
|
|
|
descriptor.vertexStage.module = vsModule;
|
2018-12-10 19:47:22 +00:00
|
|
|
descriptor.cFragmentStage.module = fsModule;
|
2019-11-07 22:23:29 +00:00
|
|
|
descriptor.cVertexState.vertexBufferCount = 1;
|
|
|
|
descriptor.cVertexState.cVertexBuffers[0].arrayStride = 4 * sizeof(float);
|
|
|
|
descriptor.cVertexState.cVertexBuffers[0].attributeCount = 1;
|
|
|
|
descriptor.cVertexState.cAttributes[0].format = wgpu::VertexFormat::Float4;
|
2019-02-15 02:20:57 +00:00
|
|
|
descriptor.depthStencilState = &descriptor.cDepthStencilState;
|
2019-10-25 11:36:47 +00:00
|
|
|
descriptor.cDepthStencilState.format = wgpu::TextureFormat::Depth24PlusStencil8;
|
2019-09-20 23:22:27 +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-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
|
|
|
|
}
|