Skip to content

Commit

Permalink
Build Triangle for all supported platforms
Browse files Browse the repository at this point in the history
  • Loading branch information
andrejnau committed Jan 22, 2024
1 parent f6b232b commit b56a775
Show file tree
Hide file tree
Showing 15 changed files with 110 additions and 49 deletions.
16 changes: 11 additions & 5 deletions cmake/compile_shaders.cmake
Original file line number Diff line number Diff line change
@@ -1,26 +1,32 @@
function(compile_shaders target shaders output)
function(compile_shaders target base_dir output_subdir shaders output_var)
if (CMAKE_CROSSCOMPILING AND IOS_OR_TVOS)
set(shader_compiler_cli "${CMAKE_BINARY_DIR}/macOS/bin/ShaderCompilerCLI")
else()
set(shader_compiler_cli "$<TARGET_FILE:ShaderCompilerCLI>")
endif()
set(output_dir "${CMAKE_BINARY_DIR}/gen/${target}/")
set(output_dir "${CMAKE_BINARY_DIR}/bin/${output_subdir}/")
foreach(full_shader_path ${shaders})
get_filename_component(shader_name ${full_shader_path} NAME_WE)
cmake_path(RELATIVE_PATH full_shader_path BASE_DIRECTORY "${base_dir}" OUTPUT_VARIABLE shader_name)
get_filename_component(shader_folder ${shader_name} DIRECTORY)
set(spirv ${output_dir}/${shader_name}.spirv)
set(dxil ${output_dir}/${shader_name}.dxil)
get_property(entrypoint SOURCE ${full_shader_path} PROPERTY SHADER_ENTRYPOINT)
get_property(type SOURCE ${full_shader_path} PROPERTY SHADER_TYPE)
get_property(model SOURCE ${full_shader_path} PROPERTY SHADER_MODEL)
add_custom_command(OUTPUT ${spirv} ${dxil}
COMMAND ${CMAKE_COMMAND} -E echo ${shader_name} ${full_shader_path} ${entrypoint} ${type} ${model} ${output_dir}
COMMAND ${CMAKE_COMMAND} -E make_directory ${output_dir}
COMMAND ${CMAKE_COMMAND} -E make_directory ${output_dir}/${shader_folder}
COMMAND ${shader_compiler_cli} ${shader_name} ${full_shader_path} ${entrypoint} ${type} ${model} ${output_dir}
DEPENDS ShaderCompilerCLI
MAIN_DEPENDENCY "${full_shader_path}"
)
set_source_files_properties(${spirv} ${dxil} PROPERTIES
MACOSX_PACKAGE_LOCATION "Resources/${output_subdir}/${shader_folder}"
)
source_group("Shader Files" FILES "${full_shader_path}")
source_group("Shader Blobs" FILES ${spirv} ${dxil})
set(compiled_shaders ${compiled_shaders} ${spirv})
set(compiled_shaders ${compiled_shaders} ${dxil})
endforeach()
set(${output} ${compiled_shaders} PARENT_SCOPE)
set(${output_var} ${compiled_shaders} PARENT_SCOPE)
endfunction()
4 changes: 1 addition & 3 deletions src/Apps/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,4 @@ if (NOT IOS_OR_TVOS)
add_subdirectory(CoreTriangle)
endif()

if (APPLE)
add_subdirectory(Triangle)
endif()
add_subdirectory(Triangle)
17 changes: 10 additions & 7 deletions src/Apps/Triangle/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
list(APPEND sources
main.mm
main.cpp
)

set(shaders_path "${assets_path}/shaders/CoreTriangle")
Expand All @@ -19,17 +19,20 @@ set_property(SOURCE ${pixel_shaders} PROPERTY SHADER_TYPE Pixel)
set(shaders_files ${pixel_shaders} ${vertex_shaders})

include(compile_shaders)
compile_shaders(Triangle "${shaders_files}" resources)
compile_shaders(Triangle "${shaders_path}" "asserts/Triangle" "${shaders_files}" resources)

set_source_files_properties(${resources} PROPERTIES
MACOSX_PACKAGE_LOCATION Resources
)

add_executable(Triangle MACOSX_BUNDLE
add_executable(Triangle WIN32 MACOSX_BUNDLE
${resources}
${shaders_files}
${sources}
)

if (WIN32)
set_target_properties(CoreTriangle PROPERTIES
LINK_FLAGS "/ENTRY:mainCRTStartup"
)
endif()

target_link_libraries(Triangle
AppLoop
AppSettings
Expand Down
19 changes: 11 additions & 8 deletions src/Apps/Triangle/main.mm → src/Apps/Triangle/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
#include "Instance/Instance.h"
#include "Utilities/Common.h"

#import <Foundation/Foundation.h>

class TriangleRenderer : public AppRenderer {
public:
TriangleRenderer(const Settings& settings);
Expand Down Expand Up @@ -82,12 +80,17 @@
constant_buffer->CommitMemory(MemoryType::kUpload);
constant_buffer->UpdateUploadBuffer(0, &constant_data, sizeof(constant_data));

auto vertex_path = [[NSBundle mainBundle] pathForResource:@"VertexShader" ofType:@"spirv"];
auto pixel_path = [[NSBundle mainBundle] pathForResource:@"PixelShader" ofType:@"spirv"];
vertex_shader =
device->CreateShader(ReadBinaryFile([vertex_path UTF8String]), ShaderBlobType::kSPIRV, ShaderType::kVertex);
pixel_shader =
device->CreateShader(ReadBinaryFile([pixel_path UTF8String]), ShaderBlobType::kSPIRV, ShaderType::kPixel);
ShaderBlobType blob_type = device->GetSupportedShaderBlobType();
std::string shader_blob_ext;
if (blob_type == ShaderBlobType::kDXIL) {
shader_blob_ext = ".dxil";
} else {
shader_blob_ext = ".spirv";
}
std::string vertex_path = GetAssertPath("asserts/Triangle/VertexShader.hlsl" + shader_blob_ext);
std::string pixel_path = GetAssertPath("asserts/Triangle/PixelShader.hlsl" + shader_blob_ext);
vertex_shader = device->CreateShader(ReadBinaryFile(vertex_path), ShaderBlobType::kSPIRV, ShaderType::kVertex);
pixel_shader = device->CreateShader(ReadBinaryFile(pixel_path), ShaderBlobType::kSPIRV, ShaderType::kPixel);
program = device->CreateProgram({ vertex_shader, pixel_shader });

ViewDesc constant_view_desc = {};
Expand Down
6 changes: 6 additions & 0 deletions src/FlyCube/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,7 @@ if (APPLE AND VULKAN_SUPPORT)
endif()

list(APPEND Utilities
Utilities/Common.cpp
Utilities/Common.h
Utilities/DXGIFormatHelper.cpp
Utilities/DXGIFormatHelper.h
Expand All @@ -251,6 +252,10 @@ list(APPEND Utilities
Utilities/VKUtility.h
)

if (APPLE AND VULKAN_SUPPORT)
set_property(SOURCE Utilities/Common.cpp PROPERTY COMPILE_FLAGS "-xobjective-c++")
endif()

list(APPEND View
$<$<BOOL:${DIRECTX_SUPPORT}>:View/DXView.cpp>
$<$<BOOL:${DIRECTX_SUPPORT}>:View/DXView.h>
Expand Down Expand Up @@ -293,6 +298,7 @@ if (UNIX AND NOT APPLE)
endif()

target_link_libraries(FlyCube
$<$<BOOL:${APPLE}>:-framework\ Foundation>
$<$<BOOL:${APPLE}>:-framework\ QuartzCore>
$<$<BOOL:${DIRECTX_SUPPORT}>:d3d12>
$<$<BOOL:${DIRECTX_SUPPORT}>:dxgi>
Expand Down
5 changes: 5 additions & 0 deletions src/FlyCube/Device/DXDevice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -572,6 +572,11 @@ RaytracingASPrebuildInfo DXDevice::GetTLASPrebuildInfo(uint32_t instance_count,
return GetAccelerationStructurePrebuildInfo(inputs);
}

ShaderBlobType DXDevice::GetSupportedShaderBlobType() const
{
return ShaderBlobType::kDXIL;
}

DXAdapter& DXDevice::GetAdapter()
{
return m_adapter;
Expand Down
1 change: 1 addition & 0 deletions src/FlyCube/Device/DXDevice.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ class DXDevice : public Device {
BuildAccelerationStructureFlags flags) const override;
RaytracingASPrebuildInfo GetTLASPrebuildInfo(uint32_t instance_count,
BuildAccelerationStructureFlags flags) const override;
ShaderBlobType GetSupportedShaderBlobType() const override;

DXAdapter& GetAdapter();
ComPtr<ID3D12Device> GetDevice();
Expand Down
1 change: 1 addition & 0 deletions src/FlyCube/Device/Device.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,5 @@ class Device : public QueryInterface {
BuildAccelerationStructureFlags flags) const = 0;
virtual RaytracingASPrebuildInfo GetTLASPrebuildInfo(uint32_t instance_count,
BuildAccelerationStructureFlags flags) const = 0;
virtual ShaderBlobType GetSupportedShaderBlobType() const = 0;
};
1 change: 1 addition & 0 deletions src/FlyCube/Device/MTDevice.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ class MTDevice : public Device, protected MVKPhysicalDeviceImpl {
BuildAccelerationStructureFlags flags) const override;
RaytracingASPrebuildInfo GetTLASPrebuildInfo(uint32_t instance_count,
BuildAccelerationStructureFlags flags) const override;
ShaderBlobType GetSupportedShaderBlobType() const override;

const id<MTLDevice>& GetDevice() const;
MVKPixelFormats& GetMVKPixelFormats();
Expand Down
5 changes: 5 additions & 0 deletions src/FlyCube/Device/MTDevice.mm
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,11 @@
return prebuild_info;
}

ShaderBlobType MTDevice::GetSupportedShaderBlobType() const
{
return ShaderBlobType::kSPIRV;
}

const id<MTLDevice>& MTDevice::GetDevice() const
{
return m_device;
Expand Down
5 changes: 5 additions & 0 deletions src/FlyCube/Device/VKDevice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -774,6 +774,11 @@ RaytracingASPrebuildInfo VKDevice::GetTLASPrebuildInfo(uint32_t instance_count,
return GetAccelerationStructurePrebuildInfo(acceleration_structure_info, { instance_count });
}

ShaderBlobType VKDevice::GetSupportedShaderBlobType() const
{
return ShaderBlobType::kSPIRV;
}

VKAdapter& VKDevice::GetAdapter()
{
return m_adapter;
Expand Down
1 change: 1 addition & 0 deletions src/FlyCube/Device/VKDevice.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ class VKDevice : public Device {
BuildAccelerationStructureFlags flags) const override;
RaytracingASPrebuildInfo GetTLASPrebuildInfo(uint32_t instance_count,
BuildAccelerationStructureFlags flags) const override;
ShaderBlobType GetSupportedShaderBlobType() const override;

VKAdapter& GetAdapter();
vk::Device GetDevice();
Expand Down
46 changes: 46 additions & 0 deletions src/FlyCube/Utilities/Common.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#include "Utilities/Common.h"

#include "Utilities/SystemUtils.h"

#include <filesystem>
#include <fstream>
#include <iterator>

#if defined(__APPLE__)
#import <Foundation/Foundation.h>
#endif

uint64_t Align(uint64_t size, uint64_t alignment)
{
return (size + (alignment - 1)) & ~(alignment - 1);
}

std::vector<uint8_t> ReadBinaryFile(const std::string& filepath)
{
std::ifstream file(filepath, std::ios::binary);
file.unsetf(std::ios::skipws);

std::streampos filesize;
file.seekg(0, std::ios::end);
filesize = file.tellg();
file.seekg(0, std::ios::beg);

std::vector<uint8_t> data;
data.reserve(filesize);
data.insert(data.begin(), std::istream_iterator<uint8_t>(file), std::istream_iterator<uint8_t>());
return data;
}

std::string GetAssertPath(const std::string& filepath)
{
#if defined(__APPLE__)
auto path = std::filesystem::path(filepath);
auto resource = [[NSBundle mainBundle] pathForResource:[NSString stringWithUTF8String:path.stem().c_str()]
ofType:[NSString stringWithUTF8String:path.extension().c_str()]
inDirectory:[NSString stringWithUTF8String:path.parent_path().c_str()]];
if (resource) {
return [resource UTF8String];
}
#endif
return GetExecutableDir() + "\\" + filepath;
}
27 changes: 5 additions & 22 deletions src/FlyCube/Utilities/Common.h
Original file line number Diff line number Diff line change
@@ -1,26 +1,9 @@
#pragma once

#include <cstdint>
#include <fstream>
#include <iterator>
#include <string>
#include <vector>

inline uint64_t Align(uint64_t size, uint64_t alignment)
{
return (size + (alignment - 1)) & ~(alignment - 1);
}

inline std::vector<uint8_t> ReadBinaryFile(const std::string& filename)
{
std::ifstream file(filename, std::ios::binary);
file.unsetf(std::ios::skipws);

std::streampos filesize;
file.seekg(0, std::ios::end);
filesize = file.tellg();
file.seekg(0, std::ios::beg);

std::vector<uint8_t> data;
data.reserve(filesize);
data.insert(data.begin(), std::istream_iterator<uint8_t>(file), std::istream_iterator<uint8_t>());
return data;
}
uint64_t Align(uint64_t size, uint64_t alignment);
std::vector<uint8_t> ReadBinaryFile(const std::string& filepath);
std::string GetAssertPath(const std::string& filepath);
5 changes: 1 addition & 4 deletions src/FlyCube/Utilities/FormatHelper.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
#include "Utilities/FormatHelper.h"

inline uint32_t Align(uint32_t size, uint32_t alignment)
{
return (size + (alignment - 1)) & ~(alignment - 1);
}
#include "Utilities/Common.h"

void GetFormatInfo(size_t width,
size_t height,
Expand Down

0 comments on commit b56a775

Please sign in to comment.