2017-05-16 21:04:22 +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-16 21:04:22 +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-16 21:04:22 +00:00
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
nxt::Device device;
|
|
|
|
|
|
|
|
nxt::Buffer vertexBuffer;
|
|
|
|
nxt::Buffer vertexBufferQuad;
|
|
|
|
|
|
|
|
nxt::Texture renderTarget;
|
|
|
|
nxt::TextureView renderTargetView;
|
|
|
|
nxt::Sampler samplerPost;
|
|
|
|
|
|
|
|
nxt::Queue queue;
|
2017-07-28 01:30:57 +00:00
|
|
|
nxt::SwapChain swapchain;
|
2017-07-14 15:32:57 +00:00
|
|
|
nxt::RenderPipeline pipeline;
|
|
|
|
nxt::RenderPipeline pipelinePost;
|
2017-05-16 21:04:22 +00:00
|
|
|
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,
|
|
|
|
};
|
2017-06-19 17:09:41 +00:00
|
|
|
vertexBuffer = utils::CreateFrozenBufferFromData(device, vertexData, sizeof(vertexData), nxt::BufferUsageBit::Vertex);
|
2017-05-16 21:04:22 +00:00
|
|
|
|
|
|
|
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,
|
|
|
|
};
|
2017-06-19 17:09:41 +00:00
|
|
|
vertexBufferQuad = utils::CreateFrozenBufferFromData(device, vertexDataQuad, sizeof(vertexDataQuad), nxt::BufferUsageBit::Vertex);
|
2017-05-16 21:04:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void initTextures() {
|
|
|
|
renderTarget = device.CreateTextureBuilder()
|
|
|
|
.SetDimension(nxt::TextureDimension::e2D)
|
|
|
|
.SetExtent(640, 480, 1)
|
|
|
|
.SetFormat(nxt::TextureFormat::R8G8B8A8Unorm)
|
|
|
|
.SetMipLevels(1)
|
2017-07-07 23:06:14 +00:00
|
|
|
.SetAllowedUsage(nxt::TextureUsageBit::OutputAttachment | nxt::TextureUsageBit::Sampled)
|
|
|
|
.SetInitialUsage(nxt::TextureUsageBit::OutputAttachment)
|
2017-05-16 21:04:22 +00:00
|
|
|
.GetResult();
|
|
|
|
renderTargetView = renderTarget.CreateTextureViewBuilder().GetResult();
|
|
|
|
|
2018-05-22 18:50:59 +00:00
|
|
|
nxt::SamplerDescriptor samplerDesc = utils::GetDefaultSamplerDescriptor();
|
|
|
|
samplerPost = device.CreateSampler(&samplerDesc);
|
2017-05-16 21:04:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void initPipeline() {
|
2017-06-19 17:09:41 +00:00
|
|
|
nxt::ShaderModule vsModule = utils::CreateShaderModule(device, nxt::ShaderStage::Vertex, R"(
|
2017-05-16 21:04:22 +00:00
|
|
|
#version 450
|
|
|
|
layout(location = 0) in vec4 pos;
|
|
|
|
void main() {
|
|
|
|
gl_Position = pos;
|
|
|
|
})"
|
|
|
|
);
|
|
|
|
|
2017-06-19 17:09:41 +00:00
|
|
|
nxt::ShaderModule fsModule = utils::CreateShaderModule(device, nxt::ShaderStage::Fragment, R"(
|
2017-05-16 21:04:22 +00:00
|
|
|
#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();
|
|
|
|
|
2017-07-14 15:32:57 +00:00
|
|
|
pipeline = device.CreateRenderPipelineBuilder()
|
2018-05-02 22:10:13 +00:00
|
|
|
.SetColorAttachmentFormat(0, nxt::TextureFormat::R8G8B8A8Unorm)
|
2017-05-16 21:04:22 +00:00
|
|
|
.SetStage(nxt::ShaderStage::Vertex, vsModule, "main")
|
|
|
|
.SetStage(nxt::ShaderStage::Fragment, fsModule, "main")
|
|
|
|
.SetInputState(inputState)
|
|
|
|
.GetResult();
|
|
|
|
}
|
|
|
|
|
|
|
|
void initPipelinePost() {
|
2017-06-19 17:09:41 +00:00
|
|
|
nxt::ShaderModule vsModule = utils::CreateShaderModule(device, nxt::ShaderStage::Vertex, R"(
|
2017-05-16 21:04:22 +00:00
|
|
|
#version 450
|
|
|
|
layout(location = 0) in vec4 pos;
|
|
|
|
void main() {
|
|
|
|
gl_Position = pos;
|
|
|
|
})"
|
|
|
|
);
|
|
|
|
|
2017-06-19 17:09:41 +00:00
|
|
|
nxt::ShaderModule fsModule = utils::CreateShaderModule(device, nxt::ShaderStage::Fragment, R"(
|
2017-05-16 21:04:22 +00:00
|
|
|
#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 = device.CreatePipelineLayoutBuilder()
|
|
|
|
.SetBindGroupLayout(0, bgl)
|
|
|
|
.GetResult();
|
|
|
|
|
2017-07-14 15:32:57 +00:00
|
|
|
pipelinePost = device.CreateRenderPipelineBuilder()
|
2018-05-02 22:10:13 +00:00
|
|
|
.SetColorAttachmentFormat(0, nxt::TextureFormat::R8G8B8A8Unorm)
|
2017-05-16 21:04:22 +00:00
|
|
|
.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() {
|
2017-05-29 18:30:29 +00:00
|
|
|
device = CreateCppNXTDevice();
|
2017-05-16 21:04:22 +00:00
|
|
|
|
2018-06-15 00:26:27 +00:00
|
|
|
queue = device.CreateQueue();
|
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-05-16 21:04:22 +00:00
|
|
|
|
|
|
|
initBuffers();
|
|
|
|
initTextures();
|
|
|
|
initPipeline();
|
|
|
|
initPipelinePost();
|
|
|
|
}
|
|
|
|
|
|
|
|
void frame() {
|
2017-07-28 01:30:57 +00:00
|
|
|
nxt::Texture backbuffer = swapchain.GetNextTexture();
|
|
|
|
auto backbufferView = backbuffer.CreateTextureViewBuilder().GetResult();
|
2018-05-02 22:10:13 +00:00
|
|
|
|
2018-05-11 17:04:44 +00:00
|
|
|
nxt::RenderPassDescriptor renderPass1 = device.CreateRenderPassDescriptorBuilder()
|
2018-05-02 22:10:13 +00:00
|
|
|
.SetColorAttachment(0, renderTargetView, nxt::LoadOp::Clear)
|
|
|
|
.GetResult();
|
|
|
|
|
2018-05-11 17:04:44 +00:00
|
|
|
nxt::RenderPassDescriptor renderPass2 = device.CreateRenderPassDescriptorBuilder()
|
2018-05-02 22:10:13 +00:00
|
|
|
.SetColorAttachment(0, backbufferView, nxt::LoadOp::Clear)
|
2017-07-28 01:30:57 +00:00
|
|
|
.GetResult();
|
|
|
|
|
2017-05-16 21:04:22 +00:00
|
|
|
static const uint32_t vertexBufferOffsets[1] = {0};
|
|
|
|
nxt::CommandBuffer commands = device.CreateCommandBufferBuilder()
|
2018-05-02 22:10:13 +00:00
|
|
|
.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)
|
2017-05-16 21:04:22 +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-16 21:04:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, const char* argv[]) {
|
2017-06-19 17:15:13 +00:00
|
|
|
if (!InitSample(argc, argv)) {
|
2017-05-16 21:04:22 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
init();
|
|
|
|
|
|
|
|
while (!ShouldQuit()) {
|
|
|
|
frame();
|
2017-07-17 21:13:57 +00:00
|
|
|
utils::USleep(16000);
|
2017-05-16 21:04:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO release stuff
|
|
|
|
}
|