add buffer creation utility function

This commit is contained in:
Austin Eng 2017-06-01 13:37:47 -04:00 committed by Corentin Wallez
parent 00176858b5
commit 5a67d196be
3 changed files with 20 additions and 25 deletions

View File

@ -58,13 +58,7 @@ void initBuffers() {
20, 21, 22, 20, 21, 22,
20, 22, 23 20, 22, 23
}; };
indexBuffer = device.CreateBufferBuilder() indexBuffer = CreateFrozenBufferFromData(device, (void*)indexData, sizeof(indexData), nxt::BufferUsageBit::Index);
.SetAllowedUsage(nxt::BufferUsageBit::Mapped | nxt::BufferUsageBit::Index)
.SetInitialUsage(nxt::BufferUsageBit::Mapped)
.SetSize(sizeof(indexData))
.GetResult();
indexBuffer.SetSubData(0, sizeof(indexData) / sizeof(uint32_t), indexData);
indexBuffer.FreezeUsage(nxt::BufferUsageBit::Index);
static const float vertexData[6 * 4 * 6] = { 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,
@ -97,15 +91,7 @@ void initBuffers() {
-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
}; };
vertexBuffer = CreateFrozenBufferFromData(device, (void*)vertexData, sizeof(vertexData), nxt::BufferUsageBit::Vertex);
vertexBuffer = device.CreateBufferBuilder()
.SetAllowedUsage(nxt::BufferUsageBit::Mapped | nxt::BufferUsageBit::Vertex)
.SetInitialUsage(nxt::BufferUsageBit::Mapped)
.SetSize(sizeof(vertexData))
.GetResult();
vertexBuffer.SetSubData(0, sizeof(vertexData) / sizeof(uint32_t),
reinterpret_cast<const uint32_t*>(vertexData));
vertexBuffer.FreezeUsage(nxt::BufferUsageBit::Vertex);
static const float planeData[6 * 4] = { 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,
@ -113,15 +99,7 @@ void initBuffers() {
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,
}; };
planeBuffer = CreateFrozenBufferFromData(device, (void*)planeData, sizeof(planeData), nxt::BufferUsageBit::Vertex);
planeBuffer = device.CreateBufferBuilder()
.SetAllowedUsage(nxt::BufferUsageBit::Mapped | nxt::BufferUsageBit::Vertex)
.SetInitialUsage(nxt::BufferUsageBit::Mapped)
.SetSize(sizeof(planeData))
.GetResult();
planeBuffer.SetSubData(0, sizeof(planeData) / sizeof(uint32_t),
reinterpret_cast<const uint32_t*>(planeData));
planeBuffer.FreezeUsage(nxt::BufferUsageBit::Vertex);
} }
struct { struct {

View File

@ -245,6 +245,17 @@ void CreateDefaultRenderPass(const nxt::Device& device, nxt::RenderPass* renderP
.GetResult(); .GetResult();
} }
nxt::Buffer CreateFrozenBufferFromData(const nxt::Device& device, void* data, uint32_t size, nxt::BufferUsageBit usage) {
nxt::Buffer buffer = device.CreateBufferBuilder()
.SetAllowedUsage(nxt::BufferUsageBit::Mapped | usage)
.SetInitialUsage(nxt::BufferUsageBit::Mapped)
.SetSize(size)
.GetResult();
buffer.SetSubData(0, size / sizeof(uint32_t), reinterpret_cast<const uint32_t*>(data));
buffer.FreezeUsage(usage);
return buffer;
}
extern "C" { extern "C" {
bool InitUtils(int argc, const char** argv) { bool InitUtils(int argc, const char** argv) {
for (int i = 0; i < argc; i++) { for (int i = 0; i < argc; i++) {
@ -296,6 +307,10 @@ extern "C" {
return CreateShaderModule(device, static_cast<nxt::ShaderStage>(stage), source).Release(); return CreateShaderModule(device, static_cast<nxt::ShaderStage>(stage), source).Release();
} }
nxtBuffer CreateFrozenBufferFromData(nxtDevice device, void* data, uint32_t size, nxtBufferUsageBit usage) {
return CreateFrozenBufferFromData(device, data, size, usage);
}
void DoSwapBuffers() { void DoSwapBuffers() {
if (cmdBufType == CmdBufType::Terrible) { if (cmdBufType == CmdBufType::Terrible) {
c2sBuf->Flush(); c2sBuf->Flush();

View File

@ -34,7 +34,9 @@ extern "C" {
nxt::Device CreateCppNXTDevice(); nxt::Device CreateCppNXTDevice();
nxt::ShaderModule CreateShaderModule(const nxt::Device& device, nxt::ShaderStage stage, const char* source); 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); void CreateDefaultRenderPass(const nxt::Device& device, nxt::RenderPass* renderPass, nxt::Framebuffer* framebuffer);
nxt::Buffer CreateFrozenBufferFromData(const nxt::Device& device, void* data, uint32_t size, nxt::BufferUsageBit usage);
#else #else
nxtDevice CreateNXTDevice(); nxtDevice CreateNXTDevice();
nxtShaderModule CreateShaderModule(nxtDevice device, nxtShaderStage stage, const char* source); nxtShaderModule CreateShaderModule(nxtDevice device, nxtShaderStage stage, const char* source);
nxtBuffer CreateFrozenBufferFromData(nxtDevice device, void* data, uint32_t size, nxtBufferUsageBit usage);
#endif #endif