Skip to content

Commit

Permalink
Vulkan 15/16 tutorial cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
emeiri committed Dec 7, 2024
1 parent 3120c1e commit 8f45754
Show file tree
Hide file tree
Showing 10 changed files with 528 additions and 15 deletions.
19 changes: 19 additions & 0 deletions Vulkan/Tutorial15/test.frag
Original file line number Diff line number Diff line change
@@ -1,3 +1,22 @@
/*
Copyright 2024 Etay Meiri
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/


#version 460

layout(location = 0) out vec4 out_Color;
Expand Down
15 changes: 2 additions & 13 deletions Vulkan/Tutorial15/test.vert
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,9 @@

#version 460

struct VertexData
{
float x, y, z;
float u, v;
};

layout (binding = 0) readonly buffer Vertices { VertexData data[]; } in_Vertices;
vec2 pos[3] = vec2[3]( vec2(-0.7, 0.7), vec2(0.7, 0.7), vec2(0.0, -0.7) );

void main()
{
VertexData vtx = in_Vertices.data[gl_VertexIndex];

vec3 pos = vec3(vtx.x, vtx.y, vtx.z);

gl_Position = vec4(pos, 1.0);
gl_Position = vec4( pos[gl_VertexIndex], 0.0, 1.0 );
}

4 changes: 2 additions & 2 deletions Vulkan/Tutorial15/tutorial15.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
along with this program. If not, see <http://www.gnu.org/licenses/>.
Vulkan For Beginners -
Tutorial #15: Vertex Buffer Part 1
Tutorial #15: Vertex Buffers
*/


Expand Down Expand Up @@ -137,7 +137,7 @@ class VulkanApp

void CreatePipeline()
{
m_pPipeline = new OgldevVK::GraphicsPipeline(m_device, m_pWindow, m_renderPass, m_vs, m_fs, &m_mesh, m_numImages);
m_pPipeline = new OgldevVK::GraphicsPipeline(m_device, m_pWindow, m_renderPass, m_vs, m_fs, NULL, m_numImages);
}


Expand Down
26 changes: 26 additions & 0 deletions Vulkan/Tutorial16/test.frag
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
Copyright 2024 Etay Meiri
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/


#version 460

layout(location = 0) out vec4 out_Color;

void main() {
out_Color = vec4( 0.0, 0.4, 1.0, 1.0 );
}
38 changes: 38 additions & 0 deletions Vulkan/Tutorial16/test.vert
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
Copyright 2024 Etay Meiri
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/


#version 460

struct VertexData
{
float x, y, z;
float u, v;
};

layout (binding = 0) readonly buffer Vertices { VertexData data[]; } in_Vertices;

void main()
{
VertexData vtx = in_Vertices.data[gl_VertexIndex];

vec3 pos = vec3(vtx.x, vtx.y, vtx.z);

gl_Position = vec4(pos, 1.0);
}

243 changes: 243 additions & 0 deletions Vulkan/Tutorial16/tutorial16.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,243 @@
/*
Copyright 2024 Etay Meiri
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
Vulkan For Beginners -
Tutorial #16: Descriptor Sets
*/


#include <stdio.h>
#include <stdlib.h>

#define GLFW_INCLUDE_VULKAN
#include <GLFW/glfw3.h>

#include <glm/glm.hpp>
#include <glm/ext.hpp>


#include "ogldev_vulkan_util.h"
#include "ogldev_vulkan_core.h"
#include "ogldev_vulkan_wrapper.h"
#include "ogldev_vulkan_shader.h"
#include "ogldev_vulkan_graphics_pipeline.h"
#include "ogldev_vulkan_simple_mesh.h"

#define WINDOW_WIDTH 1280
#define WINDOW_HEIGHT 720


void GLFW_KeyCallback(GLFWwindow* window, int key, int scancode, int action, int mods)
{
if ((key == GLFW_KEY_ESCAPE) && (action == GLFW_PRESS)) {
glfwSetWindowShouldClose(window, GLFW_TRUE);
}
}


class VulkanApp
{
public:

VulkanApp()
{
}

~VulkanApp()
{
m_vkCore.FreeCommandBuffers((u32)m_cmdBufs.size(), m_cmdBufs.data());
m_vkCore.DestroyFramebuffers(m_frameBuffers);
vkDestroyShaderModule(m_device, m_vs, NULL);
vkDestroyShaderModule(m_device, m_fs, NULL);
delete m_pPipeline;
vkDestroyRenderPass(m_device, m_renderPass, NULL);
m_mesh.Destroy(m_device);
}

void Init(const char* pAppName, GLFWwindow* pWindow)
{
m_pWindow = pWindow;
m_vkCore.Init(pAppName, pWindow);
m_device = m_vkCore.GetDevice();
m_numImages = m_vkCore.GetNumImages();
m_pQueue = m_vkCore.GetQueue();
m_renderPass = m_vkCore.CreateSimpleRenderPass();
m_frameBuffers = m_vkCore.CreateFramebuffer(m_renderPass);
CreateShaders();
CreateVertexBuffer();
CreatePipeline();
CreateCommandBuffers();
RecordCommandBuffers();
}

void RenderScene()
{
u32 ImageIndex = m_pQueue->AcquireNextImage();

m_pQueue->SubmitAsync(m_cmdBufs[ImageIndex]);

m_pQueue->Present(ImageIndex);
}

private:
void CreateCommandBuffers()
{
m_cmdBufs.resize(m_numImages);
m_vkCore.CreateCommandBuffers(m_numImages, m_cmdBufs.data());

printf("Created command buffers\n");
}


void CreateVertexBuffer()
{
struct Vertex {
Vertex(const glm::vec3& p, const glm::vec2& t)
{
Pos = p;
Tex = t;
}

glm::vec3 Pos;
glm::vec2 Tex;
};

std::vector<Vertex> Vertices = {
Vertex({-1.0f, -1.0f, 0.0f}, {0.0f, 0.0f}), // top left
Vertex({1.0f, -1.0f, 0.0f}, {0.0f, 1.0f}), // top right
Vertex({0.0f, 1.0f, 0.0f}, {1.0f, 1.0f}) // bottom middle
};

m_mesh.m_vertexBufferSize = sizeof(Vertices[0]) * Vertices.size();
m_mesh.m_vb = m_vkCore.CreateVertexBuffer(Vertices.data(), m_mesh.m_vertexBufferSize);
}


void CreateShaders()
{
m_vs = OgldevVK::CreateShaderModuleFromText(m_device, "test.vert");

m_fs = OgldevVK::CreateShaderModuleFromText(m_device, "test.frag");
}


void CreatePipeline()
{
m_pPipeline = new OgldevVK::GraphicsPipeline(m_device, m_pWindow, m_renderPass, m_vs, m_fs, &m_mesh, m_numImages);
}


void RecordCommandBuffers()
{
VkClearColorValue ClearColor = { 1.0f, 0.0f, 0.0f, 0.0f };
VkClearValue ClearValue;
ClearValue.color = ClearColor;

VkRenderPassBeginInfo RenderPassBeginInfo = {
.sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO,
.pNext = NULL,
.renderPass = m_renderPass,
.renderArea = {
.offset = {
.x = 0,
.y = 0
},
.extent = {
.width = WINDOW_WIDTH,
.height = WINDOW_HEIGHT
}
},
.clearValueCount = 1,
.pClearValues = &ClearValue
};

for (uint i = 0; i < m_cmdBufs.size(); i++) {
OgldevVK::BeginCommandBuffer(m_cmdBufs[i], VK_COMMAND_BUFFER_USAGE_SIMULTANEOUS_USE_BIT);

RenderPassBeginInfo.framebuffer = m_frameBuffers[i];

vkCmdBeginRenderPass(m_cmdBufs[i], &RenderPassBeginInfo, VK_SUBPASS_CONTENTS_INLINE);

m_pPipeline->Bind(m_cmdBufs[i], i);

u32 VertexCount = 3;
u32 InstanceCount = 1;
u32 FirstVertex = 0;
u32 FirstInstance = 0;

vkCmdDraw(m_cmdBufs[i], VertexCount, InstanceCount, FirstVertex, FirstInstance);

vkCmdEndRenderPass(m_cmdBufs[i]);

VkResult res = vkEndCommandBuffer(m_cmdBufs[i]);
CHECK_VK_RESULT(res, "vkEndCommandBuffer\n");
}

printf("Command buffers recorded\n");
}

GLFWwindow* m_pWindow = NULL;
OgldevVK::VulkanCore m_vkCore;
OgldevVK::VulkanQueue* m_pQueue = NULL;
VkDevice m_device = NULL;
int m_numImages = 0;
std::vector<VkCommandBuffer> m_cmdBufs;
VkRenderPass m_renderPass;
std::vector<VkFramebuffer> m_frameBuffers;
VkShaderModule m_vs;
VkShaderModule m_fs;
OgldevVK::GraphicsPipeline* m_pPipeline = NULL;
OgldevVK::SimpleMesh m_mesh;
};


#define APP_NAME "Tutorial 15"

int main(int argc, char* argv[])
{
if (!glfwInit()) {
return 1;
}

if (!glfwVulkanSupported()) {
return 1;
}

glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);

GLFWwindow* pWindow = glfwCreateWindow(WINDOW_WIDTH, WINDOW_HEIGHT, APP_NAME, NULL, NULL);

if (!pWindow) {
glfwTerminate();
exit(EXIT_FAILURE);
}

glfwSetKeyCallback(pWindow, GLFW_KeyCallback);

VulkanApp App;
App.Init(APP_NAME, pWindow);

while (!glfwWindowShouldClose(pWindow)) {
App.RenderScene();
glfwPollEvents();
}

glfwTerminate();

return 0;
}
Loading

0 comments on commit 8f45754

Please sign in to comment.