Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Redesign functionality of GLSLcompiler class #655

Conversation

Szymon-Rajczakowski-Mobica
Copy link
Contributor

@Szymon-Rajczakowski-Mobica Szymon-Rajczakowski-Mobica commented Mar 29, 2023

Description

Redesign functionality of GLSLcompiler class. Change name of this class to ShaderCompiler. Add support for HLSL, GLSL and SPV files.

General Checklist:

Please ensure the following points are checked:

  • My code follows the coding style
  • I have reviewed file licenses
  • I have updated the header Copyright to reflect the current year (CI build will fail if Copyright is out of date)
  • I have commented any added functions (in line with Doxygen)
  • I have commented any code that could be hard to understand
  • My changes do not add any new compiler warnings
  • My changes do not add any new validation layer errors or warnings
  • I have used existing framework/helper functions where possible
  • My changes build on Windows, Linux, macOS and Android. Otherwise I have documented any exceptions
  • My changes do not add any regressions
  • I have tested every sample to ensure everything runs correctly
  • This PR describes the scope and expected impact of the changes I am making

Sample Checklist

If your PR contains a new or modified sample, these further checks must be carried out in addition to the General Checklist:

  • I have tested the sample on at least one compliant Vulkan implementation
  • If the sample is vendor-specific, I have tagged it appropriately
  • I have stated on what implementation the sample has been tested so that others can test on different implementations and platforms
  • Any dependent assets have been merged and published in downstream modules
  • For new samples, I have added a paragraph with a summary to the appropriate chapter in the samples readme

@SaschaWillems
Copy link
Collaborator

Not sure if this should be a separate sample. Imo it would be better if this was an addition to the existing basic raytracing sample, e.g. maybe a compile time define so people can toggle between GLSL and HLSL.

Copy link
Contributor

@asuessenbach asuessenbach left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried to run hlsl_shaders, but get the following error:

[error] [framework\common\vk_common.cpp:386] Failed to compile shader, Error: ERROR: #version: ES shaders for SPIR-V require version 310 or higher
ERROR: :20: '' :  syntax error, unexpected LEFT_BRACKET
ERROR: 2 compilation errors.  No code generated.

Would I need to update anything on my end?

Besides that: is there any means to switch between HSLS and GLSL now?

framework/common/vk_common.cpp Outdated Show resolved Hide resolved
framework/common/vk_common.cpp Outdated Show resolved Hide resolved
framework/common/vk_common.cpp Outdated Show resolved Hide resolved
framework/shader_compiler.cpp Outdated Show resolved Hide resolved
framework/shader_compiler.h Outdated Show resolved Hide resolved
samples/extensions/raytracing_basic/raytracing_basic.cpp Outdated Show resolved Hide resolved
samples/extensions/raytracing_basic/raytracing_basic.cpp Outdated Show resolved Hide resolved
samples/extensions/raytracing_basic/raytracing_basic.cpp Outdated Show resolved Hide resolved
@Szymon-Rajczakowski-Mobica Szymon-Rajczakowski-Mobica changed the title WIP Hlsl raytracing basic Redesign functionality of GLSLcompiler class Jun 19, 2023
@Szymon-Rajczakowski-Mobica Szymon-Rajczakowski-Mobica marked this pull request as ready for review June 19, 2023 12:06
@CLAassistant
Copy link

CLAassistant commented Jun 26, 2023

CLA assistant check
All committers have signed the CLA.

samples/extensions/raytracing_basic/raytracing_basic.cpp Outdated Show resolved Hide resolved
framework/api_vulkan_sample.cpp Outdated Show resolved Hide resolved
framework/api_vulkan_sample.cpp Outdated Show resolved Hide resolved
framework/common/vk_common.cpp Show resolved Hide resolved
framework/shader_compiler.h Outdated Show resolved Hide resolved
.copyrightignore Outdated Show resolved Hide resolved
scripts/copyright.py Outdated Show resolved Hide resolved
asuessenbach
asuessenbach previously approved these changes Jul 3, 2023
samples/extensions/raytracing_basic/CMakeLists.txt Outdated Show resolved Hide resolved
@@ -33,7 +33,7 @@ namespace vkb
{
/// Helper class to generate SPIRV code from GLSL source
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With the compiler now being able to support other languages, the comments need to be changed too. They still refer to GLSL only.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That comment still needs to be updated.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated

@@ -54,19 +54,21 @@ class GLSLCompiler
static void reset_target_environment();

/**
* @brief Compiles GLSL to SPIRV code
* @brief Compiles GLSL/HLSL to SPIRV code
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this correct? If so, what tool is used to compile HLSL tp SPIR-V (dxc)? Or does it simply load offline compiled SPV? If it's the latter, the comment needs to be changed.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Glslang is used for compilation at runtime and for offline compilation DirectX Shader Compiler is being used.

@@ -410,7 +410,7 @@ void HlslShaders::prepare_pipelines()

// Load shaders
std::array<VkPipelineShaderStageCreateInfo, 2> shader_stages{};
shader_stages[0] = load_hlsl_shader("hlsl_shaders/hlsl_shader.vert", VK_SHADER_STAGE_VERTEX_BIT);
shader_stages[0] = load_shader("hlsl_shaders/hlsl_shader.vert", VK_SHADER_STAGE_VERTEX_BIT, vkb::ShaderSourceLanguage::VK_HLSL);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change doesn't seem correct. This is a standalone sample that is supposed to use it's own hlsl loading function, but this line would use the one from the framework instead. The change should be reverted.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This still needs to be reverted.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reverted

@@ -1,4 +1,4 @@
/* Copyright (c) 2021-2023, NVIDIA CORPORATION. All rights reserved.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think it's okay to replace the existing copyright. If you add to a file, please append your copyright instead.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is still open. This sample was done by Andreas (Nvidia).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had to revert fix, it caused CI checks for copyrights to fail

@SaschaWillems
Copy link
Collaborator

SaschaWillems commented Jul 3, 2023

I did a first quick review and added a few comments.

One thing I'm not sure about is the direction of this PR.

On one hand it seems to add HLSL support to the framework's compiler, but only through glslang, which is very very limited. And on the other hand it also adds direct loading of SPIR-V for e.g. the basic ray tracing sample.

Tbh. I think we should discuss a different approach that better fits our framework strategy.

We've been talking about HLSL support for quite some time and one option would be adding direct SPIR-V loading to the shader compiler (as a derived class maybe) so we can add HLSL to all samples with offline compiled SPIR-V files via DXC. The goal is also to not have to change every sample but to support shader language selection at framework level instead. And imo we should move into that direction.

I appreciate the work put in this PR, but it feels a bit like a stop gap towards our actual goal and we might revert a large part of that PR in the future.

I'll wait for other opinions, and I'd happily discuss this in the next samples call :)

@Patryk-Jastrzebski-Mobica
Copy link
Contributor

Hi @SaschaWillems,
We have discussed this problem and think that the best solution will be to extend HLSL support (add raytracing support) in the glslang repository. At the moment @Dawid-Lorenz-Mobica is investigating how to add raytracing support for the HLSL language in the glslang repository.

@SaschaWillems
Copy link
Collaborator

Tbh I'm not sure if that's the right approach. HLSL in glslang is very limited and the actual reference compiler is DXC. So if we want to add HLSL support we should either integrate DXC or offline compile HLSL to SPIR-V and just use that.

Update README
fix validation layers errors
Remove hlsl_raytracing_basic
to shader_compiler
- Add ShaderSourceLanguage enum
- Add src_language parameter to load_shader for
support different shader languages
- Change headers and few names in existing samples
to keep the order of arguments more meaningful
- Add switch in UI to changing shaders language in
raytracing_basic sample
fix typo
bring HPP load_shader() and update_render_pass_flags() in line with non-hpp version
use a single compile_to_spirv() with default source language rather than have 2 flavours
Signed-off-by: pawel-jastrzebski-mobica <pawel.jastrzebski@mobica.com>
@Szymon-Rajczakowski-Mobica Szymon-Rajczakowski-Mobica changed the title Redesign functionality of GLSLcompiler class WIP: Redesign functionality of GLSLcompiler class Feb 12, 2024
docs/build.adoc Outdated
@@ -208,13 +212,20 @@ build\windows\app\bin\Release\AMD64\vulkan_samples.exe
* C{pp}14 Compiler
* <<cmake-options,CMake Options>>
* <<3d-models,3D models>>
* Vulkan SDK
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can this be reverted? We don't require the SDK to build our samples.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reverted

docs/build.adoc Outdated
@@ -243,11 +254,18 @@ cmake --build build/linux --config Release --target vulkan_samples -j$(nproc)
* https://vulkan.lunarg.com/doc/sdk/latest/mac/getting_started.html[Vulkan SDK] `./install_vulkan.py`
* <<cmake-options,CMake Options>>
* <<3d-models,3D models>>
* Vulkan SDK
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reverted

@SaschaWillems
Copy link
Collaborator

SaschaWillems commented Feb 13, 2024

Also odd, I get three out-of-place hlsl related entries in the MSVC project browser now:

image

Probably left overs. Would be good if this PR did not include any HLSL shaders at all, but just the code required to load SPIR-V. We'll add the HLSL in separate PRs.

@pawel-jastrzebski-mobica
Copy link
Contributor

@SaschaWillems : do you want to remove the whole off-line shader compilation functionality from the PR?

Signed-off-by: pawel-jastrzebski-mobica <pawel.jastrzebski@mobica.com>
@SaschaWillems
Copy link
Collaborator

Nope, having a way to compile the HLSL shaders vie the build system is fine. Problem is where those projects are put in the project structure. They appear at the top level, but should instead be part of the sample itself.

@SaschaWillems SaschaWillems mentioned this pull request Mar 1, 2024
@Seweryn-Zielas-Mobica
Copy link
Contributor

I changed way of using dxc for offline compilation. I had problem with add_custom_target/add_custom_command that created targets in top level view of Visual Studio solution, I was only found way how to move it into other folders, but couldn't move it under other targets like specific samples. From what I red and understood, this is most probably not possible.
Instead of add_custom_target/add_custom_command is used execute_process which seems to work as good or maybe even better than previous solution, as now code for compiling shaders seems more straightforward and less bulky.

The difference in how it works now is rather not significant, before HLSL shaders were compiled at the start of executing build command, e.g. "cmake --build build/linux --config Release --target vulkan_samples -j$(nproc)"
Now they are compiled after running command for generating project, e.g. "cmake -G "Unix Makefiles" -Bbuild/linux -DCMAKE_BUILD_TYPE=Release"

I also updated cmake to only compile shaders if dxc executable is found, by searching for it under VULKAN_SDK path.

There is still glslang used for compilation HLSL shaders at sample runtime, and rather big modification of hpp_hello_triangle.cpp which I'm not sure if is needed.

There is still some work left to do, mostly stylistic like naming scheme and directories/folders paths, but core functionality of this PR that is compiling HLSL shaders seems to be done.

Copy link
Collaborator

@SaschaWillems SaschaWillems left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you undo the addition of HLSL shaders for the ray tracing basic sample. I'm already working on adding actual HLSL shaders, and to clearly divide work this PR should only add the offline compilation part.

@Seweryn-Zielas-Mobica
Copy link
Contributor

@SaschaWillems As we discussed that we should only add offline compilation for HLSL, I've created created new PR that only do this, as it seemed easier and less misguiding when looking at this PR title.
New PR: #990

@marty-johnson59
Copy link
Contributor

Closing this as we're going a different direction

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
hlsl Everything related to getting HLSL support added
Projects
None yet
Development

Successfully merging this pull request may close these issues.

10 participants