2017-04-20 18:38:20 +00:00
|
|
|
// Copyright 2017 The NXT 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.
|
|
|
|
|
2017-06-19 17:15:13 +00:00
|
|
|
#include "SampleUtils.h"
|
2017-04-20 18:38:20 +00:00
|
|
|
|
2017-06-19 17:09:41 +00:00
|
|
|
#include "utils/NXTHelpers.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 <string.h>
|
|
|
|
|
|
|
|
nxt::Device device;
|
|
|
|
nxt::Queue queue;
|
2017-07-28 01:30:57 +00:00
|
|
|
nxt::SwapChain swapchain;
|
|
|
|
nxt::TextureView depthStencilView;
|
2017-04-20 18:38:20 +00:00
|
|
|
nxt::Buffer buffer;
|
2017-07-14 15:32:57 +00:00
|
|
|
nxt::RenderPipeline renderPipeline;
|
2017-04-20 18:38:20 +00:00
|
|
|
nxt::BindGroup renderBindGroup;
|
2017-07-14 15:32:57 +00:00
|
|
|
nxt::ComputePipeline computePipeline;
|
2017-04-20 18:38:20 +00:00
|
|
|
nxt::BindGroup computeBindGroup;
|
|
|
|
|
|
|
|
void init() {
|
2017-05-29 18:30:29 +00:00
|
|
|
device = CreateCppNXTDevice();
|
2017-04-20 18:38:20 +00:00
|
|
|
|
|
|
|
queue = device.CreateQueueBuilder().GetResult();
|
2017-07-28 01:30:57 +00:00
|
|
|
swapchain = GetSwapChain(device);
|
2017-09-21 16:54:53 +00:00
|
|
|
swapchain.Configure(GetPreferredSwapChainTextureFormat(),
|
|
|
|
nxt::TextureUsageBit::OutputAttachment, 640, 480);
|
2017-04-20 18:38:20 +00:00
|
|
|
|
|
|
|
struct {uint32_t a; float b;} s;
|
2017-07-22 00:00:22 +00:00
|
|
|
memset(&s, 0, sizeof(s));
|
2017-04-20 18:38:20 +00:00
|
|
|
buffer = device.CreateBufferBuilder()
|
2017-06-12 21:33:44 +00:00
|
|
|
.SetAllowedUsage(nxt::BufferUsageBit::TransferDst | nxt::BufferUsageBit::Uniform | nxt::BufferUsageBit::Storage)
|
|
|
|
.SetInitialUsage(nxt::BufferUsageBit::TransferDst)
|
2017-04-20 18:38:20 +00:00
|
|
|
.SetSize(sizeof(s))
|
|
|
|
.GetResult();
|
2018-02-04 16:07:02 +00:00
|
|
|
buffer.SetSubData(0, sizeof(s), reinterpret_cast<uint8_t*>(&s));
|
2017-04-20 18:38:20 +00:00
|
|
|
|
|
|
|
nxt::BufferView view = buffer.CreateBufferViewBuilder()
|
|
|
|
.SetExtent(0, sizeof(s))
|
|
|
|
.GetResult();
|
|
|
|
|
|
|
|
{
|
2017-06-19 17:09:41 +00:00
|
|
|
nxt::ShaderModule module = utils::CreateShaderModule(device, nxt::ShaderStage::Compute, R"(
|
2017-04-20 18:38:20 +00:00
|
|
|
#version 450
|
|
|
|
layout(set = 0, binding = 0) buffer myBlock {
|
|
|
|
int a;
|
|
|
|
float b;
|
|
|
|
} myStorage;
|
|
|
|
void main() {
|
|
|
|
myStorage.a = (myStorage.a + 1) % 256;
|
|
|
|
myStorage.b = mod((myStorage.b + 0.02), 1.0);
|
|
|
|
})"
|
|
|
|
);
|
|
|
|
|
|
|
|
nxt::BindGroupLayout bgl = device.CreateBindGroupLayoutBuilder()
|
|
|
|
.SetBindingsType(nxt::ShaderStageBit::Compute, nxt::BindingType::StorageBuffer, 0, 1)
|
|
|
|
.GetResult();
|
|
|
|
|
|
|
|
nxt::PipelineLayout pl = device.CreatePipelineLayoutBuilder()
|
|
|
|
.SetBindGroupLayout(0, bgl)
|
|
|
|
.GetResult();
|
|
|
|
|
2017-07-14 15:32:57 +00:00
|
|
|
computePipeline = device.CreateComputePipelineBuilder()
|
2017-04-20 18:38:20 +00:00
|
|
|
.SetLayout(pl)
|
|
|
|
.SetStage(nxt::ShaderStage::Compute, module, "main")
|
|
|
|
.GetResult();
|
|
|
|
|
|
|
|
computeBindGroup = device.CreateBindGroupBuilder()
|
|
|
|
.SetLayout(bgl)
|
|
|
|
.SetUsage(nxt::BindGroupUsage::Frozen)
|
|
|
|
.SetBufferViews(0, 1, &view)
|
|
|
|
.GetResult();
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2017-06-19 17:09:41 +00:00
|
|
|
nxt::ShaderModule vsModule = utils::CreateShaderModule(device, nxt::ShaderStage::Vertex, R"(
|
2017-04-20 18:38:20 +00:00
|
|
|
#version 450
|
|
|
|
const vec2 pos[3] = vec2[3](vec2(0.0f, 0.5f), vec2(-0.5f, -0.5f), vec2(0.5f, -0.5f));
|
|
|
|
void main() {
|
|
|
|
gl_Position = vec4(pos[gl_VertexIndex], 0.5, 1.0);
|
|
|
|
})"
|
|
|
|
);
|
|
|
|
|
2017-08-29 17:37:45 +00:00
|
|
|
nxt::ShaderModule fsModule =
|
|
|
|
utils::CreateShaderModule(device, nxt::ShaderStage::Fragment, R"(
|
2017-04-20 18:38:20 +00:00
|
|
|
#version 450
|
|
|
|
layout(set = 0, binding = 0) uniform myBlock {
|
|
|
|
int a;
|
|
|
|
float b;
|
|
|
|
} myUbo;
|
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 = vec4(1.0, myUbo.a / 255.0, myUbo.b, 1.0);
|
2017-08-29 17:37:45 +00:00
|
|
|
})");
|
2017-04-20 18:38:20 +00:00
|
|
|
|
|
|
|
nxt::BindGroupLayout bgl = device.CreateBindGroupLayoutBuilder()
|
|
|
|
.SetBindingsType(nxt::ShaderStageBit::Fragment, nxt::BindingType::UniformBuffer, 0, 1)
|
|
|
|
.GetResult();
|
|
|
|
|
|
|
|
nxt::PipelineLayout pl = device.CreatePipelineLayoutBuilder()
|
|
|
|
.SetBindGroupLayout(0, bgl)
|
|
|
|
.GetResult();
|
|
|
|
|
2017-07-28 01:30:57 +00:00
|
|
|
depthStencilView = CreateDefaultDepthStencilView(device);
|
|
|
|
|
2017-07-14 15:32:57 +00:00
|
|
|
renderPipeline = device.CreateRenderPipelineBuilder()
|
2018-05-02 22:10:13 +00:00
|
|
|
.SetColorAttachmentFormat(0, GetPreferredSwapChainTextureFormat())
|
|
|
|
.SetDepthStencilAttachmentFormat(nxt::TextureFormat::D32FloatS8Uint)
|
2017-04-20 18:38:20 +00:00
|
|
|
.SetLayout(pl)
|
|
|
|
.SetStage(nxt::ShaderStage::Vertex, vsModule, "main")
|
|
|
|
.SetStage(nxt::ShaderStage::Fragment, fsModule, "main")
|
|
|
|
.GetResult();
|
|
|
|
|
|
|
|
renderBindGroup = device.CreateBindGroupBuilder()
|
|
|
|
.SetLayout(bgl)
|
|
|
|
.SetUsage(nxt::BindGroupUsage::Frozen)
|
|
|
|
.SetBufferViews(0, 1, &view)
|
|
|
|
.GetResult();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void frame() {
|
2017-07-28 01:30:57 +00:00
|
|
|
nxt::Texture backbuffer;
|
2018-05-11 17:04:44 +00:00
|
|
|
nxt::RenderPassDescriptor renderPass;
|
|
|
|
GetNextRenderPassDescriptor(device, swapchain, depthStencilView, &backbuffer, &renderPass);
|
2017-07-28 01:30:57 +00:00
|
|
|
|
2017-04-20 18:38:20 +00:00
|
|
|
nxt::CommandBuffer commands = device.CreateCommandBufferBuilder()
|
2018-04-18 19:18:31 +00:00
|
|
|
.TransitionBufferUsage(buffer, nxt::BufferUsageBit::Storage)
|
2017-07-10 21:07:24 +00:00
|
|
|
.BeginComputePass()
|
2017-07-14 15:32:57 +00:00
|
|
|
.SetComputePipeline(computePipeline)
|
2017-07-10 21:07:24 +00:00
|
|
|
.SetBindGroup(0, computeBindGroup)
|
|
|
|
.Dispatch(1, 1, 1)
|
|
|
|
.EndComputePass()
|
2017-04-20 18:38:20 +00:00
|
|
|
|
2018-04-18 19:18:31 +00:00
|
|
|
.TransitionBufferUsage(buffer, nxt::BufferUsageBit::Uniform)
|
2018-05-02 22:10:13 +00:00
|
|
|
.BeginRenderPass(renderPass)
|
2017-07-14 15:32:57 +00:00
|
|
|
.SetRenderPipeline(renderPipeline)
|
2017-05-16 21:04:22 +00:00
|
|
|
.SetBindGroup(0, renderBindGroup)
|
|
|
|
.DrawArrays(3, 1, 0, 0)
|
|
|
|
.EndRenderPass()
|
2017-04-20 18:38:20 +00:00
|
|
|
|
|
|
|
.GetResult();
|
|
|
|
|
|
|
|
queue.Submit(1, &commands);
|
2017-07-28 01:30:57 +00:00
|
|
|
backbuffer.TransitionUsage(nxt::TextureUsageBit::Present);
|
|
|
|
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
|
|
|
|
}
|