Skip to content

Commit

Permalink
compile Vulkan SPIR-V shaders in CMAKE_CURRENT_BINARY_DIR (#2055)
Browse files Browse the repository at this point in the history
fixes #2040 

* Compiles the Vulkan SPIR-V files in a directory based off of
CMAKE_CURRENT_BINARY_DIR.
* Changes the search path for the Vulkan SPIR-V files to this directory
rather than a semi-arbitrary set of directories.
  • Loading branch information
bashbaug authored Sep 16, 2024
1 parent 21b0a09 commit a395174
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 45 deletions.
35 changes: 14 additions & 21 deletions test_conformance/common/vulkan_wrapper/vulkan_utility.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ const VulkanPhysicalDevice &getVulkanPhysicalDevice()
cl_uint num_devices = 0;
cl_uint device_no = 0;
const size_t bufsize = BUFFERSIZE;
char buf[BUFFERSIZE];
const VulkanInstance &instance = getVulkanInstance();
const VulkanPhysicalDeviceList &physicalDeviceList =
instance.getPhysicalDeviceList();
Expand Down Expand Up @@ -753,43 +752,37 @@ std::ostream &operator<<(std::ostream &os, VulkanFormat format)
return os;
}

static char *findFilePath(const std::string filename)
static std::string findFilePath(const std::string &filename,
const std::string &startdir)
{
const char *searchPath[] = {
"./", // Same dir
"./shaders/", // In shaders folder in same dir
"../test_conformance/vulkan/shaders/" // In src folder
"/shaders/", // shaders directory, for most builds
"/../shaders/", // one directory up, for multi-config builds
};
for (unsigned int i = 0; i < sizeof(searchPath) / sizeof(char *); ++i)
{
std::string path(searchPath[i]);
std::string path(startdir);
path += searchPath[i];
path += filename;

path.append(filename);
FILE *fp;
fp = fopen(path.c_str(), "rb");

if (fp != NULL)
{
fclose(fp);
// File found
char *file_path = (char *)(malloc(path.length() + 1));
strncpy(file_path, path.c_str(), path.length() + 1);
return file_path;
}
if (fp)
{
fclose(fp);
return path;
}
}
// File not found
return 0;
return "";
}

std::vector<char> readFile(const std::string &filename)
std::vector<char> readFile(const std::string &filename,
const std::string &startdir = "")
{
char *file_path = findFilePath(filename);

std::ifstream file(file_path, std::ios::ate | std::ios::binary);
std::string filepath = findFilePath(filename, startdir);
std::ifstream file(filepath, std::ios::ate | std::ios::binary);

if (!file.is_open())
{
Expand All @@ -800,6 +793,6 @@ std::vector<char> readFile(const std::string &filename)
file.seekg(0);
file.read(buffer.data(), fileSize);
file.close();
printf("filesize is %d", fileSize);
printf("filesize is %zu\n", fileSize);
return buffer;
}
3 changes: 2 additions & 1 deletion test_conformance/common/vulkan_wrapper/vulkan_utility.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,5 +72,6 @@ operator<<(std::ostream& os,
VulkanExternalSemaphoreHandleType externalSemaphoreHandleType);
std::ostream& operator<<(std::ostream& os, VulkanFormat format);

std::vector<char> readFile(const std::string& filename);
std::vector<char> readFile(const std::string& filename,
const std::string& startdir);
#endif // _vulkan_utility_hpp_
12 changes: 0 additions & 12 deletions test_conformance/vulkan/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,8 @@ set (${MODULE_NAME}_SOURCES
test_vulkan_api_consistency_for_1dimages.cpp
test_vulkan_platform_device_info.cpp
vulkan_interop_common.cpp
../../test_common/harness/genericThread.cpp
../../test_common/harness/errorHelpers.cpp
../../test_common/harness/testHarness.cpp
../../test_common/harness/kernelHelpers.cpp
../../test_common/harness/mt19937.cpp
../../test_common/harness/msvc9.c
../../test_common/harness/parseParameters.cpp
../../test_common/harness/deviceInfo.cpp
../../test_common/harness/crc32.cpp
)

set_source_files_properties(
${${MODULE_NAME}_SOURCES}
PROPERTIES LANGUAGE CXX)
include_directories("../common/vulkan_wrapper")

add_subdirectory(shaders)
Expand Down
4 changes: 2 additions & 2 deletions test_conformance/vulkan/shaders/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ else()
string(REPLACE "GLSL_TYPE_PREFIX" "${GLSL_TYPE_PREFIX}" IMAGE2D_SHADER_CONTENT "${IMAGE2D_SHADER_CONTENT}")
file(WRITE ${IMAGE2D_SHADER_TMP_OUT_FILE} "${IMAGE2D_SHADER_CONTENT}")
execute_process(
COMMAND ${Vulkan_glslang_binary} --target-env vulkan1.0 -o image2D_${GLSL_FORMAT}.spv ${IMAGE2D_SHADER_TMP_OUT_FILE}
COMMAND ${Vulkan_glslang_binary} --target-env vulkan1.0 -o ${CMAKE_CURRENT_BINARY_DIR}/image2D_${GLSL_FORMAT}.spv ${IMAGE2D_SHADER_TMP_OUT_FILE}
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
RESULT_VARIABLE commandStatus
OUTPUT_QUIET)
Expand All @@ -34,7 +34,7 @@ else()
endif()
endforeach(IMAGE2D_FORMAT)
execute_process(
COMMAND ${Vulkan_glslang_binary} --target-env vulkan1.0 -o ${BUFFER_SHADER_IN_FILE}.spv ${BUFFER_SHADER_IN_FILE}.comp
COMMAND ${Vulkan_glslang_binary} --target-env vulkan1.0 -o ${CMAKE_CURRENT_BINARY_DIR}/${BUFFER_SHADER_IN_FILE}.spv ${BUFFER_SHADER_IN_FILE}.comp
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
RESULT_VARIABLE commandStatus
OUTPUT_QUIET)
Expand Down
10 changes: 6 additions & 4 deletions test_conformance/vulkan/test_vulkan_interop_buffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include <memory>
#include <string.h>
#include "harness/errorHelpers.h"
#include "harness/os_helpers.h"
#include "deviceInfo.h"

#define MAX_BUFFERS 5
Expand Down Expand Up @@ -115,7 +116,7 @@ int run_test_with_two_queue(

VulkanQueue &vkQueue = vkDevice.getQueue();

std::vector<char> vkBufferShader = readFile("buffer.spv");
std::vector<char> vkBufferShader = readFile("buffer.spv", exe_dir());

VulkanShaderModule vkBufferShaderModule(vkDevice, vkBufferShader);
VulkanDescriptorSetLayoutBindingList vkDescriptorSetLayoutBindingList;
Expand Down Expand Up @@ -447,7 +448,8 @@ int run_test_with_one_queue(

VulkanQueue &vkQueue = vkDevice.getQueue();

std::vector<char> vkBufferShader = readFile("buffer.spv");
std::vector<char> vkBufferShader = readFile("buffer.spv", exe_dir());

VulkanShaderModule vkBufferShaderModule(vkDevice, vkBufferShader);
VulkanDescriptorSetLayoutBindingList vkDescriptorSetLayoutBindingList;
vkDescriptorSetLayoutBindingList.addBinding(
Expand Down Expand Up @@ -749,7 +751,7 @@ int run_test_with_multi_import_same_ctx(

VulkanQueue &vkQueue = vkDevice.getQueue();

std::vector<char> vkBufferShader = readFile("buffer.spv");
std::vector<char> vkBufferShader = readFile("buffer.spv", exe_dir());

VulkanShaderModule vkBufferShaderModule(vkDevice, vkBufferShader);
VulkanDescriptorSetLayoutBindingList vkDescriptorSetLayoutBindingList;
Expand Down Expand Up @@ -1097,7 +1099,7 @@ int run_test_with_multi_import_diff_ctx(

VulkanQueue &vkQueue = vkDevice.getQueue();

std::vector<char> vkBufferShader = readFile("buffer.spv");
std::vector<char> vkBufferShader = readFile("buffer.spv", exe_dir());

VulkanShaderModule vkBufferShaderModule(vkDevice, vkBufferShader);
VulkanDescriptorSetLayoutBindingList vkDescriptorSetLayoutBindingList(
Expand Down
11 changes: 6 additions & 5 deletions test_conformance/vulkan/test_vulkan_interop_image.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include <vulkan_interop_common.hpp>
#include <string>
#include "harness/errorHelpers.h"
#include "harness/os_helpers.h"
#include <algorithm>
#include "deviceInfo.h"

Expand Down Expand Up @@ -272,8 +273,8 @@ int run_test_with_two_queue(

std::string fileName = "image2D_"
+ std::string(getVulkanFormatGLSLFormat(vkFormat)) + ".spv";
log_info("Load %s file", fileName.c_str());
vkImage2DShader = readFile(fileName);
log_info("Load file: %s\n", fileName.c_str());
vkImage2DShader = readFile(fileName, exe_dir());
VulkanShaderModule vkImage2DShaderModule(vkDevice, vkImage2DShader);

VulkanComputePipeline vkComputePipeline(vkDevice, vkPipelineLayout,
Expand Down Expand Up @@ -884,8 +885,8 @@ int run_test_with_one_queue(

std::string fileName = "image2D_"
+ std::string(getVulkanFormatGLSLFormat(vkFormat)) + ".spv";
log_info("Load %s file", fileName.c_str());
vkImage2DShader = readFile(fileName);
log_info("Load file: %s\n", fileName.c_str());
vkImage2DShader = readFile(fileName, exe_dir());
VulkanShaderModule vkImage2DShaderModule(vkDevice, vkImage2DShader);

VulkanComputePipeline vkComputePipeline(vkDevice, vkPipelineLayout,
Expand Down Expand Up @@ -1474,7 +1475,7 @@ int test_image_common(cl_device_id device_, cl_context context_,
err = setMaxImageDimensions(deviceId, max_width, max_height);
test_error_and_cleanup(err, CLEANUP, "error setting max image dimensions");

log_info("Set max_width to %lu and max_height to %lu\n", max_width,
log_info("Set max_width to %zu and max_height to %zu\n", max_width,
max_height);
context = clCreateContextFromType(contextProperties, CL_DEVICE_TYPE_GPU,
NULL, NULL, &err);
Expand Down

0 comments on commit a395174

Please sign in to comment.