Skip to content

Commit

Permalink
Updated code to match the book text
Browse files Browse the repository at this point in the history
  • Loading branch information
corporateshark committed Feb 2, 2025
1 parent b352c2b commit 1c15615
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 50 deletions.
8 changes: 4 additions & 4 deletions Chapter06/02_BRDF_LUT/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,11 @@ int main()
.vkFormat = VK_FORMAT_R16G16B16A16_SFLOAT,
.baseWidth = kBrdfW,
.baseHeight = kBrdfH,
.baseDepth = 1u,
.numDimensions = 2u,
.baseDepth = 1,
.numDimensions = 2,
.numLevels = 1,
.numLayers = 1u,
.numFaces = 1u,
.numLayers = 1,
.numFaces = 1,
.generateMipmaps = KTX_FALSE,
};

Expand Down
10 changes: 5 additions & 5 deletions Chapter06/03_FilterEnvmap/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -258,11 +258,11 @@ void process_cubemap(
.vkFormat = VK_FORMAT_R32G32B32A32_SFLOAT,
.baseWidth = 64,
.baseHeight = 64,
.baseDepth = 1u,
.numDimensions = 2u,
.numLevels = 1u,
.numLayers = 1u,
.numFaces = 6u,
.baseDepth = 1,
.numDimensions = 2,
.numLevels = 1,
.numLayers = 1,
.numFaces = 6,
.generateMipmaps = KTX_FALSE,
};

Expand Down
47 changes: 18 additions & 29 deletions Chapter06/04_MetallicRoughness/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ int main()
.debugName = "PerFrame materials",
});

struct PerDrawData {
struct PerFrameData {
mat4 model;
mat4 view;
mat4 proj;
Expand All @@ -284,45 +284,24 @@ int main()
uint32_t envId;
};

lvk::Holder<lvk::BufferHandle> drawableBuffers[2] = {
ctx->createBuffer({
.usage = lvk::BufferUsageBits_Uniform,
.storage = lvk::StorageType_HostVisible,
.size = sizeof(PerDrawData),
.debugName = "PerDraw 1",
}),
ctx->createBuffer({
.usage = lvk::BufferUsageBits_Uniform,
.storage = lvk::StorageType_HostVisible,
.size = sizeof(PerDrawData),
.debugName = "PerDraw 2",
}),
};
lvk::Holder<lvk::BufferHandle> perFrameBuffer = ctx->createBuffer({
.usage = lvk::BufferUsageBits_Storage,
.storage = lvk::StorageType_HostVisible,
.size = sizeof(PerFrameData),
.debugName = "perFrameBuffer",
});

LVK_ASSERT(pipelineSolid.valid());

aiReleaseImport(scene);

const bool rotateModel = true;
uint32_t currentBuffer = 0;

app.run([&](uint32_t width, uint32_t height, float aspectRatio, float deltaSeconds) {
const mat4 m1 = glm::rotate(mat4(1.0f), glm::radians(+90.0f), vec3(1, 0, 0));
const mat4 m2 = glm::rotate(mat4(1.0f), rotateModel ? (float)glfwGetTime() : 0.0f, vec3(0.0f, 1.0f, 0.0f));
const mat4 p = glm::perspective(45.0f, aspectRatio, 0.1f, 1000.0f);

const PerDrawData perDrawData = {
.model = m2 * m1,
.view = app.camera_.getViewMatrix(),
.proj = p,
.cameraPos = vec4(app.camera_.getPosition(), 1.0f),
.matId = 0,
.envId = 0,
};

ctx->upload(drawableBuffers[currentBuffer], &perDrawData, sizeof(PerDrawData));
currentBuffer = (currentBuffer + 1) % LVK_ARRAY_NUM_ELEMENTS(drawableBuffers);

const lvk::RenderPass renderPass = {
.color = { { .loadOp = lvk::LoadOp_Clear, .clearColor = { 1.0f, 1.0f, 1.0f, 1.0f } } },
.depth = { .loadOp = lvk::LoadOp_Clear, .clearDepth = 1.0f }
Expand All @@ -333,8 +312,18 @@ int main()
.depthStencil = { .texture = app.getDepthTexture() },
};

const PerFrameData perFrameData = {
.model = m2 * m1,
.view = app.camera_.getViewMatrix(),
.proj = p,
.cameraPos = vec4(app.camera_.getPosition(), 1.0f),
.matId = 0,
.envId = 0,
};

lvk::ICommandBuffer& buf = ctx->acquireCommandBuffer();
{
buf.cmdUpdateBuffer(perFrameBuffer, perFrameData);
buf.cmdBeginRendering(renderPass, framebuffer);
{
buf.cmdPushDebugGroupLabel("Mesh", 0xff0000ff);
Expand All @@ -348,7 +337,7 @@ int main()
uint64_t materials;
uint64_t environments;
} perFrameData = {
.draw = ctx->gpuAddress(drawableBuffers[currentBuffer]),
.draw = ctx->gpuAddress(perFrameBuffer),
.materials = ctx->gpuAddress(matBuffer),
.environments = ctx->gpuAddress(envBuffer),
};
Expand Down
12 changes: 6 additions & 6 deletions Chapter06/05_SpecularGlossiness/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -345,18 +345,18 @@ int main()
.debugName = "PerFrame environments",
});

struct FrameData {
struct PerFrameData {
mat4 model;
mat4 view;
mat4 proj;
vec4 cameraPos;
};

lvk::Holder<lvk::BufferHandle> perFrameBuffer = ctx->createBuffer({
.usage = lvk::BufferUsageBits_Uniform,
.usage = lvk::BufferUsageBits_Storage,
.storage = lvk::StorageType_HostVisible,
.size = sizeof(FrameData),
.debugName = "Per Frame data",
.size = sizeof(PerFrameData),
.debugName = "perFrameBuffer",
});

LVK_ASSERT(pipelineSolid.valid());
Expand All @@ -366,13 +366,12 @@ int main()
app.run([&](uint32_t width, uint32_t height, float aspectRatio, float deltaSeconds) {
const mat4 p = glm::perspective(45.0f, aspectRatio, 0.01f, 100.0f);

const FrameData frameData = {
const PerFrameData perFrameData = {
.model = glm::rotate(mat4(1.0f), rotateModel ? (float)glfwGetTime() : 0.0f, vec3(0.0f, 1.0f, 0.0f)),
.view = app.camera_.getViewMatrix(),
.proj = p,
.cameraPos = vec4(app.camera_.getPosition(), 1.0f),
};
ctx->upload(perFrameBuffer, &frameData, sizeof(FrameData));

struct PushConstants {
mat4 model;
Expand Down Expand Up @@ -402,6 +401,7 @@ int main()

lvk::ICommandBuffer& buf = ctx->acquireCommandBuffer();
{
buf.cmdUpdateBuffer(perFrameBuffer, perFrameData);
buf.cmdBeginRendering(renderPass, framebuffer);
buf.cmdBindVertexBuffer(0, vertexBuffer, 0);
buf.cmdBindIndexBuffer(indexBuffer, lvk::IndexFormat_UI32);
Expand Down
8 changes: 4 additions & 4 deletions Chapter10/05_HDR/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,8 @@ int main()
.debugName = "texLuminance",
}) };

for (uint32_t l = 1; l != LVK_ARRAY_NUM_ELEMENTS(texLumViews); l++) {
texLumViews[l] = ctx->createTextureView(texLumViews[0], { .mipLevel = l, .swizzle = swizzle });
for (uint32_t v = 1; v != LVK_ARRAY_NUM_ELEMENTS(texLumViews); v++) {
texLumViews[v] = ctx->createTextureView(texLumViews[0], { .mipLevel = v, .swizzle = swizzle });
}

lvk::Holder<lvk::TextureHandle> offscreenColor = ctx->createTexture({
Expand Down Expand Up @@ -338,8 +338,8 @@ int main()
ImGui::Image(texBloomPass.index(), ImVec2(windowWidth, windowWidth / aspectRatio));
ImGui::Separator();
ImGui::Text("Luminance pyramid 512x512");
for (uint32_t l = 0; l != LVK_ARRAY_NUM_ELEMENTS(texLumViews); l++) {
ImGui::Image(texLumViews[l].index(), ImVec2((int)windowWidth >> l, ((int)windowWidth >> l)));
for (uint32_t v = 0; v != LVK_ARRAY_NUM_ELEMENTS(texLumViews); v++) {
ImGui::Image(texLumViews[v].index(), ImVec2((int)windowWidth >> v, (int)windowWidth >> v));
}
ImGui::Separator();
ImGui::End();
Expand Down
4 changes: 2 additions & 2 deletions Chapter10/06_HDR_Adaptation/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,8 @@ int main()
.debugName = "texLuminance",
}) };

for (uint32_t l = 1; l != LVK_ARRAY_NUM_ELEMENTS(texLumViews); l++) {
texLumViews[l] = ctx->createTextureView(texLumViews[0], { .mipLevel = l, .swizzle = swizzle });
for (uint32_t v = 1; v != LVK_ARRAY_NUM_ELEMENTS(texLumViews); v++) {
texLumViews[v] = ctx->createTextureView(texLumViews[0], { .mipLevel = v, .swizzle = swizzle });
}

const uint16_t brightPixel = glm::packHalf1x16(50.0f);
Expand Down

0 comments on commit 1c15615

Please sign in to comment.