Skip to content

Commit

Permalink
render entire sponza scene except for textures
Browse files Browse the repository at this point in the history
  • Loading branch information
WinteryFox committed Feb 1, 2024
1 parent 00e0370 commit 1676cac
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 59 deletions.
1 change: 0 additions & 1 deletion .idea/codeStyles/Project.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions src/engine/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
project("Vixen Engine")

find_package(GLFW3 3.3 REQUIRED)
find_package(assimp REQUIRED)
pkg_check_modules(GLM REQUIRED IMPORTED_TARGET glm)
pkg_check_modules(ASSIMP REQUIRED IMPORTED_TARGET assimp)
pkg_check_modules(SPDLOG REQUIRED IMPORTED_TARGET spdlog)

add_definitions(-DGLFW_INCLUDE_NONE)
Expand All @@ -27,8 +27,8 @@ target_link_libraries(
Vixen
PUBLIC
glfw
assimp::assimp
PkgConfig::GLM
PkgConfig::ASSIMP
PkgConfig::SPDLOG
)

Expand Down
7 changes: 3 additions & 4 deletions src/engine/vk/Swapchain.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,11 @@ namespace Vixen::Vk {
* otherwise false.
*/
template <typename F>
State acquireImage(uint64_t timeout, const F& lambda) {
State acquireImage(const uint64_t timeout, const F& lambda) {
auto& imageAvailableSemaphore = imageAvailableSemaphores[currentFrame];

uint32_t imageIndex;
auto result = vkAcquireNextImageKHR(
const auto& result = vkAcquireNextImageKHR(
device->getDevice(),
swapchain,
timeout,
Expand All @@ -80,8 +80,7 @@ namespace Vixen::Vk {
currentFrame = (currentFrame + 1) % imageCount;

switch (result) {
using
enum State;
using enum State;

case VK_SUCCESS:
return OK;
Expand Down
4 changes: 0 additions & 4 deletions src/engine/vk/VkImage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,12 +120,8 @@ namespace Vixen::Vk {
cmd.begin(CommandBufferUsage::ONCE);

cmd.transitionImage(*this, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL);
layout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL;

cmd.copyBufferToImage(data, *this);

cmd.transitionImage(*this, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL);
layout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;

cmd.end();
cmd.submit(device->getTransferQueue(), {}, {}, {});
Expand Down
18 changes: 10 additions & 8 deletions src/engine/vk/VkRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,19 +33,19 @@ namespace Vixen::Vk {
}

void VkRenderer::render(
const VkMesh& mesh,
const std::vector<VkMesh>& meshes,
const std::vector<::VkDescriptorSet>& descriptorSets
) {
if (const auto state = swapchain->acquireImage(
std::numeric_limits<uint64_t>::max(),
[this, &mesh, &descriptorSets](
[this, &meshes, &descriptorSets](
const auto& currentFrame,
const auto& imageIndex,
const auto& imageAvailableSemaphore
) {
auto& commandBuffer = renderCommandBuffers[currentFrame];

prepare(imageIndex, commandBuffer, mesh, descriptorSets);
prepare(imageIndex, commandBuffer, meshes, descriptorSets);

std::vector<::VkSemaphore> signalSemaphores = {renderFinishedSemaphores[currentFrame].getSemaphore()};

Expand All @@ -68,7 +68,7 @@ namespace Vixen::Vk {
void VkRenderer::prepare(
const uint32_t imageIndex,
const VkCommandBuffer& commandBuffer,
const VkMesh& mesh,
const std::vector<VkMesh>& meshes,
const std::vector<::VkDescriptorSet>& descriptorSets
) const {
const auto& [width, height] = swapchain->getExtent();
Expand Down Expand Up @@ -98,8 +98,8 @@ namespace Vixen::Vk {
.layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
.loadStoreTarget = swapchain->getColorImageViews()[imageIndex].getImageView(),
.resolveTarget = nullptr,
.clearColor = {0.0f, 0.0f, 0.0f, 0.0f},
.clearDepth = 0.0f,
.clearColor = {0.0F, 0.0F, 0.0F, 0.0F},
.clearDepth = 0.0F,
.clearStencil = 0
}
},
Expand All @@ -122,7 +122,7 @@ namespace Vixen::Vk {
);
});

const Rectangle& rectangle{
const Rectangle rectangle{
.x = 0,
.y = 0,
.width = static_cast<float>(width),
Expand All @@ -131,7 +131,9 @@ namespace Vixen::Vk {
commandBuffer.setViewport(rectangle);
commandBuffer.setScissor(rectangle);

commandBuffer.drawMesh(mesh);
for (const auto& mesh : meshes) {
commandBuffer.drawMesh(mesh);
}

commandBuffer.endRenderPass();

Expand Down
4 changes: 2 additions & 2 deletions src/engine/vk/VkRenderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,15 @@ namespace Vixen::Vk {
~VkRenderer();

void render(
const VkMesh& mesh,
const std::vector<VkMesh>& meshes,
const std::vector<::VkDescriptorSet>& descriptorSets
);

private:
void prepare(
uint32_t imageIndex,
const VkCommandBuffer& commandBuffer,
const VkMesh& mesh,
const std::vector<VkMesh>& meshes,
const std::vector<::VkDescriptorSet>& descriptorSets
) const;
};
Expand Down
83 changes: 45 additions & 38 deletions src/engine/vk/test/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,40 +88,51 @@ int main() {
if (!scene)
throw std::runtime_error("Failed to load model from file");

const auto& aiMesh = scene->mMeshes[0];
const auto& hasColors = aiMesh->HasVertexColors(0);
const auto& hasUvs = aiMesh->HasTextureCoords(0);

std::vector<Vixen::Vk::Vertex> vertices(aiMesh->mNumVertices);
for (uint32_t i = 0; i < aiMesh->mNumVertices; i++) {
const auto& vertex = aiMesh->mVertices[i];
// TODO: Instead of storing default values for each vertex where a color or UV is missing, we should compact this down to save memory
const auto& color = hasColors ? aiMesh->mColors[0][i] : aiColor4D{1.0f, 1.0f, 1.0f, 1.0f};
const auto& uv = hasUvs ? aiMesh->mTextureCoords[0][i] : aiVector3D{1.0f, 1.0f, 1.0f};

vertices[i] = Vixen::Vk::Vertex{
.position = {vertex.x, vertex.y, vertex.z},
.color = {color.r, color.g, color.b, color.a},
.uv = {uv.x, uv.y}
};
}
std::vector<Vixen::Vk::VkMesh> meshes{};
meshes.reserve(scene->mNumMeshes);

for (auto i = 0; i < scene->mNumMeshes; i++) {
const auto& aiMesh = scene->mMeshes[i];
const auto& hasColors = aiMesh->HasVertexColors(0);
const auto& hasUvs = aiMesh->HasTextureCoords(0);

std::vector<Vixen::Vk::Vertex> vertices(aiMesh->mNumVertices);
for (uint32_t i = 0; i < aiMesh->mNumVertices; i++) {
const auto& vertex = aiMesh->mVertices[i];
// TODO: Instead of storing default values for each vertex where a color or UV is missing, we should compact this down to save memory
const auto& color = hasColors ? aiMesh->mColors[0][i] : aiColor4D{1.0F, 1.0F, 1.0F, 1.0F};
const auto& textureCoord = hasUvs ? aiMesh->mTextureCoords[0][i] : aiVector3D{1.0F, 1.0F, 1.0F};

vertices[i] = Vixen::Vk::Vertex{
.position = {vertex.x, vertex.y, vertex.z},
.color = {color.r, color.g, color.b, color.a},
.uv = {textureCoord.x, textureCoord.y}
};
}

std::vector<uint32_t> indices(aiMesh->mNumFaces * 3);
for (uint32_t i = 0; i < aiMesh->mNumFaces; i++) {
const auto& face = aiMesh->mFaces[i];
if (face.mNumIndices != 3) {
spdlog::warn("Skipping face with {} indices", face.mNumIndices);
continue;
std::vector<uint32_t> indices(aiMesh->mNumFaces * 3);
for (uint32_t i = 0; i < aiMesh->mNumFaces; i++) {
const auto& face = aiMesh->mFaces[i];
if (face.mNumIndices != 3) {
spdlog::warn("Skipping face with {} indices", face.mNumIndices);
continue;
}

indices[i * 3] = face.mIndices[0];
indices[i * 3 + 1] = face.mIndices[1];
indices[i * 3 + 2] = face.mIndices[2];
}

indices[i * 3] = face.mIndices[0];
indices[i * 3 + 1] = face.mIndices[1];
indices[i * 3 + 2] = face.mIndices[2];
meshes.emplace_back(vixen.getDevice());
meshes[i].setVertices(vertices);
meshes[i].setIndices(indices, Vixen::PrimitiveTopology::TRIANGLE_LIST);
}

aiString imagePath;
const auto& material = scene->mMaterials[aiMesh->mMaterialIndex];
assert(material != nullptr && "Material is nullptr");
const auto& material = scene->mMaterials[scene->mMeshes[0]->mMaterialIndex];
if (material == nullptr)
throw std::runtime_error("Material is nullptr");

material->GetTexture(aiTextureType_DIFFUSE, 0, &imagePath);
const auto& texture = scene->GetEmbeddedTexture(imagePath.C_Str());

Expand All @@ -144,20 +155,16 @@ int main() {
);
}

auto mesh = Vixen::Vk::VkMesh(vixen.getDevice());
mesh.setVertices(vertices);
mesh.setIndices(indices, Vixen::PrimitiveTopology::TRIANGLE_LIST);

auto camera = Vixen::Camera(glm::vec3{0.0f, 0.0f, 0.0f});

const std::vector<VkDescriptorPoolSize> sizes{
{
.type = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
.descriptorCount = 1
.descriptorCount = 256
},
{
.type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
.descriptorCount = 1
.descriptorCount = 256
}
};

Expand All @@ -169,12 +176,12 @@ int main() {
);

UniformBufferObject ubo{
glm::mat4(1.0f),
glm::mat4(1.0F),
camera.view(),
camera.perspective(static_cast<float>(width) / static_cast<float>(height))
};
ubo.model = translate(ubo.model, {0.0f, -0.5f, -1.5f});
ubo.model = rotate(ubo.model, glm::radians(225.0f), {0.0f, 1.0f, 0.0f});
ubo.model = translate(ubo.model, {0.0F, -0.5F, -1.5F});
ubo.model = rotate(ubo.model, glm::radians(225.0F), {0.0F, 1.0F, 0.0F});

auto descriptorPool = std::make_shared<Vixen::Vk::VkDescriptorPool>(vixen.getDevice(), sizes, 1);
auto mvp = Vixen::Vk::VkDescriptorSet(vixen.getDevice(), descriptorPool, *program.getDescriptorSetLayout());
Expand Down Expand Up @@ -207,7 +214,7 @@ int main() {
);
uniformBuffer.setData(reinterpret_cast<const std::byte*>(&ubo));

renderer->render(mesh, descriptorSets);
renderer->render(meshes, descriptorSets);

fps++;
if (now - old >= 1) {
Expand Down

0 comments on commit 1676cac

Please sign in to comment.