From 4e68da29ebc45a41845d7127979878930b4c170b Mon Sep 17 00:00:00 2001 From: Awais Belal Date: Mon, 5 Sep 2016 15:47:16 +0500 Subject: [PATCH 1/2] demos: make shader location relative The demo binaries expect the shader (frag/vert.spv) location to be PWD so a user has to cd to /usr/bin if the binaries are installed there in order to run them correctly. This patch tries to find the location of the binary and then assumes that the shaders are located in the same location as the binary so a user can install everything to a single dir and that will work. Signed-off-by: Awais Belal --- demos/cube.c | 64 ++++++++++++++++++++++++++++++++++++++++++++++++++++++------ demos/tri.c | 49 ++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 105 insertions(+), 8 deletions(-) diff --git a/demos/cube.c b/demos/cube.c index 6017444..f79bc59 100644 --- a/demos/cube.c +++ b/demos/cube.c @@ -29,6 +29,7 @@ #include #include #include +#include #if defined(VK_USE_PLATFORM_XLIB_KHR) || defined(VK_USE_PLATFORM_XCB_KHR) #include #endif @@ -415,6 +416,8 @@ struct demo { uint32_t current_buffer; uint32_t queue_count; + + char bin_path[255]; }; VKAPI_ATTR VkBool32 VKAPI_CALL @@ -1206,18 +1209,25 @@ static void demo_prepare_textures(struct demo *demo) { const VkFormat tex_format = VK_FORMAT_R8G8B8A8_UNORM; VkFormatProperties props; uint32_t i; + char tex_file[255]; vkGetPhysicalDeviceFormatProperties(demo->gpu, tex_format, &props); for (i = 0; i < DEMO_TEXTURE_COUNT; i++) { VkResult U_ASSERT_ONLY err; - + if (strlen(demo->bin_path) > 0) { + strcpy(tex_file, demo->bin_path); + strcat(tex_file, "/"); + strcat(tex_file, tex_files[i]); + } + else + strcpy(tex_file, tex_files[i]); if ((props.linearTilingFeatures & VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT) && !demo->use_staging_buffer) { /* Device can texture using linear textures */ demo_prepare_texture_image( - demo, tex_files[i], &demo->textures[i], VK_IMAGE_TILING_LINEAR, + demo, tex_file, &demo->textures[i], VK_IMAGE_TILING_LINEAR, VK_IMAGE_USAGE_SAMPLED_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT); @@ -1228,13 +1238,13 @@ static void demo_prepare_textures(struct demo *demo) { memset(&staging_texture, 0, sizeof(staging_texture)); demo_prepare_texture_image( - demo, tex_files[i], &staging_texture, VK_IMAGE_TILING_LINEAR, + demo, tex_file, &staging_texture, VK_IMAGE_TILING_LINEAR, VK_IMAGE_USAGE_TRANSFER_SRC_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT); demo_prepare_texture_image( - demo, tex_files[i], &demo->textures[i], VK_IMAGE_TILING_OPTIMAL, + demo, tex_file, &demo->textures[i], VK_IMAGE_TILING_OPTIMAL, (VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_SAMPLED_BIT), VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT); @@ -1557,8 +1567,16 @@ static VkShaderModule demo_prepare_vs(struct demo *demo) { #else void *vertShaderCode; size_t size; + char fname[255]; + if (strlen(demo->bin_path) > 0) { + strcpy(fname, demo->bin_path); + strcat(fname, "/"); + strcat(fname, "cube-vert.spv"); + } + else + strcpy(fname, "cube-vert.spv"); - vertShaderCode = demo_read_spv("cube-vert.spv", &size); + vertShaderCode = demo_read_spv(fname, &size); demo->vert_shader_module = demo_prepare_shader_module(demo, vertShaderCode, size); @@ -1582,8 +1600,16 @@ static VkShaderModule demo_prepare_fs(struct demo *demo) { #else void *fragShaderCode; size_t size; + char fname[255]; + if (strlen(demo->bin_path) > 0) { + strcpy(fname, demo->bin_path); + strcat(fname, "/"); + strcat(fname, "cube-frag.spv"); + } + else + strcpy(fname, "cube-frag.spv"); - fragShaderCode = demo_read_spv("cube-frag.spv", &size); + fragShaderCode = demo_read_spv(fname, &size); demo->frag_shader_module = demo_prepare_shader_module(demo, fragShaderCode, size); @@ -3034,6 +3060,30 @@ static void demo_init_connection(struct demo *demo) { #endif } +static void find_bin_path(char *in_arg, char *ret_path) { + char *ptr = getenv("PATH"); + char *pch_temp; + char pch[255]; + + if (access(in_arg, F_OK ) == 0) { + pch_temp = strrchr(in_arg, '/'); + strncpy(ret_path, in_arg, strlen(in_arg) - strlen(pch_temp)); + } else if (in_arg[0] != '/') { + pch_temp = strtok(ptr, ":"); + while (pch_temp != NULL) { + strcpy(pch, pch_temp); + strcat(pch, "/"); + strcat(pch, in_arg); + if ((access(pch, F_OK ) == 0)) { + strcpy(ret_path, pch_temp); + break; + } + else + pch_temp = strtok(NULL, ":"); + } + } +} + static void demo_init(struct demo *demo, int argc, char **argv) { vec3 eye = {0.0f, 3.0f, 5.0f}; vec3 origin = {0, 0, 0}; @@ -3053,6 +3053,8 @@ static void demo_init(struct demo *demo, int argc, char **argv) { mat4x4_identity(demo->model_matrix); demo->projection_matrix[1][1]*=-1; //Flip projection matrix from GL to Vulkan orientation. + + find_bin_path(argv[0], demo->bin_path); } #if defined(VK_USE_PLATFORM_WIN32_KHR) diff --git a/demos/tri.c b/demos/tri.c index 35d33f2..77ee5a1 100644 --- a/demos/tri.c +++ b/demos/tri.c @@ -39,6 +39,7 @@ #include #include #include +#include #ifdef _WIN32 #pragma comment(linker, "/subsystem:windows") @@ -262,6 +263,8 @@ struct demo { bool quit; uint32_t current_buffer; uint32_t queue_count; + + char bin_path[255]; }; VKAPI_ATTR VkBool32 VKAPI_CALL @@ -1288,8 +1291,16 @@ static VkShaderModule demo_prepare_vs(struct demo *demo) { #else void *vertShaderCode; size_t size = 0; + char fname[255]; + if (strlen(demo->bin_path) > 0) { + strcpy(fname, demo->bin_path); + strcat(fname, "/"); + strcat(fname, "tri-vert.spv"); + } + else + strcpy(fname, "tri-vert.spv"); - vertShaderCode = demo_read_spv("tri-vert.spv", &size); + vertShaderCode = demo_read_spv(fname, &size); demo->vert_shader_module = demo_prepare_shader_module(demo, vertShaderCode, size); @@ -1313,8 +1324,16 @@ static VkShaderModule demo_prepare_fs(struct demo *demo) { #else void *fragShaderCode; size_t size; + char fname[255]; + if (strlen(demo->bin_path) > 0) { + strcpy(fname, demo->bin_path); + strcat(fname, "/"); + strcat(fname, "tri-frag.spv"); + } + else + strcpy(fname, "tri-frag.spv"); - fragShaderCode = demo_read_spv("tri-frag.spv", &size); + fragShaderCode = demo_read_spv(fname, &size); demo->frag_shader_module = demo_prepare_shader_module(demo, fragShaderCode, size); @@ -2447,6 +2466,30 @@ static void demo_init_connection(struct demo *demo) { #endif // _WIN32 } +static void find_bin_path(const char *in_arg, char *ret_path) { + char *ptr = getenv("PATH"); + char *pch_temp; + char pch[255]; + + if (access(in_arg, F_OK ) == 0) { + pch_temp = strrchr(in_arg, '/'); + strncpy(ret_path, in_arg, strlen(in_arg) - strlen(pch_temp)); + } else if (in_arg[0] != '/') { + pch_temp = strtok(ptr, ":"); + while (pch_temp != NULL) { + strcpy(pch, pch_temp); + strcat(pch, "/"); + strcat(pch, in_arg); + if ((access(pch, F_OK ) == 0)) { + strcpy(ret_path, pch_temp); + break; + } + else + pch_temp = strtok(NULL, ":"); + } + } +} + static void demo_init(struct demo *demo, const int argc, const char *argv[]) { memset(demo, 0, sizeof(*demo)); @@ -2490,6 +2533,8 @@ static void demo_init(struct demo *demo, const int argc, const char *argv[]) demo->height = 300; demo->depthStencil = 1.0; demo->depthIncrement = -0.01f; + + find_bin_path(argv[0], demo->bin_path); } static void demo_cleanup(struct demo *demo) { -- 1.9.1