Skip to content
This repository has been archived by the owner on Nov 6, 2024. It is now read-only.

Commit

Permalink
Uniforms example
Browse files Browse the repository at this point in the history
  • Loading branch information
krisvers committed Feb 24, 2024
1 parent 51026dd commit 0f67df3
Show file tree
Hide file tree
Showing 13 changed files with 287 additions and 24 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ Debug/
Release/
*.vcxproj
*.vcxproj.*
*.o
obj/
build/
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ LINUX_LIBS = -lglfw -lvulkan
linux-vulkan-examples: linux-vulkan-static
clang -c src/main.c $(shell find src -type f -name "example*.c") src/kgfx_gh/kgfx_gh_xlib.c $(FLAGS) $(EXTRA)
mv *.o build/
clang build/*.o $(LINUX_LIBS) $(EXTRA) -Lbuild -l:libkgfx.a -lm -o kgfx-examples
clang++ build/*.o $(LINUX_LIBS) $(EXTRA) -Lbuild -l:libkgfx.a -lm -o kgfx-examples

linux-vulkan-static: clean
clang++ -static -c src/kgfx/kgfx_vulkan.cpp $(FLAGS) $(LINUX_LIBS) $(EXTRA) -o obj/kgfx_vulkan.o
Expand Down
7 changes: 0 additions & 7 deletions assets/testf.glsl

This file was deleted.

Binary file removed assets/testf.spv
Binary file not shown.
7 changes: 0 additions & 7 deletions assets/testv.glsl

This file was deleted.

Binary file removed assets/testv.spv
Binary file not shown.
Binary file modified build/libkgfx.a
Binary file not shown.
8 changes: 4 additions & 4 deletions include/kgfx/kgfx.h
Original file line number Diff line number Diff line change
Expand Up @@ -115,10 +115,10 @@ typedef enum {
} KGFXinputrate;

typedef enum {
KGFX_BINDPOINT_VERTEX = 0,
KGFX_BINDPOINT_FRAGMENT = 1,
KGFX_BINDPOINT_GEOMETRY = 2,
KGFX_BINDPOINT_COMPUTE = 3,
KGFX_BINDPOINT_VERTEX = 1,
KGFX_BINDPOINT_FRAGMENT = 16,
KGFX_BINDPOINT_GEOMETRY = 4,
KGFX_BINDPOINT_COMPUTE = 8,
KGFX_BINDPOINT_COUNT,
KGFX_BINDPOINT_MAX = KGFX_BINDPOINT_COMPUTE,
KGFX_BINDPOINT_MIN = KGFX_BINDPOINT_VERTEX,
Expand Down
Binary file added kgfx-examples
Binary file not shown.
247 changes: 247 additions & 0 deletions src/example_uniforms.c

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions src/examples.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@

int example_current();
int example_triangle();
int example_uniforms();

#endif
33 changes: 30 additions & 3 deletions src/kgfx/kgfx_vulkan.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,24 @@ constexpr VkDescriptorType descriptorUsageVkUsage(KGFXdescriptorusage usage) {
return VK_DESCRIPTOR_TYPE_MAX_ENUM;
}

const VkShaderStageFlags bindpointVkShaderStageFlags(KGFXbindpoint bindpoint) {
VkShaderStageFlags flags = 0;
if (bindpoint & KGFX_BINDPOINT_VERTEX) {
flags |= VK_SHADER_STAGE_VERTEX_BIT;
}
if (bindpoint & KGFX_BINDPOINT_FRAGMENT) {
flags |= VK_SHADER_STAGE_FRAGMENT_BIT;
}
if (bindpoint & KGFX_BINDPOINT_GEOMETRY) {
flags |= VK_SHADER_STAGE_GEOMETRY_BIT;
}
if (bindpoint & KGFX_BINDPOINT_COMPUTE) {
flags |= VK_SHADER_STAGE_COMPUTE_BIT;
}

return flags;
}

constexpr VkMemoryPropertyFlags bufferLocationVkMemoryPropertyFlags(KGFXbufferlocation location) {
switch (location) {
case KGFX_BUFFER_LOCATION_CPU:
Expand Down Expand Up @@ -997,7 +1015,7 @@ void Vulkan::render(KGFXpipeline pipeline) {
return;
}
res = recreateSwapchain();
if (res != VK_SUCCESS) {
if (res != VK_SUCCESS && res != VK_SUBOPTIMAL_KHR) {
DEBUG_OUT("Failed to recreate swapchain");
}
return;
Expand Down Expand Up @@ -1127,8 +1145,12 @@ void Vulkan::render(KGFXpipeline pipeline) {

res = vkQueuePresentKHR(presentQueue, &presentInfo);
if (res == VK_ERROR_OUT_OF_DATE_KHR || res == VK_SUBOPTIMAL_KHR) {
extent = getWindowExtent();
if (extent.width == 0 || extent.height == 0 || extent.width < surfaceCapabilities.minImageExtent.width || extent.height < surfaceCapabilities.minImageExtent.height || extent.width > surfaceCapabilities.maxImageExtent.width || extent.height > surfaceCapabilities.maxImageExtent.height) {
return;
}
res = recreateSwapchain();
if (res != VK_SUCCESS) {
if (res != VK_SUCCESS || res != VK_SUBOPTIMAL_KHR) {
DEBUG_OUT("Failed to recreate swapchain");
}
return;
Expand Down Expand Up @@ -1489,7 +1511,7 @@ KGFXpipeline Vulkan::createPipeline(KGFXpipelinedesc pipelineDesc) {
descriptorSetLayoutBindings[i].binding = pipelineDesc.layout.pDescriptorSets[i].binding;
descriptorSetLayoutBindings[i].descriptorType = descriptorUsageVkUsage(pipelineDesc.layout.pDescriptorSets[i].usage);
descriptorSetLayoutBindings[i].descriptorCount = 1;
descriptorSetLayoutBindings[i].stageFlags = stageFlags[pipelineDesc.layout.pDescriptorSets[i].bindpoint];
descriptorSetLayoutBindings[i].stageFlags = bindpointVkShaderStageFlags(pipelineDesc.layout.pDescriptorSets[i].bindpoint);
descriptorSetLayoutBindings[i].pImmutableSamplers = nullptr;
}

Expand Down Expand Up @@ -1807,6 +1829,11 @@ VkExtent2D Vulkan::getWindowExtent() {
}

VkResult Vulkan::recreateSwapchain() {
VkExtent2D extent = getWindowExtent();
if (extent.width == 0 || extent.height == 0) {
return VK_SUBOPTIMAL_KHR;
}

vkDestroySemaphore(device, imageAvailableSemaphore, nullptr);

VkSemaphoreCreateInfo semaphoreCreateInfo = {};
Expand Down
3 changes: 2 additions & 1 deletion src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#include <stdlib.h>
#include "examples.h"

#define EXAMPLE_COUNT 2
#define EXAMPLE_COUNT 3
#define FLAG_COUNT 3

typedef int (*example_func)(void);
Expand All @@ -15,6 +15,7 @@ struct example_entry {
struct example_entry examples[EXAMPLE_COUNT] = {
{ .func = example_current, .name = "current" },
{ .func = example_triangle, .name = "triangle" },
{ .func = example_uniforms, .name = "uniforms" },
};

typedef void (*flag_func)(void);
Expand Down

0 comments on commit 0f67df3

Please sign in to comment.