mirror of
https://github.com/encounter/dawn-cmake.git
synced 2025-12-09 21:47:47 +00:00
Split NXT helpers from example/Utils into src/utils
This will make it possible to use them in the test suites
This commit is contained in:
committed by
Corentin Wallez
parent
1bd219d8a8
commit
5ee7afdfbe
@@ -19,6 +19,8 @@ list(APPEND UTILS_SOURCES
|
||||
${UTILS_DIR}/BackendBinding.h
|
||||
${UTILS_DIR}/NullBinding.cpp
|
||||
${UTILS_DIR}/OpenGLBinding.cpp
|
||||
${UTILS_DIR}/NXTHelpers.cpp
|
||||
${UTILS_DIR}/NXTHelpers.h
|
||||
)
|
||||
|
||||
if (APPLE)
|
||||
|
||||
106
src/utils/NXTHelpers.cpp
Normal file
106
src/utils/NXTHelpers.cpp
Normal file
@@ -0,0 +1,106 @@
|
||||
// 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 "NXTHelpers.h"
|
||||
|
||||
#include <shaderc/shaderc.hpp>
|
||||
|
||||
#include <iomanip>
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
|
||||
namespace utils {
|
||||
|
||||
nxt::ShaderModule CreateShaderModule(const nxt::Device& device, nxt::ShaderStage stage, const char* source) {
|
||||
shaderc::Compiler compiler;
|
||||
shaderc::CompileOptions options;
|
||||
|
||||
shaderc_shader_kind kind;
|
||||
switch (stage) {
|
||||
case nxt::ShaderStage::Vertex:
|
||||
kind = shaderc_glsl_vertex_shader;
|
||||
break;
|
||||
case nxt::ShaderStage::Fragment:
|
||||
kind = shaderc_glsl_fragment_shader;
|
||||
break;
|
||||
case nxt::ShaderStage::Compute:
|
||||
kind = shaderc_glsl_compute_shader;
|
||||
break;
|
||||
}
|
||||
|
||||
auto result = compiler.CompileGlslToSpv(source, strlen(source), kind, "myshader?", options);
|
||||
if (result.GetCompilationStatus() != shaderc_compilation_status_success) {
|
||||
std::cerr << result.GetErrorMessage();
|
||||
return {};
|
||||
}
|
||||
|
||||
size_t size = (result.cend() - result.cbegin());
|
||||
|
||||
#ifdef DUMP_SPIRV_ASSEMBLY
|
||||
{
|
||||
auto resultAsm = compiler.CompileGlslToSpvAssembly(source, strlen(source), kind, "myshader?", options);
|
||||
size_t sizeAsm = (resultAsm.cend() - resultAsm.cbegin());
|
||||
|
||||
char* buffer = reinterpret_cast<char*>(malloc(sizeAsm + 1));
|
||||
memcpy(buffer, resultAsm.cbegin(), sizeAsm);
|
||||
buffer[sizeAsm] = '\0';
|
||||
printf("SPIRV ASSEMBLY DUMP START\n%s\nSPIRV ASSEMBLY DUMP END\n", buffer);
|
||||
free(buffer);
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef DUMP_SPIRV_JS_ARRAY
|
||||
printf("SPIRV JS ARRAY DUMP START\n");
|
||||
for (size_t i = 0; i < size; i++) {
|
||||
printf("%#010x", result.cbegin()[i]);
|
||||
if ((i + 1) % 4 == 0) {
|
||||
printf(",\n");
|
||||
} else {
|
||||
printf(", ");
|
||||
}
|
||||
}
|
||||
printf("\n");
|
||||
printf("SPIRV JS ARRAY DUMP END\n");
|
||||
#endif
|
||||
|
||||
return device.CreateShaderModuleBuilder()
|
||||
.SetSource(size, result.cbegin())
|
||||
.GetResult();
|
||||
}
|
||||
|
||||
void CreateDefaultRenderPass(const nxt::Device& device, nxt::RenderPass* renderPass, nxt::Framebuffer* framebuffer) {
|
||||
*renderPass = device.CreateRenderPassBuilder()
|
||||
.SetAttachmentCount(1)
|
||||
.AttachmentSetFormat(0, nxt::TextureFormat::R8G8B8A8Unorm)
|
||||
.SetSubpassCount(1)
|
||||
.SubpassSetColorAttachment(0, 0, 0)
|
||||
.GetResult();
|
||||
*framebuffer = device.CreateFramebufferBuilder()
|
||||
.SetRenderPass(*renderPass)
|
||||
.SetDimensions(640, 480)
|
||||
.GetResult();
|
||||
}
|
||||
|
||||
nxt::Buffer CreateFrozenBufferFromData(const nxt::Device& device, const void* data, uint32_t size, nxt::BufferUsageBit usage) {
|
||||
nxt::Buffer buffer = device.CreateBufferBuilder()
|
||||
.SetAllowedUsage(nxt::BufferUsageBit::TransferDst | usage)
|
||||
.SetInitialUsage(nxt::BufferUsageBit::TransferDst)
|
||||
.SetSize(size)
|
||||
.GetResult();
|
||||
buffer.SetSubData(0, size / sizeof(uint32_t), reinterpret_cast<const uint32_t*>(data));
|
||||
buffer.FreezeUsage(usage);
|
||||
return buffer;
|
||||
}
|
||||
|
||||
}
|
||||
23
src/utils/NXTHelpers.h
Normal file
23
src/utils/NXTHelpers.h
Normal file
@@ -0,0 +1,23 @@
|
||||
// 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 <nxt/nxtcpp.h>
|
||||
|
||||
namespace utils {
|
||||
|
||||
nxt::ShaderModule CreateShaderModule(const nxt::Device& device, nxt::ShaderStage stage, const char* source);
|
||||
void CreateDefaultRenderPass(const nxt::Device& device, nxt::RenderPass* renderPass, nxt::Framebuffer* framebuffer);
|
||||
nxt::Buffer CreateFrozenBufferFromData(const nxt::Device& device, const void* data, uint32_t size, nxt::BufferUsageBit usage);
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user