Skip to content

Commit

Permalink
ADD minimum pipeline setup:
Browse files Browse the repository at this point in the history
- grass texture
- shader files
- script for compiling shader
- pipeline setup
  • Loading branch information
SaumonDesMers authored and afaure42 committed Feb 9, 2024
1 parent a6884cc commit e1074d3
Show file tree
Hide file tree
Showing 12 changed files with 207 additions and 31 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,12 @@
*.out
*.app

# Compiled Shaders
*.spv

.vscode

latex
html
build
glslc
Binary file added assets/textures/grass.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 4 additions & 1 deletion build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@
# Download the necessary external libraries
./scripts/download_cppVulkanAPI.sh

# Compile shaders
./scripts/compile_shaders.sh

# create build directory
mkdir -p build

# build the project
cd build
cmake ..
make -j
make -j
13 changes: 13 additions & 0 deletions scripts/compile_shaders.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# https://github.com/google/shaderc/blob/main/downloads.md to download glslc

# /usr/local/bin/glslc simple_shader.vert -o simple_shader.vert.spv
# /usr/local/bin/glslc simple_shader.frag -o simple_shader.frag.spv

# ./glslc simple_shader.vert -o simple_shader.vert.spv
# ./glslc simple_shader.frag -o simple_shader.frag.spv

./external/glslc/glslc shaders/simple_shader.vert -o shaders/simple_shader.vert.spv
./external/glslc/glslc shaders/simple_shader.frag -o shaders/simple_shader.frag.spv

# glslang -V simple_shader.vert -o simple_shader.vert.spv
# glslang -V simple_shader.frag -o simple_shader.frag.spv
13 changes: 13 additions & 0 deletions shaders/simple_shader.frag
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#version 450

layout(set=1, binding = 0) uniform sampler2D texSampler;

layout(location = 0) in vec3 fragColor;
layout(location = 1) in vec2 fragTexCoord;

layout(location = 0) out vec4 outColor;

void main() {
// outColor = vec4(fragColor, 1.0);
outColor = texture(texSampler, fragTexCoord);
}
23 changes: 23 additions & 0 deletions shaders/simple_shader.vert
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#version 450

layout(set=0, binding = 0) uniform UniformBufferObject {
mat4 view;
mat4 proj;
} ubo;

layout(push_constant) uniform PushConstantObject {
mat4 model;
} pc;

layout(location = 0) in vec3 inPosition;
layout(location = 1) in vec3 inColor;
layout(location = 2) in vec2 inTexCoord;

layout(location = 0) out vec3 fragColor;
layout(location = 1) out vec2 fragTexCoord;

void main() {
gl_Position = ubo.proj * ubo.view * pc.model * vec4(inPosition, 1.0);
fragColor = inColor;
fragTexCoord = inTexCoord;
}
8 changes: 6 additions & 2 deletions src/app/application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,16 @@
#include <iostream>

Application::Application():
m_worldScene(),
m_world_scene(),
m_window("Vox", 800, 600),
m_renderAPI(m_window.getGLFWwindow()),
m_render_thread(m_renderAPI, m_worldScene)
m_render_thread(m_renderAPI, m_world_scene)
{
LOG_INFO("Application::Application()");

m_world_scene.camera().setPosition(glm::vec3(-2.0f, 0.5f, 0.5f));

m_world_scene.addMeshData(0, glm::mat4(1.0f));
}

Application::~Application()
Expand Down
2 changes: 1 addition & 1 deletion src/app/application.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class Application

private:

WorldScene m_worldScene;
WorldScene m_world_scene;
Window m_window;
vk::RenderAPI m_renderAPI;
RenderThread m_render_thread;
Expand Down
10 changes: 8 additions & 2 deletions src/app/scenes/WorldScene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ void WorldScene::removeMesh(uint32_t meshID)
});
}

std::vector<WorldScene::MeshRenderData> WorldScene::getMeshRenderData()
std::vector<WorldScene::MeshRenderData> WorldScene::getMeshRenderData() const
{
std::unique_lock<std::mutex> lock(m_mesh_render_data_mutex);
return m_mesh_render_data;
Expand Down Expand Up @@ -75,11 +75,17 @@ void WorldScene::Camera::moveDirection(float x_offset, float y_offset)
yaw += x_offset * sensitivity;
}

void WorldScene::Camera::setPosition(const glm::vec3& position)
{
std::lock_guard<std::mutex> lock(m_mutex);
this->position = position;
}

glm::vec3 WorldScene::Camera::direction() const
{
return glm::vec3(
cos(glm::radians(pitch)) * cos(glm::radians(yaw)),
sin(glm::radians(pitch)),
cos(glm::radians(pitch)) * sin(glm::radians(yaw))
);
}
}
49 changes: 28 additions & 21 deletions src/app/scenes/WorldScene.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
/**
* @brief Class to hold the world scene. The instance of this class will be
* accessed by multiple threads so it will include synchronization logic.
*
*
* @details The WorldScene class will hold the mesh data and the camera data.
*/
class WorldScene
Expand All @@ -27,7 +27,7 @@ class WorldScene

class Camera
{

public:

Camera() = default;
Expand All @@ -40,48 +40,55 @@ class WorldScene

/**
* @brief Get the view matrix of the camera.
*
*
* @return The view matrix.
*/
glm::mat4 getViewMatrix() const;

/**
* @brief Get the projection matrix of the camera.
*
* @param aspect_ratio
*
* @param aspect_ratio
* @return The projection matrix.
*/
glm::mat4 getProjectionMatrix(float aspect_ratio) const;

/**
* @brief Move the camera forward/backward on the xz plane.
*
* @param distance
*
* @param distance
*/
void moveForward(float distance);

/**
* @brief Move the camera right/left on the xz plane.
*
* @param distance
*
* @param distance
*/
void moveRight(float distance);

/**
* @brief Move the camera up/down on the y axis.
*
* @param distance
*
* @param distance
*/
void moveUp(float distance);

/**
* @brief Move the camera rotation from the cursor movement.
*
*
* @param x_offset x movement of the cursor.
* @param y_offset y movement of the cursor.
*/
void moveDirection(float x_offset, float y_offset);


/**
* @brief Set the position of the camera.
*
* @param position
*/
void setPosition(const glm::vec3& position);

private:

glm::vec3 position{ 0.0f, 0.0f, 0.0f };
Expand Down Expand Up @@ -111,45 +118,45 @@ class WorldScene

/**
* @brief Function to add a mesh to the scene.
*
*
* @param meshID The mesh ID.
* @param model The model matrix of the mesh.
*/
void addMeshData(uint32_t meshID, glm::mat4 model);

/**
* @brief Function to remove a mesh from the scene.
*
*
* @param meshID The mesh ID to remove.
*/
void removeMesh(uint32_t meshID);

/**
* @brief Function to get the mesh data.
*
*
* @return std::vector<MeshRenderData> The mesh data.
*/
std::vector<MeshRenderData> getMeshRenderData();
std::vector<MeshRenderData> getMeshRenderData() const;

/**
* @brief Function to get the camera.
*
*
* @return A reference to the camera.
*/
Camera& camera() { return m_camera; }

/**
* @brief Function const to get the camera.
*
*
* @return A const reference to the camera.
*
*
*/
const Camera& camera() const { return m_camera; }

private:

std::vector<MeshRenderData> m_mesh_render_data;
std::mutex m_mesh_render_data_mutex;
mutable std::mutex m_mesh_render_data_mutex;

Camera m_camera;
};
93 changes: 92 additions & 1 deletion src/app/threads/render/RenderThread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ RenderThread::RenderThread(
):
AThreadWrapper(),
m_renderAPI(renderAPI),
m_worldScene(worldScene)
m_world_scene(worldScene)
{
}

Expand All @@ -19,11 +19,102 @@ RenderThread::~RenderThread()

void RenderThread::init()
{
vk::Mesh::ID mesh_id = m_renderAPI.loadModel("assets/models/cube.obj");
LOG_DEBUG("Mesh ID: " << mesh_id);


vk::UniformBuffer::CreateInfo uniform_buffer_info{};
uniform_buffer_info.size = sizeof(ViewProj_UBO);
uniform_buffer_info.stageFlags = VK_SHADER_STAGE_VERTEX_BIT;

m_proj_view_ubo_id = m_renderAPI.createUniformBuffer(uniform_buffer_info);

vk::Texture::CreateInfo texture_info{};
texture_info.filepath = "assets/textures/grass.jpg";
texture_info.mipLevel = 1;
texture_info.stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT;

m_texture_id = m_renderAPI.loadTexture(texture_info);


vk::Pipeline::CreateInfo pipeline_create_info{};
pipeline_create_info.vertexShaderPath = "shaders/simple_shader.vert.spv";
pipeline_create_info.fragmentShaderPath = "shaders/simple_shader.frag.spv";
pipeline_create_info.descriptorSetLayouts = {
m_renderAPI.getUniformBuffer(m_proj_view_ubo_id)->descriptor()->layout(),
m_renderAPI.getTexture(m_texture_id)->descriptor()->layout()
};
pipeline_create_info.pushConstantRanges = {
{VK_SHADER_STAGE_VERTEX_BIT, 0, sizeof(ModelMatrix_push_constant)}
};

m_simple_shader_pipeline_id = m_renderAPI.createPipeline(pipeline_create_info);
}

void RenderThread::loop()
{
std::vector<WorldScene::MeshRenderData> mesh_render_data = m_world_scene.getMeshRenderData();

m_renderAPI.startDraw();
m_renderAPI.startRendering();

//############################################################################

int width, height;
glfwGetFramebufferSize(m_renderAPI.getWindow(), &width, &height);

ViewProj_UBO ubo{};
ubo.view = m_world_scene.camera().getViewMatrix();
ubo.proj = m_world_scene.camera().getProjectionMatrix(width / (float) height);
ubo.proj[1][1] *= -1;

m_renderAPI.getUniformBuffer(m_proj_view_ubo_id)->buffer(m_renderAPI.currentFrame())->write(&ubo, sizeof(ubo));

m_renderAPI.bindPipeline(m_simple_shader_pipeline_id);

m_renderAPI.bindDescriptor(
m_simple_shader_pipeline_id,
0, 1,
m_renderAPI.getUniformBuffer(m_proj_view_ubo_id)->descriptor()->pSet(m_renderAPI.currentFrame())
);

m_renderAPI.bindDescriptor(
m_simple_shader_pipeline_id,
1, 1,
m_renderAPI.getTexture(m_texture_id)->descriptor()->pSet(m_renderAPI.currentFrame())
);


VkViewport viewport{};
viewport.x = 0.0f;
viewport.y = 0.0f;
viewport.width = static_cast<float>(width);
viewport.height = static_cast<float>(height);
viewport.minDepth = 0.0f;
viewport.maxDepth = 1.0f;
m_renderAPI.setViewport(viewport);

VkRect2D scissor{};
scissor.offset = { 0, 0 };
scissor.extent = { static_cast<uint32_t>(width), static_cast<uint32_t>(height) };
m_renderAPI.setScissor(scissor);

for (auto& data : mesh_render_data)
{
ModelMatrix_push_constant pushConstant{};
pushConstant.model = data.model;
m_renderAPI.pushConstant(
m_simple_shader_pipeline_id,
VK_SHADER_STAGE_VERTEX_BIT,
sizeof(ModelMatrix_push_constant),
&pushConstant
);

m_renderAPI.drawMesh(data.id);
}

//############################################################################

m_renderAPI.endRendering();
m_renderAPI.endDraw();
}
Loading

0 comments on commit e1074d3

Please sign in to comment.