Load shaders and create shader module
This commit is contained in:
+37
-37
@@ -5,57 +5,57 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
struct VSInput {
|
struct VSInput {
|
||||||
float3 Pos;
|
float3 Pos;
|
||||||
float3 Normal;
|
float3 Normal;
|
||||||
float2 UV;
|
float2 UV;
|
||||||
};
|
};
|
||||||
|
|
||||||
Sampler2D textures[];
|
Sampler2D textures[];
|
||||||
|
|
||||||
struct ShaderData {
|
struct ShaderData {
|
||||||
float4x4 projection;
|
float4x4 projection;
|
||||||
float4x4 view;
|
float4x4 view;
|
||||||
float4x4 model[3];
|
float4x4 model[3];
|
||||||
float4 lightPos;
|
float4 lightPos;
|
||||||
uint32_t selected;
|
uint32_t selected;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct VSOutput {
|
struct VSOutput {
|
||||||
float4 Pos : SV_POSITION;
|
float4 Pos : SV_POSITION;
|
||||||
float3 Normal;
|
float3 Normal;
|
||||||
float2 UV;
|
float2 UV;
|
||||||
float3 Factor;
|
float3 Factor;
|
||||||
float3 LightVec;
|
float3 LightVec;
|
||||||
float3 ViewVec;
|
float3 ViewVec;
|
||||||
uint32_t InstanceIndex;
|
uint32_t InstanceIndex;
|
||||||
};
|
};
|
||||||
|
|
||||||
[shader("vertex")]
|
[shader("vertex")]
|
||||||
VSOutput main(VSInput input, uniform ShaderData *shaderData, uint instanceIndex : SV_VulkanInstanceID) {
|
VSOutput main(VSInput input, uniform ShaderData *shaderData, uint instanceIndex : SV_VulkanInstanceID) {
|
||||||
VSOutput output;
|
VSOutput output;
|
||||||
float4x4 modelMat = shaderData->model[instanceIndex];
|
float4x4 modelMat = shaderData->model[instanceIndex];
|
||||||
output.Normal = mul((float3x3)mul(shaderData->view, modelMat), input.Normal);
|
output.Normal = mul((float3x3)mul(shaderData->view, modelMat), input.Normal);
|
||||||
output.UV = input.UV;
|
output.UV = input.UV;
|
||||||
output.Pos = mul(shaderData->projection, mul(shaderData->view, mul(modelMat, float4(input.Pos.xyz, 1.0))));
|
output.Pos = mul(shaderData->projection, mul(shaderData->view, mul(modelMat, float4(input.Pos.xyz, 1.0))));
|
||||||
output.Factor = (shaderData->selected == instanceIndex ? 3.0f : 1.0f);
|
output.Factor = (shaderData->selected == instanceIndex ? 3.0f : 1.0f);
|
||||||
output.InstanceIndex = instanceIndex;
|
output.InstanceIndex = instanceIndex;
|
||||||
// Calculate view vectors required for lighting
|
// Calculate view vectors required for lighting
|
||||||
float4 fragPos = mul(mul(shaderData->view, modelMat), float4(input.Pos.xyz, 1.0));
|
float4 fragPos = mul(mul(shaderData->view, modelMat), float4(input.Pos.xyz, 1.0));
|
||||||
output.LightVec = shaderData->lightPos.xyz - fragPos.xyz;
|
output.LightVec = shaderData->lightPos.xyz - fragPos.xyz;
|
||||||
output.ViewVec = -fragPos.xyz;
|
output.ViewVec = -fragPos.xyz;
|
||||||
return output;
|
return output;
|
||||||
}
|
}
|
||||||
|
|
||||||
[shader("fragment")]
|
[shader("fragment")]
|
||||||
float4 main(VSOutput input) {
|
float4 main(VSOutput input) {
|
||||||
// Phong lighting
|
// Phong lighting
|
||||||
float3 N = normalize(input.Normal);
|
float3 N = normalize(input.Normal);
|
||||||
float3 L = normalize(input.LightVec);
|
float3 L = normalize(input.LightVec);
|
||||||
float3 V = normalize(input.ViewVec);
|
float3 V = normalize(input.ViewVec);
|
||||||
float3 R = reflect(-L, N);
|
float3 R = reflect(-L, N);
|
||||||
float3 diffuse = max(dot(N, L), 0.0025);
|
float3 diffuse = max(dot(N, L), 0.0025);
|
||||||
float3 specular = pow(max(dot(R, V), 0.0), 16.0) * 0.75;
|
float3 specular = pow(max(dot(R, V), 0.0), 16.0) * 0.75;
|
||||||
// Sample from texture
|
// Sample from texture
|
||||||
float3 color = textures[NonUniformResourceIndex(input.InstanceIndex)].Sample(input.UV).rgb * input.Factor;
|
float3 color = textures[NonUniformResourceIndex(input.InstanceIndex)].Sample(input.UV).rgb * input.Factor;
|
||||||
return float4(diffuse * color.rgb + specular, 1.0);
|
return float4(diffuse * color.rgb + specular, 1.0);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -18,7 +18,7 @@ build_ktx () {
|
|||||||
build_app () {
|
build_app () {
|
||||||
bear -- clang++ -g -c -Wno-nullability-completeness -DVK_NO_PROTOTYPES -I$VULKAN_SDK/include -Ivendor/include vulkan_profiles/vulkan_profiles.cpp main.cpp
|
bear -- clang++ -g -c -Wno-nullability-completeness -DVK_NO_PROTOTYPES -I$VULKAN_SDK/include -Ivendor/include vulkan_profiles/vulkan_profiles.cpp main.cpp
|
||||||
bear -a -- clang -g -c -DVK_NO_PROTOTYPES -Ivulkan_profiles -I$VULKAN_SDK/include $VULKAN_SDK/include/volk/volk.c wapp/wapp.c
|
bear -a -- clang -g -c -DVK_NO_PROTOTYPES -Ivulkan_profiles -I$VULKAN_SDK/include $VULKAN_SDK/include/volk/volk.c wapp/wapp.c
|
||||||
bear -a -- clang++ -g -DVK_NO_PROTOTYPES -Lvendor/lib -lSDL3 -lglm -ltinyobjloader -lktx -o main *.o
|
bear -a -- clang++ -g -DVK_NO_PROTOTYPES -L$VULKAN_SDK/lib -Lvendor/lib -lSDL3 -lglm -ltinyobjloader -lktx -lslang -o main *.o
|
||||||
}
|
}
|
||||||
|
|
||||||
clean_obj () {
|
clean_obj () {
|
||||||
|
|||||||
@@ -13,6 +13,8 @@
|
|||||||
#include <SDL3/SDL_keycode.h>
|
#include <SDL3/SDL_keycode.h>
|
||||||
#include <SDL3/SDL_video.h>
|
#include <SDL3/SDL_video.h>
|
||||||
#include <SDL3/SDL_vulkan.h>
|
#include <SDL3/SDL_vulkan.h>
|
||||||
|
#include <slang/slang.h>
|
||||||
|
#include <slang/slang-com-ptr.h>
|
||||||
#include <tiny_obj_loader.h>
|
#include <tiny_obj_loader.h>
|
||||||
#define VMA_IMPLEMENTATION
|
#define VMA_IMPLEMENTATION
|
||||||
#include <vma/vk_mem_alloc.h>
|
#include <vma/vk_mem_alloc.h>
|
||||||
@@ -87,6 +89,8 @@ typedef VkCommandBuffer *VkCommandBufferArray;
|
|||||||
typedef Texture *TextureArray;
|
typedef Texture *TextureArray;
|
||||||
typedef VkDescriptorImageInfo *VkDescriptorImageInfoArray;
|
typedef VkDescriptorImageInfo *VkDescriptorImageInfoArray;
|
||||||
typedef VkBufferImageCopy *VkBufferImageCopyArray;
|
typedef VkBufferImageCopy *VkBufferImageCopyArray;
|
||||||
|
typedef slang::TargetDesc *SlangTargetDescArray;
|
||||||
|
typedef slang::CompilerOptionEntry *SlangCompOptEntryArray;
|
||||||
// }}}
|
// }}}
|
||||||
|
|
||||||
// {{{ Helper Function Declarations
|
// {{{ Helper Function Declarations
|
||||||
@@ -138,6 +142,8 @@ wapp_intern VkDescriptorImageInfoArray tex_descriptors;
|
|||||||
wapp_intern VkDescriptorSetLayout desc_set_layout_tex = VK_NULL_HANDLE;
|
wapp_intern VkDescriptorSetLayout desc_set_layout_tex = VK_NULL_HANDLE;
|
||||||
wapp_intern VkDescriptorPool desc_pool = VK_NULL_HANDLE;
|
wapp_intern VkDescriptorPool desc_pool = VK_NULL_HANDLE;
|
||||||
wapp_intern VkDescriptorSet desc_set_tex = VK_NULL_HANDLE;
|
wapp_intern VkDescriptorSet desc_set_tex = VK_NULL_HANDLE;
|
||||||
|
wapp_intern Slang::ComPtr<slang::IGlobalSession> slang_global_session;
|
||||||
|
wapp_intern VkShaderModule shader_module = VK_NULL_HANDLE;
|
||||||
// }}}
|
// }}}
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
@@ -900,6 +906,60 @@ int main() {
|
|||||||
|
|
||||||
wapp_mem_arena_allocator_temp_end(&arena);
|
wapp_mem_arena_allocator_temp_end(&arena);
|
||||||
|
|
||||||
|
// {{{ Shaders
|
||||||
|
// {{{ Runtime Compile Shaders
|
||||||
|
// {{{ Initialise Slang Session
|
||||||
|
// Global session: the connection between the application and the Slang library
|
||||||
|
slang::createGlobalSession(slang_global_session.writeRef());
|
||||||
|
|
||||||
|
// {{{ Define Compilation Scope
|
||||||
|
slang::TargetDesc target = {};
|
||||||
|
target.format = SLANG_SPIRV;
|
||||||
|
target.profile = {slang_global_session->findProfile("spirv_1_4")};
|
||||||
|
|
||||||
|
SlangTargetDescArray slang_targets = wapp_array_with_capacity(slang::TargetDesc, 8, ARRAY_INIT_NONE);
|
||||||
|
wapp_array_append_capped(slang::TargetDesc, slang_targets, &target);
|
||||||
|
|
||||||
|
slang::CompilerOptionEntry entry = {};
|
||||||
|
entry.name = slang::CompilerOptionName::EmitSpirvDirectly;
|
||||||
|
entry.value = {slang::CompilerOptionValueKind::Int, 1};
|
||||||
|
|
||||||
|
SlangCompOptEntryArray slang_options = wapp_array_with_capacity(slang::CompilerOptionEntry, 8, ARRAY_INIT_NONE);
|
||||||
|
wapp_array_append_capped(slang::CompilerOptionEntry, slang_options, &entry);
|
||||||
|
// }}}
|
||||||
|
|
||||||
|
// {{{ Create Session
|
||||||
|
slang::SessionDesc slang_session_desc = {};
|
||||||
|
slang_session_desc.targets = slang_targets;
|
||||||
|
slang_session_desc.targetCount = wapp_array_count(slang_targets);
|
||||||
|
slang_session_desc.defaultMatrixLayoutMode = SLANG_MATRIX_LAYOUT_COLUMN_MAJOR;
|
||||||
|
slang_session_desc.compilerOptionEntries = slang_options;
|
||||||
|
slang_session_desc.compilerOptionEntryCount = wapp_array_count(slang_options);
|
||||||
|
|
||||||
|
Slang::ComPtr<slang::ISession> slang_session;
|
||||||
|
slang_global_session->createSession(slang_session_desc, slang_session.writeRef());
|
||||||
|
// }}}
|
||||||
|
// }}}
|
||||||
|
|
||||||
|
// {{{ Load Shader Code
|
||||||
|
Slang::ComPtr<slang::IModule> slang_module {
|
||||||
|
slang_session->loadModuleFromSource("triangle", "assets/shader.slang", nullptr, nullptr),
|
||||||
|
};
|
||||||
|
|
||||||
|
Slang::ComPtr<slang::IBlob> spirv;
|
||||||
|
slang_module->getTargetCode(0, spirv.writeRef());
|
||||||
|
// }}}
|
||||||
|
// }}}
|
||||||
|
|
||||||
|
// {{{ Create Shader Module
|
||||||
|
VkShaderModuleCreateInfo module_create_info = {};
|
||||||
|
module_create_info.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
|
||||||
|
module_create_info.codeSize = spirv->getBufferSize();
|
||||||
|
module_create_info.pCode = (u32 *)spirv->getBufferPointer();
|
||||||
|
check(vkCreateShaderModule(device, &module_create_info, NULL, &shader_module));
|
||||||
|
// }}}
|
||||||
|
// }}}
|
||||||
|
|
||||||
// {{{ Render Loop
|
// {{{ Render Loop
|
||||||
SDL_Event event = {};
|
SDL_Event event = {};
|
||||||
while (running) {
|
while (running) {
|
||||||
@@ -919,6 +979,7 @@ int main() {
|
|||||||
// }}}
|
// }}}
|
||||||
|
|
||||||
// {{{ Cleanup
|
// {{{ Cleanup
|
||||||
|
vkDestroyShaderModule(device, shader_module, NULL);
|
||||||
vkDestroyDescriptorPool(device, desc_pool, NULL);
|
vkDestroyDescriptorPool(device, desc_pool, NULL);
|
||||||
vkDestroyDescriptorSetLayout(device, desc_set_layout_tex, NULL);
|
vkDestroyDescriptorSetLayout(device, desc_set_layout_tex, NULL);
|
||||||
for (u32 i = 0; i < wapp_array_count(textures); ++i) {
|
for (u32 i = 0; i < wapp_array_count(textures); ++i) {
|
||||||
|
|||||||
Reference in New Issue
Block a user