Remove samples that were only used as temporary tests

However keep the following samples:
 - CHelloTriangle that demonstrates the C API
 - CppHelloTriangle that demonstrates the C++ API
 - Animometer that matches the WebGL benchmark
 - HelloDepthStencil that does more than just a triangle, renamed
   to CubeReflection
 - ComputeBoids as a cool graphics and compute interop demo
 - glTFViewer that shows slightly more advanced usage of NXT
This commit is contained in:
Corentin Wallez 2018-07-03 14:30:52 +02:00 committed by Corentin Wallez
parent 66c06f42f4
commit 30f85cb484
9 changed files with 2 additions and 877 deletions

View File

@ -45,15 +45,9 @@ function(add_nxt_sample target sources)
endfunction()
add_nxt_sample(CHelloTriangle CHelloTriangle.cpp)
add_nxt_sample(CppHelloTriangle HelloTriangle.cpp)
add_nxt_sample(CppHelloTriangle CppHelloTriangle.cpp)
add_nxt_sample(ComputeBoids ComputeBoids.cpp)
add_nxt_sample(HelloVertices HelloVertices.cpp)
add_nxt_sample(HelloInstancing HelloInstancing.cpp)
add_nxt_sample(HelloIndices HelloIndices.cpp)
add_nxt_sample(HelloUBO HelloUBO.cpp)
add_nxt_sample(HelloCompute HelloCompute.cpp)
add_nxt_sample(RenderToTexture RenderToTexture.cpp)
add_nxt_sample(Animometer Animometer.cpp)
add_nxt_sample(CppHelloDepthStencil HelloDepthStencil.cpp)
add_nxt_sample(CubeReflection CubeReflection.cpp)
add_nxt_sample(glTFViewer glTFViewer/glTFViewer.cpp)

View File

@ -1,169 +0,0 @@
// 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.
#include "SampleUtils.h"
#include "utils/NXTHelpers.h"
#include "utils/SystemUtils.h"
#include <string.h>
nxt::Device device;
nxt::Queue queue;
nxt::SwapChain swapchain;
nxt::TextureView depthStencilView;
nxt::Buffer buffer;
nxt::RenderPipeline renderPipeline;
nxt::BindGroup renderBindGroup;
nxt::ComputePipeline computePipeline;
nxt::BindGroup computeBindGroup;
void init() {
device = CreateCppNXTDevice();
queue = device.CreateQueue();
swapchain = GetSwapChain(device);
swapchain.Configure(GetPreferredSwapChainTextureFormat(),
nxt::TextureUsageBit::OutputAttachment, 640, 480);
struct {uint32_t a; float b;} s;
memset(&s, 0, sizeof(s));
buffer = device.CreateBufferBuilder()
.SetAllowedUsage(nxt::BufferUsageBit::TransferDst | nxt::BufferUsageBit::Uniform | nxt::BufferUsageBit::Storage)
.SetInitialUsage(nxt::BufferUsageBit::TransferDst)
.SetSize(sizeof(s))
.GetResult();
buffer.SetSubData(0, sizeof(s), reinterpret_cast<uint8_t*>(&s));
nxt::BufferView view = buffer.CreateBufferViewBuilder()
.SetExtent(0, sizeof(s))
.GetResult();
{
nxt::ShaderModule module = utils::CreateShaderModule(device, nxt::ShaderStage::Compute, R"(
#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 = utils::MakeBasicPipelineLayout(device, &bgl);
computePipeline = device.CreateComputePipelineBuilder()
.SetLayout(pl)
.SetStage(nxt::ShaderStage::Compute, module, "main")
.GetResult();
computeBindGroup = device.CreateBindGroupBuilder()
.SetLayout(bgl)
.SetUsage(nxt::BindGroupUsage::Frozen)
.SetBufferViews(0, 1, &view)
.GetResult();
}
{
nxt::ShaderModule vsModule = utils::CreateShaderModule(device, nxt::ShaderStage::Vertex, R"(
#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);
})"
);
nxt::ShaderModule fsModule =
utils::CreateShaderModule(device, nxt::ShaderStage::Fragment, R"(
#version 450
layout(set = 0, binding = 0) uniform myBlock {
int a;
float b;
} myUbo;
layout(location = 0) out vec4 fragColor;
void main() {
fragColor = vec4(1.0, myUbo.a / 255.0, myUbo.b, 1.0);
})");
nxt::BindGroupLayout bgl = device.CreateBindGroupLayoutBuilder()
.SetBindingsType(nxt::ShaderStageBit::Fragment, nxt::BindingType::UniformBuffer, 0, 1)
.GetResult();
nxt::PipelineLayout pl = utils::MakeBasicPipelineLayout(device, &bgl);
depthStencilView = CreateDefaultDepthStencilView(device);
renderPipeline = device.CreateRenderPipelineBuilder()
.SetColorAttachmentFormat(0, GetPreferredSwapChainTextureFormat())
.SetDepthStencilAttachmentFormat(nxt::TextureFormat::D32FloatS8Uint)
.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() {
nxt::Texture backbuffer;
nxt::RenderPassDescriptor renderPass;
GetNextRenderPassDescriptor(device, swapchain, depthStencilView, &backbuffer, &renderPass);
nxt::CommandBuffer commands = device.CreateCommandBufferBuilder()
.TransitionBufferUsage(buffer, nxt::BufferUsageBit::Storage)
.BeginComputePass()
.SetComputePipeline(computePipeline)
.SetBindGroup(0, computeBindGroup)
.Dispatch(1, 1, 1)
.EndComputePass()
.TransitionBufferUsage(buffer, nxt::BufferUsageBit::Uniform)
.BeginRenderPass(renderPass)
.SetRenderPipeline(renderPipeline)
.SetBindGroup(0, renderBindGroup)
.DrawArrays(3, 1, 0, 0)
.EndRenderPass()
.GetResult();
queue.Submit(1, &commands);
backbuffer.TransitionUsage(nxt::TextureUsageBit::Present);
swapchain.Present(backbuffer);
DoFlush();
}
int main(int argc, const char* argv[]) {
if (!InitSample(argc, argv)) {
return 1;
}
init();
while (!ShouldQuit()) {
frame();
utils::USleep(16000);
}
// TODO release stuff
}

View File

@ -1,121 +0,0 @@
// 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.
#include "SampleUtils.h"
#include "utils/NXTHelpers.h"
#include "utils/SystemUtils.h"
#include <vector>
nxt::Device device;
nxt::Buffer indexBuffer;
nxt::Buffer vertexBuffer;
nxt::Queue queue;
nxt::SwapChain swapchain;
nxt::TextureView depthStencilView;
nxt::RenderPipeline pipeline;
void initBuffers() {
static const uint32_t indexData[3] = {
0, 1, 2,
};
indexBuffer = utils::CreateFrozenBufferFromData(device, indexData, sizeof(indexData), nxt::BufferUsageBit::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::CreateFrozenBufferFromData(device, vertexData, sizeof(vertexData), nxt::BufferUsageBit::Vertex);
}
void init() {
device = CreateCppNXTDevice();
queue = device.CreateQueue();
swapchain = GetSwapChain(device);
swapchain.Configure(GetPreferredSwapChainTextureFormat(),
nxt::TextureUsageBit::OutputAttachment, 640, 480);
initBuffers();
nxt::ShaderModule vsModule = utils::CreateShaderModule(device, nxt::ShaderStage::Vertex, R"(
#version 450
layout(location = 0) in vec4 pos;
void main() {
gl_Position = pos;
})"
);
nxt::ShaderModule fsModule = utils::CreateShaderModule(device, nxt::ShaderStage::Fragment, R"(
#version 450
layout(location = 0) out vec4 fragColor;
void main() {
fragColor = vec4(1.0, 0.0, 0.0, 1.0);
})");
auto inputState = device.CreateInputStateBuilder()
.SetAttribute(0, 0, nxt::VertexFormat::FloatR32G32B32A32, 0)
.SetInput(0, 4 * sizeof(float), nxt::InputStepMode::Vertex)
.GetResult();
depthStencilView = CreateDefaultDepthStencilView(device);
pipeline = device.CreateRenderPipelineBuilder()
.SetColorAttachmentFormat(0, GetPreferredSwapChainTextureFormat())
.SetDepthStencilAttachmentFormat(nxt::TextureFormat::D32FloatS8Uint)
.SetStage(nxt::ShaderStage::Vertex, vsModule, "main")
.SetStage(nxt::ShaderStage::Fragment, fsModule, "main")
.SetIndexFormat(nxt::IndexFormat::Uint32)
.SetInputState(inputState)
.GetResult();
}
void frame() {
nxt::Texture backbuffer;
nxt::RenderPassDescriptor renderPass;
GetNextRenderPassDescriptor(device, swapchain, depthStencilView, &backbuffer, &renderPass);
static const uint32_t vertexBufferOffsets[1] = {0};
nxt::CommandBuffer commands = device.CreateCommandBufferBuilder()
.BeginRenderPass(renderPass)
.SetRenderPipeline(pipeline)
.SetVertexBuffers(0, 1, &vertexBuffer, vertexBufferOffsets)
.SetIndexBuffer(indexBuffer, 0)
.DrawElements(3, 1, 0, 0)
.EndRenderPass()
.GetResult();
queue.Submit(1, &commands);
backbuffer.TransitionUsage(nxt::TextureUsageBit::Present);
swapchain.Present(backbuffer);
DoFlush();
}
int main(int argc, const char* argv[]) {
if (!InitSample(argc, argv)) {
return 1;
}
init();
while (!ShouldQuit()) {
frame();
utils::USleep(16000);
}
// TODO release stuff
}

View File

@ -1,126 +0,0 @@
// 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.
#include "SampleUtils.h"
#include "utils/NXTHelpers.h"
#include "utils/SystemUtils.h"
#include <vector>
nxt::Device device;
nxt::Buffer vertexBuffer;
nxt::Buffer instanceBuffer;
nxt::Queue queue;
nxt::SwapChain swapchain;
nxt::TextureView depthStencilView;
nxt::RenderPipeline pipeline;
void initBuffers() {
static const float vertexData[12] = {
0.0f, 0.1f, 0.0f, 1.0f,
-0.1f, -0.1f, 0.0f, 1.0f,
0.1f, -0.1f, 0.0f, 1.0f,
};
vertexBuffer = utils::CreateFrozenBufferFromData(device, vertexData, sizeof(vertexData), nxt::BufferUsageBit::Vertex);
static const float instanceData[8] = {
-0.5f, -0.5f,
-0.5f, 0.5f,
0.5f, -0.5f,
0.5f, 0.5f,
};
instanceBuffer = utils::CreateFrozenBufferFromData(device, instanceData, sizeof(instanceData), nxt::BufferUsageBit::Vertex);
}
void init() {
device = CreateCppNXTDevice();
queue = device.CreateQueue();
swapchain = GetSwapChain(device);
swapchain.Configure(GetPreferredSwapChainTextureFormat(),
nxt::TextureUsageBit::OutputAttachment, 640, 480);
initBuffers();
nxt::ShaderModule vsModule = utils::CreateShaderModule(device, nxt::ShaderStage::Vertex, R"(
#version 450
layout(location = 0) in vec4 pos;
layout(location = 1) in vec2 instance;
void main() {
gl_Position = vec4(pos.xy + instance, pos.zw);
})"
);
nxt::ShaderModule fsModule = utils::CreateShaderModule(device, nxt::ShaderStage::Fragment, R"(
#version 450
layout(location = 0) out vec4 fragColor;
void main() {
fragColor = vec4(1.0, 0.0, 0.0, 1.0);
})");
auto inputState = device.CreateInputStateBuilder()
.SetAttribute(0, 0, nxt::VertexFormat::FloatR32G32B32A32, 0)
.SetInput(0, 4 * sizeof(float), nxt::InputStepMode::Vertex)
.SetAttribute(1, 1, nxt::VertexFormat::FloatR32G32, 0)
.SetInput(1, 2 * sizeof(float), nxt::InputStepMode::Instance)
.GetResult();
depthStencilView = CreateDefaultDepthStencilView(device);
pipeline = device.CreateRenderPipelineBuilder()
.SetColorAttachmentFormat(0, GetPreferredSwapChainTextureFormat())
.SetDepthStencilAttachmentFormat(nxt::TextureFormat::D32FloatS8Uint)
.SetStage(nxt::ShaderStage::Vertex, vsModule, "main")
.SetStage(nxt::ShaderStage::Fragment, fsModule, "main")
.SetInputState(inputState)
.GetResult();
}
void frame() {
nxt::Texture backbuffer;
nxt::RenderPassDescriptor renderPass;
GetNextRenderPassDescriptor(device, swapchain, depthStencilView, &backbuffer, &renderPass);
static const uint32_t vertexBufferOffsets[1] = {0};
nxt::CommandBuffer commands = device.CreateCommandBufferBuilder()
.BeginRenderPass(renderPass)
.SetRenderPipeline(pipeline)
.SetVertexBuffers(0, 1, &vertexBuffer, vertexBufferOffsets)
.SetVertexBuffers(1, 1, &instanceBuffer, vertexBufferOffsets)
.DrawArrays(3, 4, 0, 0)
.EndRenderPass()
.GetResult();
queue.Submit(1, &commands);
backbuffer.TransitionUsage(nxt::TextureUsageBit::Present);
swapchain.Present(backbuffer);
DoFlush();
}
int main(int argc, const char* argv[]) {
if (!InitSample(argc, argv)) {
return 1;
}
init();
while (!ShouldQuit()) {
frame();
utils::USleep(16000);
}
// TODO release stuff
}

View File

@ -1,129 +0,0 @@
// 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.
#include "SampleUtils.h"
#include "utils/NXTHelpers.h"
#include "utils/SystemUtils.h"
nxt::Device device;
nxt::Queue queue;
nxt::SwapChain swapchain;
nxt::TextureView depthStencilView;
nxt::RenderPipeline pipeline;
nxt::Buffer buffer;
nxt::BindGroup bindGroup;
struct {uint32_t a; float b;} s;
void init() {
device = CreateCppNXTDevice();
queue = device.CreateQueue();
swapchain = GetSwapChain(device);
swapchain.Configure(GetPreferredSwapChainTextureFormat(),
nxt::TextureUsageBit::OutputAttachment, 640, 480);
nxt::ShaderModule vsModule = utils::CreateShaderModule(device, nxt::ShaderStage::Vertex, R"(
#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);
})"
);
nxt::ShaderModule fsModule = utils::CreateShaderModule(device, nxt::ShaderStage::Fragment, R"(
#version 450
layout(set = 0, binding = 0) uniform myBlock {
int a;
float b;
} myUbo;
layout(location = 0) out vec4 fragColor;
void main() {
fragColor = vec4(1.0, myUbo.a / 255.0, myUbo.b, 1.0);
})");
nxt::BindGroupLayout bgl = device.CreateBindGroupLayoutBuilder()
.SetBindingsType(nxt::ShaderStageBit::Fragment, nxt::BindingType::UniformBuffer, 0, 1)
.GetResult();
nxt::PipelineLayout pl = utils::MakeBasicPipelineLayout(device, &bgl);
depthStencilView = CreateDefaultDepthStencilView(device);
pipeline = device.CreateRenderPipelineBuilder()
.SetColorAttachmentFormat(0, GetPreferredSwapChainTextureFormat())
.SetDepthStencilAttachmentFormat(nxt::TextureFormat::D32FloatS8Uint)
.SetLayout(pl)
.SetStage(nxt::ShaderStage::Vertex, vsModule, "main")
.SetStage(nxt::ShaderStage::Fragment, fsModule, "main")
.GetResult();
buffer = device.CreateBufferBuilder()
.SetAllowedUsage(nxt::BufferUsageBit::TransferDst | nxt::BufferUsageBit::Uniform)
.SetInitialUsage(nxt::BufferUsageBit::TransferDst)
.SetSize(sizeof(s))
.GetResult();
nxt::BufferView view = buffer.CreateBufferViewBuilder()
.SetExtent(0, sizeof(s))
.GetResult();
bindGroup = device.CreateBindGroupBuilder()
.SetLayout(bgl)
.SetUsage(nxt::BindGroupUsage::Frozen)
.SetBufferViews(0, 1, &view)
.GetResult();
}
void frame() {
s.a = (s.a + 1) % 256;
s.b += 0.02f;
if (s.b >= 1.0f) {s.b = 0.0f;}
buffer.TransitionUsage(nxt::BufferUsageBit::TransferDst);
buffer.SetSubData(0, sizeof(s), reinterpret_cast<uint8_t*>(&s));
nxt::Texture backbuffer;
nxt::RenderPassDescriptor renderPass;
GetNextRenderPassDescriptor(device, swapchain, depthStencilView, &backbuffer, &renderPass);
nxt::CommandBuffer commands = device.CreateCommandBufferBuilder()
.BeginRenderPass(renderPass)
.SetRenderPipeline(pipeline)
.TransitionBufferUsage(buffer, nxt::BufferUsageBit::Uniform)
.SetBindGroup(0, bindGroup)
.DrawArrays(3, 1, 0, 0)
.EndRenderPass()
.GetResult();
queue.Submit(1, &commands);
backbuffer.TransitionUsage(nxt::TextureUsageBit::Present);
swapchain.Present(backbuffer);
DoFlush();
}
int main(int argc, const char* argv[]) {
if (!InitSample(argc, argv)) {
return 1;
}
init();
while (!ShouldQuit()) {
frame();
utils::USleep(16000);
}
// TODO release stuff
}

View File

@ -1,114 +0,0 @@
// 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.
#include "SampleUtils.h"
#include "utils/NXTHelpers.h"
#include "utils/SystemUtils.h"
#include <vector>
nxt::Device device;
nxt::Buffer vertexBuffer;
nxt::Queue queue;
nxt::SwapChain swapchain;
nxt::TextureView depthStencilView;
nxt::RenderPipeline pipeline;
void initBuffers() {
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::CreateFrozenBufferFromData(device, vertexData, sizeof(vertexData), nxt::BufferUsageBit::Vertex);
}
void init() {
device = CreateCppNXTDevice();
queue = device.CreateQueue();
swapchain = GetSwapChain(device);
swapchain.Configure(GetPreferredSwapChainTextureFormat(),
nxt::TextureUsageBit::OutputAttachment, 640, 480);
initBuffers();
nxt::ShaderModule vsModule = utils::CreateShaderModule(device, nxt::ShaderStage::Vertex, R"(
#version 450
layout(location = 0) in vec4 pos;
void main() {
gl_Position = pos;
})"
);
nxt::ShaderModule fsModule = utils::CreateShaderModule(device, nxt::ShaderStage::Fragment, R"(
#version 450
layout(location = 0) out vec4 fragColor;
void main() {
fragColor = vec4(1.0, 0.0, 0.0, 1.0);
})"
);
auto inputState = device.CreateInputStateBuilder()
.SetAttribute(0, 0, nxt::VertexFormat::FloatR32G32B32A32, 0)
.SetInput(0, 4 * sizeof(float), nxt::InputStepMode::Vertex)
.GetResult();
depthStencilView = CreateDefaultDepthStencilView(device);
pipeline = device.CreateRenderPipelineBuilder()
.SetColorAttachmentFormat(0, GetPreferredSwapChainTextureFormat())
.SetDepthStencilAttachmentFormat(nxt::TextureFormat::D32FloatS8Uint)
.SetStage(nxt::ShaderStage::Vertex, vsModule, "main")
.SetStage(nxt::ShaderStage::Fragment, fsModule, "main")
.SetInputState(inputState)
.GetResult();
}
void frame() {
nxt::Texture backbuffer;
nxt::RenderPassDescriptor renderPass;
GetNextRenderPassDescriptor(device, swapchain, depthStencilView, &backbuffer, &renderPass);
static const uint32_t vertexBufferOffsets[1] = {0};
nxt::CommandBuffer commands = device.CreateCommandBufferBuilder()
.BeginRenderPass(renderPass)
.SetRenderPipeline(pipeline)
.SetVertexBuffers(0, 1, &vertexBuffer, vertexBufferOffsets)
.DrawArrays(3, 1, 0, 0)
.EndRenderPass()
.GetResult();
queue.Submit(1, &commands);
backbuffer.TransitionUsage(nxt::TextureUsageBit::Present);
swapchain.Present(backbuffer);
DoFlush();
}
int main(int argc, const char* argv[]) {
if (!InitSample(argc, argv)) {
return 1;
}
init();
while (!ShouldQuit()) {
frame();
utils::USleep(16000);
}
// TODO release stuff
}

View File

@ -1,210 +0,0 @@
// 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.
#include "SampleUtils.h"
#include "utils/NXTHelpers.h"
#include "utils/SystemUtils.h"
#include <vector>
nxt::Device device;
nxt::Buffer vertexBuffer;
nxt::Buffer vertexBufferQuad;
nxt::Texture renderTarget;
nxt::TextureView renderTargetView;
nxt::Sampler samplerPost;
nxt::Queue queue;
nxt::SwapChain swapchain;
nxt::RenderPipeline pipeline;
nxt::RenderPipeline pipelinePost;
nxt::BindGroup bindGroup;
void initBuffers() {
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::CreateFrozenBufferFromData(device, vertexData, sizeof(vertexData), nxt::BufferUsageBit::Vertex);
static const float vertexDataQuad[24] = {
-1.0f, -1.0f, 0.0f, 1.0f,
-1.0f, 1.0f, 0.0f, 1.0f,
1.0f, -1.0f, 0.0f, 1.0f,
-1.0f, 1.0f, 0.0f, 1.0f,
1.0f, -1.0f, 0.0f, 1.0f,
1.0f, 1.0f, 0.0f, 1.0f,
};
vertexBufferQuad = utils::CreateFrozenBufferFromData(device, vertexDataQuad, sizeof(vertexDataQuad), nxt::BufferUsageBit::Vertex);
}
void initTextures() {
renderTarget = device.CreateTextureBuilder()
.SetDimension(nxt::TextureDimension::e2D)
.SetExtent(640, 480, 1)
.SetFormat(nxt::TextureFormat::R8G8B8A8Unorm)
.SetMipLevels(1)
.SetAllowedUsage(nxt::TextureUsageBit::OutputAttachment | nxt::TextureUsageBit::Sampled)
.SetInitialUsage(nxt::TextureUsageBit::OutputAttachment)
.GetResult();
renderTargetView = renderTarget.CreateTextureViewBuilder().GetResult();
nxt::SamplerDescriptor samplerDesc = utils::GetDefaultSamplerDescriptor();
samplerPost = device.CreateSampler(&samplerDesc);
}
void initPipeline() {
nxt::ShaderModule vsModule = utils::CreateShaderModule(device, nxt::ShaderStage::Vertex, R"(
#version 450
layout(location = 0) in vec4 pos;
void main() {
gl_Position = pos;
})"
);
nxt::ShaderModule fsModule = utils::CreateShaderModule(device, nxt::ShaderStage::Fragment, R"(
#version 450
layout(location = 0) out vec4 fragColor;
void main() {
fragColor = vec4(1.0, 0.0, 0.0, 1.0);
})"
);
auto inputState = device.CreateInputStateBuilder()
.SetAttribute(0, 0, nxt::VertexFormat::FloatR32G32B32A32, 0)
.SetInput(0, 4 * sizeof(float), nxt::InputStepMode::Vertex)
.GetResult();
pipeline = device.CreateRenderPipelineBuilder()
.SetColorAttachmentFormat(0, nxt::TextureFormat::R8G8B8A8Unorm)
.SetStage(nxt::ShaderStage::Vertex, vsModule, "main")
.SetStage(nxt::ShaderStage::Fragment, fsModule, "main")
.SetInputState(inputState)
.GetResult();
}
void initPipelinePost() {
nxt::ShaderModule vsModule = utils::CreateShaderModule(device, nxt::ShaderStage::Vertex, R"(
#version 450
layout(location = 0) in vec4 pos;
void main() {
gl_Position = pos;
})"
);
nxt::ShaderModule fsModule = utils::CreateShaderModule(device, nxt::ShaderStage::Fragment, R"(
#version 450
layout(set = 0, binding = 0) uniform sampler samp;
layout(set = 0, binding = 1) uniform texture2D tex;
layout(location = 0) out vec4 fragColor;
void main() {
fragColor = texture(sampler2D(tex, samp), gl_FragCoord.xy / vec2(640.0, 480.0)) + vec4(0.0, 0.0, 0.5, 0.0);
})"
);
auto inputState = device.CreateInputStateBuilder()
.SetAttribute(0, 0, nxt::VertexFormat::FloatR32G32B32A32, 0)
.SetInput(0, 4 * sizeof(float), nxt::InputStepMode::Vertex)
.GetResult();
nxt::BindGroupLayout bgl = device.CreateBindGroupLayoutBuilder()
.SetBindingsType(nxt::ShaderStageBit::Fragment, nxt::BindingType::Sampler, 0, 1)
.SetBindingsType(nxt::ShaderStageBit::Fragment, nxt::BindingType::SampledTexture, 1, 1)
.GetResult();
nxt::PipelineLayout pl = utils::MakeBasicPipelineLayout(device, &bgl);
pipelinePost = device.CreateRenderPipelineBuilder()
.SetColorAttachmentFormat(0, nxt::TextureFormat::R8G8B8A8Unorm)
.SetLayout(pl)
.SetStage(nxt::ShaderStage::Vertex, vsModule, "main")
.SetStage(nxt::ShaderStage::Fragment, fsModule, "main")
.SetInputState(inputState)
.GetResult();
bindGroup = device.CreateBindGroupBuilder()
.SetLayout(bgl)
.SetUsage(nxt::BindGroupUsage::Frozen)
.SetSamplers(0, 1, &samplerPost)
.SetTextureViews(1, 1, &renderTargetView)
.GetResult();
}
void init() {
device = CreateCppNXTDevice();
queue = device.CreateQueue();
swapchain = GetSwapChain(device);
swapchain.Configure(GetPreferredSwapChainTextureFormat(),
nxt::TextureUsageBit::OutputAttachment, 640, 480);
initBuffers();
initTextures();
initPipeline();
initPipelinePost();
}
void frame() {
nxt::Texture backbuffer = swapchain.GetNextTexture();
auto backbufferView = backbuffer.CreateTextureViewBuilder().GetResult();
nxt::RenderPassDescriptor renderPass1 = device.CreateRenderPassDescriptorBuilder()
.SetColorAttachment(0, renderTargetView, nxt::LoadOp::Clear)
.GetResult();
nxt::RenderPassDescriptor renderPass2 = device.CreateRenderPassDescriptorBuilder()
.SetColorAttachment(0, backbufferView, nxt::LoadOp::Clear)
.GetResult();
static const uint32_t vertexBufferOffsets[1] = {0};
nxt::CommandBuffer commands = device.CreateCommandBufferBuilder()
.BeginRenderPass(renderPass1)
// renderTarget implicitly locked to to Attachment usage (if not already frozen)
.SetRenderPipeline(pipeline)
.SetVertexBuffers(0, 1, &vertexBuffer, vertexBufferOffsets)
.DrawArrays(3, 1, 0, 0)
.EndRenderPass()
// renderTarget usage unlocked, but left in Attachment usage
.TransitionTextureUsage(renderTarget, nxt::TextureUsageBit::Sampled)
.BeginRenderPass(renderPass2)
.SetRenderPipeline(pipelinePost)
.SetVertexBuffers(0, 1, &vertexBufferQuad, vertexBufferOffsets)
.SetBindGroup(0, bindGroup)
.DrawArrays(6, 1, 0, 0)
.EndRenderPass()
.GetResult();
queue.Submit(1, &commands);
backbuffer.TransitionUsage(nxt::TextureUsageBit::Present);
swapchain.Present(backbuffer);
DoFlush();
}
int main(int argc, const char* argv[]) {
if (!InitSample(argc, argv)) {
return 1;
}
init();
while (!ShouldQuit()) {
frame();
utils::USleep(16000);
}
// TODO release stuff
}