2017-05-31 00:03:44 +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-05-31 00:03:44 +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-05-31 00:03:44 +00:00
|
|
|
#include <vector>
|
|
|
|
#include <glm/glm/glm.hpp>
|
|
|
|
#include <glm/glm/gtc/matrix_transform.hpp>
|
|
|
|
#include <glm/glm/gtc/type_ptr.hpp>
|
|
|
|
|
|
|
|
nxt::Device device;
|
|
|
|
|
|
|
|
nxt::Buffer indexBuffer;
|
|
|
|
nxt::Buffer vertexBuffer;
|
|
|
|
nxt::Buffer planeBuffer;
|
|
|
|
nxt::Buffer cameraBuffer;
|
|
|
|
nxt::Buffer transformBuffer[2];
|
|
|
|
|
|
|
|
nxt::BindGroup cameraBindGroup;
|
|
|
|
nxt::BindGroup bindGroup[2];
|
|
|
|
nxt::BindGroup cubeTransformBindGroup[2];
|
|
|
|
|
|
|
|
nxt::Queue queue;
|
2017-07-28 01:30:57 +00:00
|
|
|
nxt::SwapChain swapchain;
|
|
|
|
nxt::TextureView depthStencilView;
|
2017-07-14 15:32:57 +00:00
|
|
|
nxt::RenderPipeline pipeline;
|
|
|
|
nxt::RenderPipeline planePipeline;
|
|
|
|
nxt::RenderPipeline reflectionPipeline;
|
2017-05-31 00:03:44 +00:00
|
|
|
nxt::RenderPass renderpass;
|
|
|
|
|
|
|
|
void initBuffers() {
|
|
|
|
static const uint32_t indexData[6*6] = {
|
|
|
|
0, 1, 2,
|
|
|
|
0, 2, 3,
|
|
|
|
|
|
|
|
4, 5, 6,
|
|
|
|
4, 6, 7,
|
|
|
|
|
|
|
|
8, 9, 10,
|
|
|
|
8, 10, 11,
|
|
|
|
|
|
|
|
12, 13, 14,
|
|
|
|
12, 14, 15,
|
|
|
|
|
|
|
|
16, 17, 18,
|
|
|
|
16, 18, 19,
|
|
|
|
|
|
|
|
20, 21, 22,
|
|
|
|
20, 22, 23
|
|
|
|
};
|
2017-06-19 17:09:41 +00:00
|
|
|
indexBuffer = utils::CreateFrozenBufferFromData(device, indexData, sizeof(indexData), nxt::BufferUsageBit::Index);
|
2017-05-31 00:03:44 +00:00
|
|
|
|
|
|
|
static const float vertexData[6 * 4 * 6] = {
|
|
|
|
-1.0, -1.0, 1.0, 1.0, 0.0, 0.0,
|
|
|
|
1.0, -1.0, 1.0, 1.0, 0.0, 0.0,
|
|
|
|
1.0, 1.0, 1.0, 1.0, 0.0, 0.0,
|
|
|
|
-1.0, 1.0, 1.0, 1.0, 0.0, 0.0,
|
|
|
|
|
|
|
|
-1.0, -1.0, -1.0, 1.0, 1.0, 0.0,
|
|
|
|
-1.0, 1.0, -1.0, 1.0, 1.0, 0.0,
|
|
|
|
1.0, 1.0, -1.0, 1.0, 1.0, 0.0,
|
|
|
|
1.0, -1.0, -1.0, 1.0, 1.0, 0.0,
|
|
|
|
|
|
|
|
-1.0, 1.0, -1.0, 1.0, 0.0, 1.0,
|
|
|
|
-1.0, 1.0, 1.0, 1.0, 0.0, 1.0,
|
|
|
|
1.0, 1.0, 1.0, 1.0, 0.0, 1.0,
|
|
|
|
1.0, 1.0, -1.0, 1.0, 0.0, 1.0,
|
|
|
|
|
|
|
|
-1.0, -1.0, -1.0, 0.0, 1.0, 0.0,
|
|
|
|
1.0, -1.0, -1.0, 0.0, 1.0, 0.0,
|
|
|
|
1.0, -1.0, 1.0, 0.0, 1.0, 0.0,
|
|
|
|
-1.0, -1.0, 1.0, 0.0, 1.0, 0.0,
|
|
|
|
|
|
|
|
1.0, -1.0, -1.0, 0.0, 1.0, 1.0,
|
|
|
|
1.0, 1.0, -1.0, 0.0, 1.0, 1.0,
|
|
|
|
1.0, 1.0, 1.0, 0.0, 1.0, 1.0,
|
|
|
|
1.0, -1.0, 1.0, 0.0, 1.0, 1.0,
|
|
|
|
|
|
|
|
-1.0, -1.0, -1.0, 1.0, 1.0, 1.0,
|
|
|
|
-1.0, -1.0, 1.0, 1.0, 1.0, 1.0,
|
|
|
|
-1.0, 1.0, 1.0, 1.0, 1.0, 1.0,
|
|
|
|
-1.0, 1.0, -1.0, 1.0, 1.0, 1.0
|
|
|
|
};
|
2017-06-19 17:09:41 +00:00
|
|
|
vertexBuffer = utils::CreateFrozenBufferFromData(device, vertexData, sizeof(vertexData), nxt::BufferUsageBit::Vertex);
|
2017-05-31 00:03:44 +00:00
|
|
|
|
|
|
|
static const float planeData[6 * 4] = {
|
|
|
|
-2.0, -1.0, -2.0, 0.5, 0.5, 0.5,
|
|
|
|
2.0, -1.0, -2.0, 0.5, 0.5, 0.5,
|
|
|
|
2.0, -1.0, 2.0, 0.5, 0.5, 0.5,
|
|
|
|
-2.0, -1.0, 2.0, 0.5, 0.5, 0.5,
|
|
|
|
};
|
2017-06-19 17:09:41 +00:00
|
|
|
planeBuffer = utils::CreateFrozenBufferFromData(device, planeData, sizeof(planeData), nxt::BufferUsageBit::Vertex);
|
2017-05-31 00:03:44 +00:00
|
|
|
}
|
|
|
|
|
2017-06-02 18:31:53 +00:00
|
|
|
struct CameraData {
|
2017-05-31 00:03:44 +00:00
|
|
|
glm::mat4 view;
|
|
|
|
glm::mat4 proj;
|
|
|
|
} cameraData;
|
|
|
|
|
|
|
|
void init() {
|
|
|
|
device = CreateCppNXTDevice();
|
|
|
|
|
|
|
|
queue = device.CreateQueueBuilder().GetResult();
|
2017-07-28 01:30:57 +00:00
|
|
|
swapchain = GetSwapChain(device);
|
2017-08-30 23:53:27 +00:00
|
|
|
swapchain.Configure(nxt::TextureFormat::R8G8B8A8Unorm, nxt::TextureUsageBit::OutputAttachment, 640, 480);
|
2017-05-31 00:03:44 +00:00
|
|
|
|
|
|
|
initBuffers();
|
|
|
|
|
2017-06-19 17:09:41 +00:00
|
|
|
nxt::ShaderModule vsModule = utils::CreateShaderModule(device, nxt::ShaderStage::Vertex, R"(
|
2017-05-31 00:03:44 +00:00
|
|
|
#version 450
|
|
|
|
layout(set = 0, binding = 0) uniform cameraData {
|
|
|
|
mat4 view;
|
|
|
|
mat4 proj;
|
|
|
|
} camera;
|
|
|
|
layout(set = 0, binding = 1) uniform modelData {
|
|
|
|
mat4 modelMatrix;
|
|
|
|
};
|
|
|
|
layout(location = 0) in vec3 pos;
|
|
|
|
layout(location = 1) in vec3 col;
|
|
|
|
layout(location = 2) out vec3 f_col;
|
|
|
|
void main() {
|
|
|
|
f_col = col;
|
|
|
|
gl_Position = camera.proj * camera.view * modelMatrix * vec4(pos, 1.0);
|
|
|
|
})"
|
|
|
|
);
|
|
|
|
|
2017-06-19 17:09:41 +00:00
|
|
|
nxt::ShaderModule fsModule = utils::CreateShaderModule(device, nxt::ShaderStage::Fragment, R"(
|
2017-05-31 00:03:44 +00:00
|
|
|
#version 450
|
|
|
|
layout(location = 2) in vec3 f_col;
|
|
|
|
out vec4 fragColor;
|
|
|
|
void main() {
|
|
|
|
fragColor = vec4(f_col, 1.0);
|
|
|
|
})"
|
|
|
|
);
|
|
|
|
|
2017-06-19 17:09:41 +00:00
|
|
|
nxt::ShaderModule fsReflectionModule = utils::CreateShaderModule(device, nxt::ShaderStage::Fragment, R"(
|
2017-05-31 00:03:44 +00:00
|
|
|
#version 450
|
|
|
|
layout(location = 2) in vec3 f_col;
|
|
|
|
out vec4 fragColor;
|
|
|
|
void main() {
|
|
|
|
fragColor = vec4(mix(f_col, vec3(0.5, 0.5, 0.5), 0.5), 1.0);
|
|
|
|
})"
|
|
|
|
);
|
|
|
|
|
|
|
|
auto inputState = device.CreateInputStateBuilder()
|
|
|
|
.SetAttribute(0, 0, nxt::VertexFormat::FloatR32G32B32, 0)
|
|
|
|
.SetAttribute(1, 0, nxt::VertexFormat::FloatR32G32B32, 3 * sizeof(float))
|
|
|
|
.SetInput(0, 6 * sizeof(float), nxt::InputStepMode::Vertex)
|
|
|
|
.GetResult();
|
|
|
|
|
|
|
|
nxt::BindGroupLayout bgl = device.CreateBindGroupLayoutBuilder()
|
|
|
|
.SetBindingsType(nxt::ShaderStageBit::Vertex, nxt::BindingType::UniformBuffer, 0, 2)
|
|
|
|
.GetResult();
|
|
|
|
|
|
|
|
nxt::PipelineLayout pl = device.CreatePipelineLayoutBuilder()
|
|
|
|
.SetBindGroupLayout(0, bgl)
|
|
|
|
.GetResult();
|
|
|
|
|
|
|
|
cameraBuffer = device.CreateBufferBuilder()
|
2017-06-12 21:33:44 +00:00
|
|
|
.SetAllowedUsage(nxt::BufferUsageBit::TransferDst | nxt::BufferUsageBit::Uniform)
|
|
|
|
.SetInitialUsage(nxt::BufferUsageBit::TransferDst)
|
2017-06-02 18:31:53 +00:00
|
|
|
.SetSize(sizeof(CameraData))
|
2017-05-31 00:03:44 +00:00
|
|
|
.GetResult();
|
|
|
|
|
|
|
|
glm::mat4 transform(1.0);
|
2017-06-19 17:09:41 +00:00
|
|
|
transformBuffer[0] = utils::CreateFrozenBufferFromData(device, &transform, sizeof(glm::mat4), nxt::BufferUsageBit::Uniform);
|
2017-05-31 00:03:44 +00:00
|
|
|
|
|
|
|
transform = glm::translate(transform, glm::vec3(0.f, -2.f, 0.f));
|
2017-06-19 17:09:41 +00:00
|
|
|
transformBuffer[1] = utils::CreateFrozenBufferFromData(device, &transform, sizeof(glm::mat4), nxt::BufferUsageBit::Uniform);
|
2017-05-31 00:03:44 +00:00
|
|
|
|
|
|
|
nxt::BufferView cameraBufferView = cameraBuffer.CreateBufferViewBuilder()
|
2017-06-02 18:31:53 +00:00
|
|
|
.SetExtent(0, sizeof(CameraData))
|
2017-05-31 00:03:44 +00:00
|
|
|
.GetResult();
|
|
|
|
|
|
|
|
nxt::BufferView transformBufferView[2] = {
|
|
|
|
transformBuffer[0].CreateBufferViewBuilder()
|
|
|
|
.SetExtent(0, sizeof(glm::mat4))
|
|
|
|
.GetResult(),
|
|
|
|
transformBuffer[1].CreateBufferViewBuilder()
|
|
|
|
.SetExtent(0, sizeof(glm::mat4))
|
|
|
|
.GetResult(),
|
|
|
|
};
|
|
|
|
|
|
|
|
bindGroup[0] = device.CreateBindGroupBuilder()
|
|
|
|
.SetLayout(bgl)
|
|
|
|
.SetUsage(nxt::BindGroupUsage::Frozen)
|
|
|
|
.SetBufferViews(0, 1, &cameraBufferView)
|
|
|
|
.SetBufferViews(1, 1, &transformBufferView[0])
|
|
|
|
.GetResult();
|
|
|
|
|
|
|
|
bindGroup[1] = device.CreateBindGroupBuilder()
|
|
|
|
.SetLayout(bgl)
|
|
|
|
.SetUsage(nxt::BindGroupUsage::Frozen)
|
|
|
|
.SetBufferViews(0, 1, &cameraBufferView)
|
|
|
|
.SetBufferViews(1, 1, &transformBufferView[1])
|
|
|
|
.GetResult();
|
|
|
|
|
2017-07-28 01:30:57 +00:00
|
|
|
renderpass = CreateDefaultRenderPass(device);
|
|
|
|
depthStencilView = CreateDefaultDepthStencilView(device);
|
2017-05-31 00:03:44 +00:00
|
|
|
|
|
|
|
auto depthStencilState = device.CreateDepthStencilStateBuilder()
|
2017-06-01 15:30:03 +00:00
|
|
|
.SetDepthCompareFunction(nxt::CompareFunction::Less)
|
|
|
|
.SetDepthWriteEnabled(true)
|
2017-05-31 00:03:44 +00:00
|
|
|
.GetResult();
|
|
|
|
|
2017-07-14 15:32:57 +00:00
|
|
|
pipeline = device.CreateRenderPipelineBuilder()
|
2017-05-31 00:03:44 +00:00
|
|
|
.SetSubpass(renderpass, 0)
|
|
|
|
.SetLayout(pl)
|
|
|
|
.SetStage(nxt::ShaderStage::Vertex, vsModule, "main")
|
|
|
|
.SetStage(nxt::ShaderStage::Fragment, fsModule, "main")
|
|
|
|
.SetInputState(inputState)
|
|
|
|
.SetDepthStencilState(depthStencilState)
|
|
|
|
.GetResult();
|
|
|
|
|
|
|
|
auto planeStencilState = device.CreateDepthStencilStateBuilder()
|
2017-06-01 15:30:03 +00:00
|
|
|
.SetDepthCompareFunction(nxt::CompareFunction::Less)
|
|
|
|
.SetDepthWriteEnabled(false)
|
|
|
|
.SetStencilFunction(nxt::Face::Both, nxt::CompareFunction::Always, nxt::StencilOperation::Keep, nxt::StencilOperation::Keep, nxt::StencilOperation::Replace)
|
2017-05-31 00:03:44 +00:00
|
|
|
.GetResult();
|
|
|
|
|
2017-07-14 15:32:57 +00:00
|
|
|
planePipeline = device.CreateRenderPipelineBuilder()
|
2017-05-31 00:03:44 +00:00
|
|
|
.SetSubpass(renderpass, 0)
|
|
|
|
.SetLayout(pl)
|
|
|
|
.SetStage(nxt::ShaderStage::Vertex, vsModule, "main")
|
|
|
|
.SetStage(nxt::ShaderStage::Fragment, fsModule, "main")
|
|
|
|
.SetInputState(inputState)
|
|
|
|
.SetDepthStencilState(planeStencilState)
|
|
|
|
.GetResult();
|
|
|
|
|
|
|
|
auto reflectionStencilState = device.CreateDepthStencilStateBuilder()
|
2017-06-01 15:30:03 +00:00
|
|
|
.SetDepthCompareFunction(nxt::CompareFunction::Less)
|
|
|
|
.SetDepthWriteEnabled(true)
|
|
|
|
.SetStencilFunction(nxt::Face::Both, nxt::CompareFunction::Equal, nxt::StencilOperation::Keep, nxt::StencilOperation::Keep, nxt::StencilOperation::Replace)
|
2017-05-31 00:03:44 +00:00
|
|
|
.GetResult();
|
|
|
|
|
2017-07-14 15:32:57 +00:00
|
|
|
reflectionPipeline = device.CreateRenderPipelineBuilder()
|
2017-05-31 00:03:44 +00:00
|
|
|
.SetSubpass(renderpass, 0)
|
|
|
|
.SetLayout(pl)
|
|
|
|
.SetStage(nxt::ShaderStage::Vertex, vsModule, "main")
|
|
|
|
.SetStage(nxt::ShaderStage::Fragment, fsReflectionModule, "main")
|
|
|
|
.SetInputState(inputState)
|
|
|
|
.SetDepthStencilState(reflectionStencilState)
|
|
|
|
.GetResult();
|
|
|
|
|
|
|
|
cameraData.proj = glm::perspective(glm::radians(45.0f), 1.f, 1.0f, 100.0f);
|
|
|
|
}
|
|
|
|
|
|
|
|
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.01f;
|
2017-05-31 00:03:44 +00:00
|
|
|
if (s.b >= 1.0f) {s.b = 0.0f;}
|
|
|
|
static const uint32_t vertexBufferOffsets[1] = {0};
|
|
|
|
|
|
|
|
cameraData.view = glm::lookAt(
|
|
|
|
glm::vec3(8.f * std::sin(glm::radians(s.b * 360.f)), 2.f, 8.f * std::cos(glm::radians(s.b * 360.f))),
|
|
|
|
glm::vec3(0.0f, 0.0f, 0.0f),
|
|
|
|
glm::vec3(0.0f, 1.0f, 0.0f)
|
|
|
|
);
|
|
|
|
|
2017-06-12 21:33:44 +00:00
|
|
|
cameraBuffer.TransitionUsage(nxt::BufferUsageBit::TransferDst);
|
2017-06-02 18:31:53 +00:00
|
|
|
cameraBuffer.SetSubData(0, sizeof(CameraData) / sizeof(uint32_t), reinterpret_cast<uint32_t*>(&cameraData));
|
2017-05-31 00:03:44 +00:00
|
|
|
|
2017-07-28 01:30:57 +00:00
|
|
|
nxt::Texture backbuffer;
|
|
|
|
nxt::Framebuffer framebuffer;
|
|
|
|
GetNextFramebuffer(device, renderpass, swapchain, depthStencilView, &backbuffer, &framebuffer);
|
|
|
|
|
2017-05-31 00:03:44 +00:00
|
|
|
nxt::CommandBuffer commands = device.CreateCommandBufferBuilder()
|
|
|
|
.BeginRenderPass(renderpass, framebuffer)
|
2017-06-30 06:53:08 +00:00
|
|
|
.BeginRenderSubpass()
|
2017-07-14 15:32:57 +00:00
|
|
|
.SetRenderPipeline(pipeline)
|
2017-05-31 00:03:44 +00:00
|
|
|
.TransitionBufferUsage(cameraBuffer, nxt::BufferUsageBit::Uniform)
|
|
|
|
.SetBindGroup(0, bindGroup[0])
|
|
|
|
.SetVertexBuffers(0, 1, &vertexBuffer, vertexBufferOffsets)
|
|
|
|
.SetIndexBuffer(indexBuffer, 0, nxt::IndexFormat::Uint32)
|
|
|
|
.DrawElements(36, 1, 0, 0)
|
|
|
|
|
2017-06-01 15:30:03 +00:00
|
|
|
.SetStencilReference(0x1)
|
2017-07-14 15:32:57 +00:00
|
|
|
.SetRenderPipeline(planePipeline)
|
2017-07-21 22:36:13 +00:00
|
|
|
.SetBindGroup(0, bindGroup[0])
|
2017-05-31 00:03:44 +00:00
|
|
|
.SetVertexBuffers(0, 1, &planeBuffer, vertexBufferOffsets)
|
|
|
|
.DrawElements(6, 1, 0, 0)
|
|
|
|
|
2017-07-14 15:32:57 +00:00
|
|
|
.SetRenderPipeline(reflectionPipeline)
|
2017-05-31 00:03:44 +00:00
|
|
|
.SetVertexBuffers(0, 1, &vertexBuffer, vertexBufferOffsets)
|
|
|
|
.SetBindGroup(0, bindGroup[1])
|
|
|
|
.DrawElements(36, 1, 0, 0)
|
2017-06-30 06:53:08 +00:00
|
|
|
.EndRenderSubpass()
|
2017-05-31 00:03:44 +00:00
|
|
|
.EndRenderPass()
|
|
|
|
.GetResult();
|
|
|
|
|
|
|
|
queue.Submit(1, &commands);
|
2017-07-28 01:30:57 +00:00
|
|
|
backbuffer.TransitionUsage(nxt::TextureUsageBit::Present);
|
|
|
|
swapchain.Present(backbuffer);
|
|
|
|
DoFlush();
|
2017-05-31 00:03:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, const char* argv[]) {
|
2017-06-19 17:15:13 +00:00
|
|
|
if (!InitSample(argc, argv)) {
|
2017-05-31 00:03:44 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
init();
|
|
|
|
|
|
|
|
while (!ShouldQuit()) {
|
|
|
|
frame();
|
2017-07-17 21:13:57 +00:00
|
|
|
utils::USleep(16000);
|
2017-05-31 00:03:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO release stuff
|
|
|
|
}
|