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-05-30 21:59:09 +00:00
|
|
|
// Enable this before including any headers as we want inttypes.h to define
|
|
|
|
// format macros such as PRId64 that are used in picojson.
|
|
|
|
#ifndef __STDC_FORMAT_MACROS
|
|
|
|
#define __STDC_FORMAT_MACROS
|
|
|
|
#endif
|
|
|
|
|
2018-08-13 06:23:27 +00:00
|
|
|
#include "../SampleUtils.h"
|
2017-04-20 18:38:20 +00:00
|
|
|
|
2018-07-26 12:54:08 +00:00
|
|
|
#include "common/Assert.h"
|
2017-07-13 17:50:16 +00:00
|
|
|
#include "common/Math.h"
|
|
|
|
#include "common/Constants.h"
|
2018-12-10 19:47:22 +00:00
|
|
|
#include "utils/ComboRenderPipelineDescriptor.h"
|
2018-07-18 12:00:56 +00:00
|
|
|
#include "utils/DawnHelpers.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 <bitset>
|
|
|
|
#define GLM_FORCE_DEPTH_ZERO_TO_ONE
|
|
|
|
#include <glm/mat4x4.hpp>
|
|
|
|
#include <glm/gtc/matrix_inverse.hpp>
|
|
|
|
#include <glm/gtc/matrix_transform.hpp>
|
|
|
|
#include <glm/gtc/type_ptr.hpp>
|
|
|
|
|
|
|
|
#define TINYGLTF_LOADER_IMPLEMENTATION
|
|
|
|
#define STB_IMAGE_IMPLEMENTATION
|
2018-07-26 12:54:08 +00:00
|
|
|
#define PICOJSON_ASSERT ASSERT
|
2018-08-13 06:23:27 +00:00
|
|
|
#undef __STDC_FORMAT_MACROS
|
2017-04-20 18:38:20 +00:00
|
|
|
#include <tinygltfloader/tiny_gltf_loader.h>
|
|
|
|
|
2017-07-11 01:44:06 +00:00
|
|
|
#include "GLFW/glfw3.h"
|
|
|
|
|
2017-04-20 18:38:20 +00:00
|
|
|
#include "Camera.inl"
|
|
|
|
|
|
|
|
namespace gl {
|
|
|
|
enum {
|
|
|
|
Triangles = 0x0004,
|
|
|
|
UnsignedShort = 0x1403,
|
|
|
|
UnsignedInt = 0x1405,
|
|
|
|
Float = 0x1406,
|
|
|
|
RGBA = 0x1908,
|
|
|
|
Nearest = 0x2600,
|
|
|
|
Linear = 0x2601,
|
|
|
|
NearestMipmapNearest = 0x2700,
|
|
|
|
LinearMipmapNearest = 0x2701,
|
|
|
|
NearestMipmapLinear = 0x2702,
|
|
|
|
LinearMipmapLinear = 0x2703,
|
|
|
|
ArrayBuffer = 0x8892,
|
|
|
|
ElementArrayBuffer = 0x8893,
|
|
|
|
FragmentShader = 0x8B30,
|
|
|
|
VertexShader = 0x8B31,
|
|
|
|
FloatVec2 = 0x8B50,
|
|
|
|
FloatVec3 = 0x8B51,
|
|
|
|
FloatVec4 = 0x8B52,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
struct MaterialInfo {
|
2018-07-18 11:45:46 +00:00
|
|
|
dawn::RenderPipeline pipeline;
|
|
|
|
dawn::BindGroup bindGroup0;
|
2017-04-20 18:38:20 +00:00
|
|
|
std::map<uint32_t, std::string> slotSemantics;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct u_transform_block {
|
|
|
|
glm::mat4 modelViewProj;
|
|
|
|
glm::mat4 modelInvTr;
|
|
|
|
};
|
|
|
|
|
2018-07-18 11:45:46 +00:00
|
|
|
dawn::Device device;
|
|
|
|
dawn::Queue queue;
|
|
|
|
dawn::SwapChain swapchain;
|
|
|
|
dawn::TextureView depthStencilView;
|
2017-04-20 18:38:20 +00:00
|
|
|
|
2018-07-18 11:45:46 +00:00
|
|
|
dawn::Buffer defaultBuffer;
|
|
|
|
std::map<std::string, dawn::Buffer> buffers;
|
|
|
|
std::map<std::string, dawn::CommandBuffer> commandBuffers;
|
2017-04-20 18:38:20 +00:00
|
|
|
std::map<uint32_t, std::string> slotSemantics = {{0, "POSITION"}, {1, "NORMAL"}, {2, "TEXCOORD_0"}};
|
|
|
|
|
2018-07-18 11:45:46 +00:00
|
|
|
std::map<std::string, dawn::Sampler> samplers;
|
|
|
|
std::map<std::string, dawn::TextureView> textures;
|
2017-04-20 18:38:20 +00:00
|
|
|
|
|
|
|
tinygltf::Scene scene;
|
|
|
|
glm::mat4 projection = glm::perspective(glm::radians(60.f), 640.f/480, 0.1f, 2000.f);
|
|
|
|
Camera camera;
|
|
|
|
|
|
|
|
// Helpers
|
|
|
|
namespace {
|
|
|
|
std::string getFilePathExtension(const std::string &FileName) {
|
|
|
|
if (FileName.find_last_of(".") != std::string::npos) {
|
|
|
|
return FileName.substr(FileName.find_last_of(".") + 1);
|
|
|
|
}
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
2018-07-18 11:45:46 +00:00
|
|
|
bool techniqueParameterTypeToVertexFormat(int type, dawn::VertexFormat *format) {
|
2017-04-20 18:38:20 +00:00
|
|
|
switch (type) {
|
|
|
|
case gl::FloatVec2:
|
2019-03-21 11:18:31 +00:00
|
|
|
*format = dawn::VertexFormat::Float2;
|
2017-04-20 18:38:20 +00:00
|
|
|
return true;
|
|
|
|
case gl::FloatVec3:
|
2019-03-21 11:18:31 +00:00
|
|
|
*format = dawn::VertexFormat::Float3;
|
2017-04-20 18:38:20 +00:00
|
|
|
return true;
|
|
|
|
case gl::FloatVec4:
|
2019-03-21 11:18:31 +00:00
|
|
|
*format = dawn::VertexFormat::Float4;
|
2017-04-20 18:38:20 +00:00
|
|
|
return true;
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Initialization
|
|
|
|
namespace {
|
|
|
|
void initBuffers() {
|
2018-08-22 13:37:29 +00:00
|
|
|
dawn::BufferDescriptor descriptor;
|
|
|
|
descriptor.size = 256;
|
|
|
|
descriptor.usage = dawn::BufferUsageBit::Vertex | dawn::BufferUsageBit::Index;
|
|
|
|
defaultBuffer = device.CreateBuffer(&descriptor);
|
2017-04-20 18:38:20 +00:00
|
|
|
|
|
|
|
for (const auto& bv : scene.bufferViews) {
|
|
|
|
const auto& iBufferViewID = bv.first;
|
|
|
|
const auto& iBufferView = bv.second;
|
|
|
|
|
2018-07-18 11:45:46 +00:00
|
|
|
dawn::BufferUsageBit usage = dawn::BufferUsageBit::None;
|
2017-04-20 18:38:20 +00:00
|
|
|
switch (iBufferView.target) {
|
|
|
|
case gl::ArrayBuffer:
|
2018-07-18 11:45:46 +00:00
|
|
|
usage |= dawn::BufferUsageBit::Vertex;
|
2017-04-20 18:38:20 +00:00
|
|
|
break;
|
|
|
|
case gl::ElementArrayBuffer:
|
2018-07-18 11:45:46 +00:00
|
|
|
usage |= dawn::BufferUsageBit::Index;
|
2017-04-20 18:38:20 +00:00
|
|
|
break;
|
|
|
|
case 0:
|
|
|
|
fprintf(stderr, "TODO: buffer view has no target; skipping\n");
|
|
|
|
continue;
|
|
|
|
default:
|
|
|
|
fprintf(stderr, "unsupported buffer view target %d\n", iBufferView.target);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
const auto& iBuffer = scene.buffers.at(iBufferView.buffer);
|
|
|
|
|
2017-07-11 01:44:06 +00:00
|
|
|
size_t iBufferViewSize =
|
2017-04-20 18:38:20 +00:00
|
|
|
iBufferView.byteLength ? iBufferView.byteLength :
|
|
|
|
(iBuffer.data.size() - iBufferView.byteOffset);
|
2018-07-09 13:15:07 +00:00
|
|
|
auto oBuffer = utils::CreateBufferFromData(device, &iBuffer.data.at(iBufferView.byteOffset), static_cast<uint32_t>(iBufferViewSize), usage);
|
2017-04-20 18:38:20 +00:00
|
|
|
buffers[iBufferViewID] = std::move(oBuffer);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-11 01:44:06 +00:00
|
|
|
const MaterialInfo& getMaterial(const std::string& iMaterialID, size_t stridePos, size_t strideNor, size_t strideTxc) {
|
|
|
|
static std::map<std::tuple<std::string, size_t, size_t, size_t>, MaterialInfo> materials;
|
2017-04-20 18:38:20 +00:00
|
|
|
auto key = make_tuple(iMaterialID, stridePos, strideNor, strideTxc);
|
2017-07-11 01:44:06 +00:00
|
|
|
auto materialIterator = materials.find(key);
|
|
|
|
if (materialIterator != materials.end()) {
|
|
|
|
return materialIterator->second;
|
2017-04-20 18:38:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const auto& iMaterial = scene.materials.at(iMaterialID);
|
|
|
|
const auto& iTechnique = scene.techniques.at(iMaterial.technique);
|
|
|
|
|
2017-08-09 18:44:48 +00:00
|
|
|
bool hasTexture = false;
|
|
|
|
std::string iTextureID;
|
|
|
|
{
|
|
|
|
auto it = iMaterial.values.find("diffuse");
|
|
|
|
if (it != iMaterial.values.end() && !it->second.string_value.empty()) {
|
|
|
|
hasTexture = true;
|
|
|
|
iTextureID = it->second.string_value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-18 11:45:46 +00:00
|
|
|
auto oVSModule = utils::CreateShaderModule(device, dawn::ShaderStage::Vertex, R"(
|
2017-04-20 18:38:20 +00:00
|
|
|
#version 450
|
|
|
|
|
2018-07-17 18:12:47 +00:00
|
|
|
layout(push_constant) uniform u_transform_block {
|
2017-04-20 18:38:20 +00:00
|
|
|
mat4 modelViewProj;
|
|
|
|
mat4 modelInvTr;
|
|
|
|
} u_transform;
|
|
|
|
|
|
|
|
layout(location = 0) in vec4 a_position;
|
|
|
|
layout(location = 1) in vec3 a_normal;
|
|
|
|
layout(location = 2) in vec2 a_texcoord;
|
|
|
|
|
|
|
|
layout(location = 0) out vec3 v_normal;
|
|
|
|
layout(location = 1) out vec2 v_texcoord;
|
|
|
|
|
|
|
|
void main() {
|
|
|
|
v_normal = (u_transform.modelInvTr * vec4(normalize(a_normal), 0)).rgb;
|
|
|
|
v_texcoord = a_texcoord;
|
|
|
|
gl_Position = u_transform.modelViewProj * a_position;
|
|
|
|
})");
|
|
|
|
|
2017-08-09 18:44:48 +00:00
|
|
|
auto oFSSourceTextured = R"(
|
2017-04-20 18:38:20 +00:00
|
|
|
#version 450
|
|
|
|
|
2018-09-28 19:51:18 +00:00
|
|
|
layout(set = 0, binding = 0) uniform sampler u_samp;
|
|
|
|
layout(set = 0, binding = 1) uniform texture2D u_tex;
|
2017-04-20 18:38:20 +00:00
|
|
|
|
|
|
|
layout(location = 0) in vec3 v_normal;
|
|
|
|
layout(location = 1) in vec2 v_texcoord;
|
|
|
|
|
2018-01-12 19:49:31 +00:00
|
|
|
layout(location = 0) out vec4 fragcolor;
|
2017-04-20 18:38:20 +00:00
|
|
|
|
|
|
|
void main() {
|
|
|
|
const vec3 lightdir = normalize(vec3(-1, -2, 3));
|
|
|
|
vec3 normal = normalize(v_normal);
|
|
|
|
float diffuse = abs(dot(lightdir, normal));
|
|
|
|
float diffamb = diffuse * 0.85 + 0.15;
|
|
|
|
vec3 albedo = texture(sampler2D(u_tex, u_samp), v_texcoord).rgb;
|
|
|
|
fragcolor = vec4(diffamb * albedo, 1);
|
2017-08-09 18:44:48 +00:00
|
|
|
})";
|
|
|
|
auto oFSSourceUntextured = R"(
|
|
|
|
#version 450
|
|
|
|
|
|
|
|
layout(location = 0) in vec3 v_normal;
|
|
|
|
layout(location = 1) in vec2 v_texcoord;
|
|
|
|
|
2018-01-12 19:49:31 +00:00
|
|
|
layout(location = 0) out vec4 fragcolor;
|
2017-08-09 18:44:48 +00:00
|
|
|
|
|
|
|
void main() {
|
|
|
|
const vec3 lightdir = normalize(vec3(-1, -2, 3));
|
|
|
|
vec3 normal = normalize(v_normal);
|
|
|
|
float diffuse = abs(dot(lightdir, normal));
|
|
|
|
float diffamb = diffuse * 0.85 + 0.15;
|
|
|
|
fragcolor = vec4(vec3(diffamb), 1);
|
|
|
|
})";
|
|
|
|
|
2018-07-18 11:45:46 +00:00
|
|
|
auto oFSModule = utils::CreateShaderModule(device, dawn::ShaderStage::Fragment, hasTexture ? oFSSourceTextured : oFSSourceUntextured);
|
2017-04-20 18:38:20 +00:00
|
|
|
|
2019-03-27 18:08:50 +00:00
|
|
|
utils::ComboRenderPipelineDescriptor descriptor(device);
|
2019-03-28 17:09:23 +00:00
|
|
|
descriptor.cInputState.indexFormat = dawn::IndexFormat::Uint16;
|
2019-03-27 18:08:50 +00:00
|
|
|
uint32_t numAttributes = 0;
|
|
|
|
uint32_t numInputs = 0;
|
2017-04-20 18:38:20 +00:00
|
|
|
std::bitset<3> slotsSet;
|
|
|
|
for (const auto& a : iTechnique.attributes) {
|
|
|
|
const auto iAttributeName = a.first;
|
|
|
|
const auto iParameter = iTechnique.parameters.at(a.second);
|
2018-07-18 11:45:46 +00:00
|
|
|
dawn::VertexFormat format;
|
2017-04-20 18:38:20 +00:00
|
|
|
if (!techniqueParameterTypeToVertexFormat(iParameter.type, &format)) {
|
|
|
|
fprintf(stderr, "unsupported technique parameter type %d\n", iParameter.type);
|
|
|
|
continue;
|
|
|
|
}
|
2019-03-28 05:09:01 +00:00
|
|
|
descriptor.cInputState.cAttributes[numAttributes].format = format;
|
2019-02-14 23:56:07 +00:00
|
|
|
|
2017-04-20 18:38:20 +00:00
|
|
|
if (iParameter.semantic == "POSITION") {
|
2019-03-28 05:09:01 +00:00
|
|
|
descriptor.cInputState.cInputs[numInputs].stride = static_cast<uint32_t>(stridePos);
|
2019-03-27 18:08:50 +00:00
|
|
|
numAttributes++;
|
|
|
|
numInputs++;
|
2017-04-20 18:38:20 +00:00
|
|
|
slotsSet.set(0);
|
|
|
|
} else if (iParameter.semantic == "NORMAL") {
|
2019-03-28 05:09:01 +00:00
|
|
|
descriptor.cInputState.cAttributes[numAttributes].shaderLocation = 1;
|
|
|
|
descriptor.cInputState.cAttributes[numAttributes].inputSlot = 1;
|
|
|
|
descriptor.cInputState.cInputs[numInputs].inputSlot = 1;
|
|
|
|
descriptor.cInputState.cInputs[numInputs].stride = static_cast<uint32_t>(strideNor);
|
2019-03-27 18:08:50 +00:00
|
|
|
numAttributes++;
|
|
|
|
numInputs++;
|
2017-04-20 18:38:20 +00:00
|
|
|
slotsSet.set(1);
|
|
|
|
} else if (iParameter.semantic == "TEXCOORD_0") {
|
2019-03-28 05:09:01 +00:00
|
|
|
descriptor.cInputState.cAttributes[numAttributes].shaderLocation = 2;
|
|
|
|
descriptor.cInputState.cAttributes[numAttributes].inputSlot = 2;
|
|
|
|
descriptor.cInputState.cInputs[numInputs].inputSlot = 2;
|
|
|
|
descriptor.cInputState.cInputs[numInputs].stride = static_cast<uint32_t>(strideTxc);
|
2019-03-27 18:08:50 +00:00
|
|
|
numAttributes++;
|
|
|
|
numInputs++;
|
2017-04-20 18:38:20 +00:00
|
|
|
slotsSet.set(2);
|
|
|
|
} else {
|
|
|
|
fprintf(stderr, "unsupported technique attribute semantic %s\n", iParameter.semantic.c_str());
|
|
|
|
}
|
|
|
|
}
|
2017-07-11 01:44:06 +00:00
|
|
|
for (uint32_t i = 0; i < slotsSet.size(); i++) {
|
2017-04-20 18:38:20 +00:00
|
|
|
if (slotsSet[i]) {
|
|
|
|
continue;
|
|
|
|
}
|
2019-03-28 05:09:01 +00:00
|
|
|
descriptor.cInputState.cAttributes[numAttributes].shaderLocation = i;
|
|
|
|
descriptor.cInputState.cAttributes[numAttributes].inputSlot = i;
|
|
|
|
descriptor.cInputState.cAttributes[numAttributes].format = dawn::VertexFormat::Float4;
|
2019-03-27 18:08:50 +00:00
|
|
|
|
2019-03-28 05:09:01 +00:00
|
|
|
descriptor.cInputState.cInputs[numInputs].inputSlot = i;
|
2019-03-27 18:08:50 +00:00
|
|
|
|
|
|
|
numAttributes++;
|
|
|
|
numInputs++;
|
2017-04-20 18:38:20 +00:00
|
|
|
}
|
2019-03-27 18:08:50 +00:00
|
|
|
descriptor.cInputState.numAttributes = numAttributes;
|
|
|
|
descriptor.cInputState.numInputs = numInputs;
|
2017-04-20 18:38:20 +00:00
|
|
|
|
2018-07-18 11:45:46 +00:00
|
|
|
constexpr dawn::ShaderStageBit kNoStages{};
|
|
|
|
dawn::BindGroupLayout bindGroupLayout = utils::MakeBindGroupLayout(
|
2018-07-10 19:23:50 +00:00
|
|
|
device, {
|
2018-07-18 11:45:46 +00:00
|
|
|
{0, hasTexture ? dawn::ShaderStageBit::Fragment : kNoStages,
|
|
|
|
dawn::BindingType::Sampler},
|
|
|
|
{1, hasTexture ? dawn::ShaderStageBit::Fragment : kNoStages,
|
|
|
|
dawn::BindingType::SampledTexture},
|
2018-07-10 19:23:50 +00:00
|
|
|
});
|
2017-04-20 18:38:20 +00:00
|
|
|
|
2018-06-27 23:21:39 +00:00
|
|
|
auto pipelineLayout = utils::MakeBasicPipelineLayout(device, &bindGroupLayout);
|
2018-12-10 19:47:22 +00:00
|
|
|
|
|
|
|
descriptor.layout = pipelineLayout;
|
|
|
|
descriptor.cVertexStage.module = oVSModule;
|
|
|
|
descriptor.cFragmentStage.module = oFSModule;
|
2019-02-15 02:20:57 +00:00
|
|
|
descriptor.depthStencilState = &descriptor.cDepthStencilState;
|
|
|
|
descriptor.cDepthStencilState.format = dawn::TextureFormat::D32FloatS8Uint;
|
2019-02-20 13:00:36 +00:00
|
|
|
descriptor.cColorStates[0]->format = GetPreferredSwapChainTextureFormat();
|
2019-01-04 04:28:37 +00:00
|
|
|
descriptor.cDepthStencilState.depthWriteEnabled = true;
|
|
|
|
descriptor.cDepthStencilState.depthCompare = dawn::CompareFunction::Less;
|
2018-12-10 19:47:22 +00:00
|
|
|
|
|
|
|
dawn::RenderPipeline pipeline = device.CreateRenderPipeline(&descriptor);
|
2017-04-20 18:38:20 +00:00
|
|
|
|
2018-12-05 07:18:30 +00:00
|
|
|
dawn::BindGroup bindGroup;
|
2018-08-20 15:37:57 +00:00
|
|
|
|
2017-08-09 18:44:48 +00:00
|
|
|
if (hasTexture) {
|
|
|
|
const auto& textureView = textures[iTextureID];
|
|
|
|
const auto& iSamplerID = scene.textures[iTextureID].sampler;
|
2018-12-05 07:18:30 +00:00
|
|
|
bindGroup = utils::MakeBindGroup(device, bindGroupLayout, {
|
|
|
|
{0, samplers[iSamplerID]},
|
|
|
|
{1, textureView}
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
bindGroup = utils::MakeBindGroup(device, bindGroupLayout, {});
|
2017-04-20 18:38:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
MaterialInfo material = {
|
2018-12-05 07:18:30 +00:00
|
|
|
pipeline,
|
|
|
|
bindGroup,
|
2017-04-20 18:38:20 +00:00
|
|
|
std::map<uint32_t, std::string>(),
|
|
|
|
};
|
|
|
|
materials[key] = std::move(material);
|
|
|
|
return materials.at(key);
|
|
|
|
}
|
|
|
|
|
|
|
|
void initSamplers() {
|
|
|
|
for (const auto& s : scene.samplers) {
|
|
|
|
const auto& iSamplerID = s.first;
|
|
|
|
const auto& iSampler = s.second;
|
|
|
|
|
2018-07-18 11:45:46 +00:00
|
|
|
dawn::SamplerDescriptor desc = utils::GetDefaultSamplerDescriptor();
|
2018-05-17 21:09:07 +00:00
|
|
|
// TODO: wrap modes
|
|
|
|
|
2017-04-20 18:38:20 +00:00
|
|
|
switch (iSampler.magFilter) {
|
|
|
|
case gl::Nearest:
|
2018-07-18 11:45:46 +00:00
|
|
|
desc.magFilter = dawn::FilterMode::Nearest;
|
2017-04-20 18:38:20 +00:00
|
|
|
break;
|
|
|
|
case gl::Linear:
|
2018-07-18 11:45:46 +00:00
|
|
|
desc.magFilter = dawn::FilterMode::Linear;
|
2017-04-20 18:38:20 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
fprintf(stderr, "unsupported magFilter %d\n", iSampler.magFilter);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
switch (iSampler.minFilter) {
|
|
|
|
case gl::Nearest:
|
|
|
|
case gl::NearestMipmapNearest:
|
|
|
|
case gl::NearestMipmapLinear:
|
2018-07-18 11:45:46 +00:00
|
|
|
desc.minFilter = dawn::FilterMode::Nearest;
|
2017-04-20 18:38:20 +00:00
|
|
|
break;
|
|
|
|
case gl::Linear:
|
|
|
|
case gl::LinearMipmapNearest:
|
|
|
|
case gl::LinearMipmapLinear:
|
2018-07-18 11:45:46 +00:00
|
|
|
desc.minFilter = dawn::FilterMode::Linear;
|
2017-04-20 18:38:20 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
fprintf(stderr, "unsupported minFilter %d\n", iSampler.magFilter);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
switch (iSampler.minFilter) {
|
|
|
|
case gl::NearestMipmapNearest:
|
|
|
|
case gl::LinearMipmapNearest:
|
2018-07-18 11:45:46 +00:00
|
|
|
desc.mipmapFilter = dawn::FilterMode::Nearest;
|
2017-04-20 18:38:20 +00:00
|
|
|
break;
|
|
|
|
case gl::NearestMipmapLinear:
|
|
|
|
case gl::LinearMipmapLinear:
|
2018-07-18 11:45:46 +00:00
|
|
|
desc.mipmapFilter = dawn::FilterMode::Linear;
|
2017-04-20 18:38:20 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2018-05-17 21:09:07 +00:00
|
|
|
samplers[iSamplerID] = device.CreateSampler(&desc);
|
2017-04-20 18:38:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void initTextures() {
|
|
|
|
for (const auto& t : scene.textures) {
|
|
|
|
const auto& iTextureID = t.first;
|
|
|
|
const auto& iTexture = t.second;
|
|
|
|
const auto& iImage = scene.images[iTexture.source];
|
|
|
|
|
2018-07-18 11:45:46 +00:00
|
|
|
dawn::TextureFormat format = dawn::TextureFormat::R8G8B8A8Unorm;
|
2017-04-20 18:38:20 +00:00
|
|
|
switch (iTexture.format) {
|
|
|
|
case gl::RGBA:
|
2018-07-18 11:45:46 +00:00
|
|
|
format = dawn::TextureFormat::R8G8B8A8Unorm;
|
2017-04-20 18:38:20 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
fprintf(stderr, "unsupported texture format %d\n", iTexture.format);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-08-27 00:44:48 +00:00
|
|
|
dawn::TextureDescriptor descriptor;
|
|
|
|
descriptor.dimension = dawn::TextureDimension::e2D;
|
2018-09-18 12:49:22 +00:00
|
|
|
descriptor.size.width = iImage.width;
|
|
|
|
descriptor.size.height = iImage.height;
|
|
|
|
descriptor.size.depth = 1;
|
2019-02-21 00:45:19 +00:00
|
|
|
descriptor.arrayLayerCount = 1;
|
2018-12-12 09:27:16 +00:00
|
|
|
descriptor.sampleCount = 1;
|
2018-08-27 00:44:48 +00:00
|
|
|
descriptor.format = format;
|
2019-02-21 00:45:19 +00:00
|
|
|
descriptor.mipLevelCount = 1;
|
2018-08-27 00:44:48 +00:00
|
|
|
descriptor.usage = dawn::TextureUsageBit::TransferDst | dawn::TextureUsageBit::Sampled;
|
|
|
|
auto oTexture = device.CreateTexture(&descriptor);
|
2017-04-20 18:38:20 +00:00
|
|
|
// TODO: release this texture
|
|
|
|
|
|
|
|
const uint8_t* origData = iImage.image.data();
|
|
|
|
const uint8_t* data = nullptr;
|
|
|
|
std::vector<uint8_t> newData;
|
2017-07-13 17:50:16 +00:00
|
|
|
|
|
|
|
uint32_t width = static_cast<uint32_t>(iImage.width);
|
|
|
|
uint32_t height = static_cast<uint32_t>(iImage.height);
|
|
|
|
uint32_t rowSize = width * 4;
|
|
|
|
uint32_t rowPitch = Align(rowSize, kTextureRowPitchAlignment);
|
|
|
|
|
|
|
|
if (iImage.component == 3 || iImage.component == 4) {
|
|
|
|
if (rowSize != rowPitch || iImage.component == 3) {
|
|
|
|
newData.resize(rowPitch * height);
|
|
|
|
uint32_t pixelsPerRow = rowPitch / 4;
|
|
|
|
for (uint32_t y = 0; y < height; ++y) {
|
|
|
|
for (uint32_t x = 0; x < width; ++x) {
|
|
|
|
size_t oldIndex = x + y * height;
|
|
|
|
size_t newIndex = x + y * pixelsPerRow;
|
|
|
|
if (iImage.component == 4) {
|
|
|
|
newData[4 * newIndex + 0] = origData[4 * oldIndex + 0];
|
|
|
|
newData[4 * newIndex + 1] = origData[4 * oldIndex + 1];
|
|
|
|
newData[4 * newIndex + 2] = origData[4 * oldIndex + 2];
|
|
|
|
newData[4 * newIndex + 3] = origData[4 * oldIndex + 3];
|
|
|
|
} else if (iImage.component == 3) {
|
|
|
|
newData[4 * newIndex + 0] = origData[3 * oldIndex + 0];
|
|
|
|
newData[4 * newIndex + 1] = origData[3 * oldIndex + 1];
|
|
|
|
newData[4 * newIndex + 2] = origData[3 * oldIndex + 2];
|
|
|
|
newData[4 * newIndex + 3] = 255;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
data = newData.data();
|
|
|
|
} else {
|
|
|
|
data = origData;
|
2017-04-20 18:38:20 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
fprintf(stderr, "unsupported image.component %d\n", iImage.component);
|
|
|
|
}
|
|
|
|
|
2018-07-18 11:45:46 +00:00
|
|
|
dawn::Buffer staging = utils::CreateBufferFromData(device, data, rowPitch * iImage.height, dawn::BufferUsageBit::TransferSrc);
|
2018-11-28 17:54:13 +00:00
|
|
|
dawn::BufferCopyView bufferCopyView =
|
|
|
|
utils::CreateBufferCopyView(staging, 0, rowPitch, 0);
|
|
|
|
dawn::TextureCopyView textureCopyView =
|
2018-12-12 09:27:46 +00:00
|
|
|
utils::CreateTextureCopyView(oTexture, 0, 0, {0, 0, 0});
|
2018-11-28 17:54:13 +00:00
|
|
|
dawn::Extent3D copySize = {iImage.width, iImage.height, 1};
|
2019-02-15 12:54:08 +00:00
|
|
|
|
|
|
|
dawn::CommandEncoder encoder = device.CreateCommandEncoder();
|
|
|
|
encoder.CopyBufferToTexture(&bufferCopyView, &textureCopyView, ©Size);
|
|
|
|
|
|
|
|
dawn::CommandBuffer cmdbuf = encoder.Finish();
|
2017-04-20 18:38:20 +00:00
|
|
|
queue.Submit(1, &cmdbuf);
|
|
|
|
|
2019-04-09 16:57:00 +00:00
|
|
|
textures[iTextureID] = oTexture.CreateDefaultView();
|
2017-04-20 18:38:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void init() {
|
2018-07-18 12:06:10 +00:00
|
|
|
device = CreateCppDawnDevice();
|
2017-04-20 18:38:20 +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(),
|
2018-07-18 11:45:46 +00:00
|
|
|
dawn::TextureUsageBit::OutputAttachment, 640, 480);
|
2017-07-28 01:30:57 +00:00
|
|
|
|
|
|
|
depthStencilView = CreateDefaultDepthStencilView(device);
|
2017-04-20 18:38:20 +00:00
|
|
|
|
|
|
|
initBuffers();
|
|
|
|
initSamplers();
|
|
|
|
initTextures();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Drawing
|
|
|
|
namespace {
|
2018-09-21 00:24:37 +00:00
|
|
|
void drawMesh(dawn::RenderPassEncoder& pass, const tinygltf::Mesh& iMesh, const glm::mat4& model) {
|
2017-04-20 18:38:20 +00:00
|
|
|
for (const auto& iPrim : iMesh.primitives) {
|
|
|
|
if (iPrim.mode != gl::Triangles) {
|
|
|
|
fprintf(stderr, "unsupported primitive mode %d\n", iPrim.mode);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
u_transform_block transforms = {
|
|
|
|
(projection * camera.view() * model),
|
|
|
|
glm::inverseTranspose(model),
|
|
|
|
};
|
|
|
|
|
2017-07-11 01:44:06 +00:00
|
|
|
size_t strides[3] = {0};
|
2017-04-20 18:38:20 +00:00
|
|
|
for (const auto& s : slotSemantics) {
|
|
|
|
if (s.first < 3) {
|
|
|
|
auto it = iPrim.attributes.find(s.second);
|
|
|
|
if (it == iPrim.attributes.end()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
const auto& iAccessorName = it->second;
|
|
|
|
strides[s.first] = scene.accessors.at(iAccessorName).byteStride;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
const MaterialInfo& material = getMaterial(iPrim.material, strides[0], strides[1], strides[2]);
|
2018-12-21 10:40:26 +00:00
|
|
|
pass.SetPipeline(material.pipeline);
|
2019-03-18 06:01:37 +00:00
|
|
|
pass.SetBindGroup(0, material.bindGroup0, 0, nullptr);
|
2018-09-21 00:24:37 +00:00
|
|
|
pass.SetPushConstants(dawn::ShaderStageBit::Vertex,
|
2018-07-17 18:12:47 +00:00
|
|
|
0, sizeof(u_transform_block) / sizeof(uint32_t),
|
|
|
|
reinterpret_cast<const uint32_t*>(&transforms));
|
2017-04-20 18:38:20 +00:00
|
|
|
|
|
|
|
uint32_t vertexCount = 0;
|
|
|
|
for (const auto& s : slotSemantics) {
|
|
|
|
uint32_t slot = s.first;
|
|
|
|
auto it = iPrim.attributes.find(s.second);
|
|
|
|
if (it == iPrim.attributes.end()) {
|
2019-04-05 20:51:29 +00:00
|
|
|
uint64_t zero = 0;
|
2018-09-21 00:24:37 +00:00
|
|
|
pass.SetVertexBuffers(slot, 1, &defaultBuffer, &zero);
|
2017-04-20 18:38:20 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
const auto& iAccessor = scene.accessors.at(it->second);
|
|
|
|
if (iAccessor.componentType != gl::Float ||
|
|
|
|
(iAccessor.type != TINYGLTF_TYPE_VEC4 && iAccessor.type != TINYGLTF_TYPE_VEC3 && iAccessor.type != TINYGLTF_TYPE_VEC2)) {
|
|
|
|
fprintf(stderr, "unsupported vertex accessor component type %d and type %d\n", iAccessor.componentType, iAccessor.type);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2017-07-11 01:44:06 +00:00
|
|
|
if (vertexCount == 0) {
|
|
|
|
vertexCount = static_cast<uint32_t>(iAccessor.count);
|
2017-04-20 18:38:20 +00:00
|
|
|
}
|
|
|
|
const auto& oBuffer = buffers.at(iAccessor.bufferView);
|
2019-04-05 20:51:29 +00:00
|
|
|
uint64_t iBufferOffset = static_cast<uint64_t>(iAccessor.byteOffset);
|
2018-09-21 00:24:37 +00:00
|
|
|
pass.SetVertexBuffers(slot, 1, &oBuffer, &iBufferOffset);
|
2017-04-20 18:38:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!iPrim.indices.empty()) {
|
|
|
|
const auto& iIndices = scene.accessors.at(iPrim.indices);
|
|
|
|
// DrawElements
|
|
|
|
if (iIndices.componentType != gl::UnsignedShort || iIndices.type != TINYGLTF_TYPE_SCALAR) {
|
|
|
|
fprintf(stderr, "unsupported index accessor component type %d and type %d\n", iIndices.componentType, iIndices.type);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
const auto& oIndicesBuffer = buffers.at(iIndices.bufferView);
|
2018-09-21 00:24:37 +00:00
|
|
|
pass.SetIndexBuffer(oIndicesBuffer, static_cast<uint32_t>(iIndices.byteOffset));
|
2018-12-13 01:05:26 +00:00
|
|
|
pass.DrawIndexed(static_cast<uint32_t>(iIndices.count), 1, 0, 0, 0);
|
2017-04-20 18:38:20 +00:00
|
|
|
} else {
|
|
|
|
// DrawArrays
|
2018-12-10 05:20:19 +00:00
|
|
|
pass.Draw(vertexCount, 1, 0, 0);
|
2017-04-20 18:38:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-21 00:24:37 +00:00
|
|
|
void drawNode(dawn::RenderPassEncoder& pass, const tinygltf::Node& node, const glm::mat4& parent = glm::mat4()) {
|
2017-04-20 18:38:20 +00:00
|
|
|
glm::mat4 model;
|
|
|
|
if (node.matrix.size() == 16) {
|
|
|
|
model = glm::make_mat4(node.matrix.data());
|
|
|
|
} else {
|
|
|
|
if (node.scale.size() == 3) {
|
|
|
|
glm::vec3 scale = glm::make_vec3(node.scale.data());
|
|
|
|
model = glm::scale(model, scale);
|
|
|
|
}
|
|
|
|
if (node.rotation.size() == 4) {
|
|
|
|
glm::quat rotation = glm::make_quat(node.rotation.data());
|
|
|
|
model = glm::mat4_cast(rotation) * model;
|
|
|
|
}
|
|
|
|
if (node.translation.size() == 3) {
|
|
|
|
glm::vec3 translation = glm::make_vec3(node.translation.data());
|
|
|
|
model = glm::translate(model, translation);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
model = parent * model;
|
|
|
|
|
|
|
|
for (const auto& meshID : node.meshes) {
|
2018-09-21 00:24:37 +00:00
|
|
|
drawMesh(pass, scene.meshes[meshID], model);
|
2017-04-20 18:38:20 +00:00
|
|
|
}
|
|
|
|
for (const auto& child : node.children) {
|
2018-09-21 00:24:37 +00:00
|
|
|
drawNode(pass, scene.nodes.at(child), model);
|
2017-04-20 18:38:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void frame() {
|
2019-02-27 09:21:56 +00:00
|
|
|
dawn::Texture backbuffer = swapchain.GetNextTexture();
|
2017-07-28 01:30:57 +00:00
|
|
|
|
2017-04-20 18:38:20 +00:00
|
|
|
const auto& defaultSceneNodes = scene.scenes.at(scene.defaultScene);
|
2019-02-15 12:54:08 +00:00
|
|
|
dawn::CommandEncoder encoder = device.CreateCommandEncoder();
|
2018-09-21 00:24:37 +00:00
|
|
|
{
|
2019-04-09 16:57:00 +00:00
|
|
|
utils::ComboRenderPassDescriptor renderPass({backbuffer.CreateDefaultView()},
|
2019-02-27 09:21:56 +00:00
|
|
|
depthStencilView);
|
|
|
|
dawn::RenderPassEncoder pass = encoder.BeginRenderPass(&renderPass);
|
2018-09-21 00:24:37 +00:00
|
|
|
for (const auto& n : defaultSceneNodes) {
|
|
|
|
const auto& node = scene.nodes.at(n);
|
|
|
|
drawNode(pass, node);
|
|
|
|
}
|
|
|
|
pass.EndPass();
|
2017-04-20 18:38:20 +00:00
|
|
|
}
|
2018-09-21 00:24:37 +00:00
|
|
|
|
2019-02-15 12:54:08 +00:00
|
|
|
dawn::CommandBuffer commands = encoder.Finish();
|
2017-08-11 21:36:20 +00:00
|
|
|
queue.Submit(1, &commands);
|
|
|
|
|
2017-07-28 01:30:57 +00:00
|
|
|
swapchain.Present(backbuffer);
|
|
|
|
DoFlush();
|
2017-04-20 18:38:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Mouse camera control
|
|
|
|
namespace {
|
|
|
|
bool buttons[GLFW_MOUSE_BUTTON_LAST + 1] = {0};
|
|
|
|
|
2017-07-07 18:39:38 +00:00
|
|
|
void mouseButtonCallback(GLFWwindow*, int button, int action, int) {
|
2017-04-20 18:38:20 +00:00
|
|
|
buttons[button] = (action == GLFW_PRESS);
|
|
|
|
}
|
|
|
|
|
2017-07-07 18:39:38 +00:00
|
|
|
void cursorPosCallback(GLFWwindow*, double mouseX, double mouseY) {
|
2017-07-11 01:44:06 +00:00
|
|
|
static double oldX, oldY;
|
|
|
|
float dX = static_cast<float>(mouseX - oldX);
|
|
|
|
float dY = static_cast<float>(mouseY - oldY);
|
2017-04-20 18:38:20 +00:00
|
|
|
oldX = mouseX;
|
|
|
|
oldY = mouseY;
|
|
|
|
|
|
|
|
if (buttons[2] || (buttons[0] && buttons[1])) {
|
2017-07-11 01:44:06 +00:00
|
|
|
camera.pan(-dX * 0.002f, dY * 0.002f);
|
2017-04-20 18:38:20 +00:00
|
|
|
} else if (buttons[0]) {
|
2018-07-16 17:30:16 +00:00
|
|
|
camera.rotate(dX * 0.01f, dY * 0.01f);
|
2017-04-20 18:38:20 +00:00
|
|
|
} else if (buttons[1]) {
|
2017-07-11 01:44:06 +00:00
|
|
|
camera.zoom(dY * -0.005f);
|
2017-04-20 18:38:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-07 18:39:38 +00:00
|
|
|
void scrollCallback(GLFWwindow*, double, double yoffset) {
|
2017-07-11 01:44:06 +00:00
|
|
|
camera.zoom(static_cast<float>(yoffset) * 0.04f);
|
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;
|
|
|
|
}
|
|
|
|
if (argc < 2) {
|
2018-07-18 13:28:38 +00:00
|
|
|
fprintf(stderr, "Usage: %s model.gltf [... Dawn Options]\n", argv[0]);
|
2017-04-20 18:38:20 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
tinygltf::TinyGLTFLoader loader;
|
|
|
|
std::string err;
|
|
|
|
std::string input_filename(argv[1]);
|
|
|
|
std::string ext = getFilePathExtension(input_filename);
|
|
|
|
|
|
|
|
bool ret = false;
|
|
|
|
if (ext.compare("glb") == 0) {
|
|
|
|
// assume binary glTF.
|
|
|
|
ret = loader.LoadBinaryFromFile(&scene, &err, input_filename.c_str());
|
|
|
|
} else {
|
|
|
|
// assume ascii glTF.
|
|
|
|
ret = loader.LoadASCIIFromFile(&scene, &err, input_filename.c_str());
|
|
|
|
}
|
|
|
|
if (!err.empty()) {
|
|
|
|
fprintf(stderr, "ERR: %s\n", err.c_str());
|
|
|
|
}
|
|
|
|
if (!ret) {
|
|
|
|
fprintf(stderr, "Failed to load .glTF : %s\n", argv[1]);
|
|
|
|
exit(-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
init();
|
|
|
|
|
2017-05-29 18:30:29 +00:00
|
|
|
GLFWwindow* window = GetGLFWWindow();
|
2017-04-20 18:38:20 +00:00
|
|
|
glfwSetMouseButtonCallback(window, mouseButtonCallback);
|
|
|
|
glfwSetCursorPosCallback(window, cursorPosCallback);
|
|
|
|
glfwSetScrollCallback(window, scrollCallback);
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|