-
Notifications
You must be signed in to change notification settings - Fork 0
Gl reflection #36
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
Merged
Merged
Gl reflection #36
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
dc82e14
feat: add module glsl reflector
WSQS 9a0dabc
feat: implement glsl reflector
Sophomore42 cc36db2
Vertex layout (#34)
WSQS 3f12b7e
feat: Implement glsl reflector into sdl wrapper
Sophomore42 6656a97
feat: recreate data when shader update
Sophomore42 41ea80d
fix: add header file cstdint
Sophomore42 aa041fd
fix: import module
Sophomore42 402db2b
fix: remove install for glslr
WSQS e17de31
fix: add time out for gtest_discover_tests
WSQS 36ee1fd
Merge branch 'dev' into gl_reflection
WSQS f81bbdd
fix:add head file to make in compile under mint
WSQS 2061a92
fix: add and clean up header file
WSQS 7ef1ad8
style: format code with ClangFormat
deepsource-autofix[bot] File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| cmake_minimum_required(VERSION 3.30) | ||
| project(glsl_reflector LANGUAGES CXX) | ||
|
|
||
| set(CMAKE_CXX_STANDARD 23) | ||
| add_library(glsl_reflector STATIC) | ||
|
|
||
| target_sources(glsl_reflector | ||
| PUBLIC | ||
| FILE_SET cxx_modules TYPE CXX_MODULES FILES | ||
| modules/glsl_reflector.ixx | ||
| modules/glsl_reflector.type.ixx | ||
| ) | ||
|
|
||
| target_link_libraries(glsl_reflector PUBLIC glslang glslang::glslang-default-resource-limits) | ||
|
|
||
| add_executable(glslr test/test.cpp) | ||
|
|
||
| target_link_libraries(glslr PUBLIC glsl_reflector gtest gtest_main) | ||
|
|
||
| add_test(NAME unit_tests COMMAND glslr) | ||
| gtest_discover_tests(glslr | ||
| DISCOVERY_TIMEOUT 60) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| // glsl_reflector.ixx | ||
| // Created by sophomore on 11/19/25. | ||
| // | ||
| module; | ||
| #include <iostream> | ||
| #include <string> | ||
| #include <vector> | ||
| #include "glslang/Include/intermediate.h" | ||
| #include "glslang/MachineIndependent/localintermediate.h" | ||
| #include "glslang/Public/ResourceLimits.h" | ||
| #include "glslang/Public/ShaderLang.h" | ||
| export module glsl_reflector; | ||
| export import :type; | ||
|
|
||
| namespace sopho | ||
| { | ||
|
|
||
| class GlslangProcessGuard | ||
| { | ||
| public: | ||
| GlslangProcessGuard() { glslang::InitializeProcess(); } | ||
| ~GlslangProcessGuard() { glslang::FinalizeProcess(); } | ||
| }; | ||
|
|
||
| auto to_basic_type(glslang::TBasicType gl_basic_type) | ||
| { | ||
| switch (gl_basic_type) | ||
| { | ||
| case glslang::EbtFloat: | ||
| return BasicType::FLOAT; | ||
| default: | ||
| return BasicType::NONE; | ||
| } | ||
| } | ||
|
|
||
| export auto reflect_vertex(const std::string& vertex_source) | ||
| { | ||
| GlslangProcessGuard glslang_initializer{}; | ||
| VertexReflection result{}; | ||
| glslang::TShader shader(EShLangVertex); | ||
| std::vector sources{vertex_source.data()}; | ||
| shader.setStrings(sources.data(), sources.size()); | ||
|
|
||
| int clientInputSemanticsVersion = 100; // not used for desktop GL | ||
| glslang::EShTargetClientVersion clientVersion = glslang::EShTargetOpenGL_450; | ||
| glslang::EShTargetLanguageVersion targetVersion = glslang::EShTargetSpv_1_0; | ||
|
|
||
| shader.setEnvInput(glslang::EShSourceGlsl, EShLangVertex, glslang::EShClientOpenGL, | ||
| clientInputSemanticsVersion); | ||
| shader.setEnvClient(glslang::EShClientOpenGL, clientVersion); | ||
| shader.setEnvTarget(glslang::EShTargetSpv, targetVersion); | ||
|
|
||
| EShMessages messages = (EShMessages)(EShMsgDefault | EShMsgSpvRules | EShMsgVulkanRules); | ||
|
|
||
| if (!shader.parse(GetDefaultResources(), 450, ENoProfile, false, false, messages)) | ||
| { | ||
| std::cerr << "Parse failed:\n" << shader.getInfoLog() << std::endl; | ||
| return result; | ||
| } | ||
|
|
||
| glslang::TProgram program; | ||
| program.addShader(&shader); | ||
| if (!program.link(messages)) | ||
| { | ||
| std::cerr << "Link failed:\n" << program.getInfoLog() << std::endl; | ||
| return result; | ||
| } | ||
| program.buildReflection(); | ||
| auto count = program.getNumPipeInputs(); | ||
| for (auto i = 0; i < count; i++) | ||
| { | ||
| const auto& var = program.getPipeInput(i); | ||
|
|
||
| const auto& type = var.getType(); | ||
| const auto& q = type->getQualifier(); | ||
|
|
||
| std::string name = var.name.c_str(); | ||
| auto vector = type->getVectorSize(); | ||
| std::cout << name << vector << std::endl; | ||
| result.inputs.emplace_back(VertexInfo{.location = var.layoutLocation(), | ||
| .name = var.name, | ||
| .basic_type = to_basic_type(type->getBasicType()), | ||
| .vector_size = type->getVectorSize()}); | ||
| } | ||
| return result; | ||
| } | ||
WSQS marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } // namespace sopho | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| // glsl_reflector.type.ixx | ||
| // Created by wsqsy on 11/20/2025. | ||
| // | ||
| module; | ||
| #include <cstdint> | ||
| #include <string> | ||
| #include <vector> | ||
| export module glsl_reflector:type; | ||
|
|
||
| export namespace sopho | ||
| { | ||
| enum class BasicType : std::uint8_t | ||
| { | ||
| NONE, | ||
| FLOAT | ||
| }; | ||
|
|
||
| struct VertexInfo | ||
| { | ||
| std::uint32_t location{}; | ||
| std::string name{}; | ||
| BasicType basic_type{}; | ||
| int vector_size{}; | ||
| }; | ||
|
|
||
| struct VertexReflection | ||
| { | ||
| std::vector<VertexInfo> inputs{}; | ||
| }; | ||
| } // namespace sopho |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| // test.cpp | ||
| // Created by wsqsy on 11/19/2025. | ||
| // | ||
| #include <string> | ||
WSQS marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| #include <vector> | ||
WSQS marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
WSQS marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| #include <gtest/gtest.h> | ||
|
|
||
| import glsl_reflector; | ||
WSQS marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| TEST(BasicTest, Basic) { EXPECT_EQ(1 + 1, 2); } | ||
|
|
||
| TEST(reflect_vertex, Basic) | ||
| { | ||
| auto reflect_info = sopho::reflect_vertex(R"( | ||
| #version 450 | ||
| layout(location = 0) in vec3 inPos; | ||
| layout(location = 1) in vec3 inNormal; | ||
|
|
||
| layout(location = 0) out vec3 outNormal; | ||
|
|
||
| void main() { | ||
| gl_Position = vec4(inPos, 1.0); | ||
| outNormal = inNormal; | ||
| } | ||
| )"); | ||
| EXPECT_EQ(reflect_info.inputs.size(), 2); | ||
|
|
||
| EXPECT_EQ(reflect_info.inputs[0].location, 0); | ||
| EXPECT_EQ(reflect_info.inputs[0].name, "inPos"); | ||
| EXPECT_EQ(reflect_info.inputs[0].basic_type, sopho::BasicType::FLOAT); | ||
| EXPECT_EQ(reflect_info.inputs[0].vector_size, 3); | ||
|
|
||
| EXPECT_EQ(reflect_info.inputs[1].location, 1); | ||
| EXPECT_EQ(reflect_info.inputs[1].name, "inNormal"); | ||
| EXPECT_EQ(reflect_info.inputs[1].basic_type, sopho::BasicType::FLOAT); | ||
| EXPECT_EQ(reflect_info.inputs[1].vector_size, 3); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -5,10 +5,12 @@ | |||||
| #include <array> | ||||||
| #include <cmath> | ||||||
| #include <expected> | ||||||
| #include <format> | ||||||
| #include <iostream> | ||||||
WSQS marked this conversation as resolved.
Show resolved
Hide resolved
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. warning: included header format is not used directly [misc-include-cleaner]
Suggested change
|
||||||
| #include <memory> | ||||||
| #include <numbers> | ||||||
| #include <string> | ||||||
| #include <variant> | ||||||
|
|
||||||
WSQS marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
| #include "imgui.h" | ||||||
| #include "imgui_impl_sdl3.h" | ||||||
|
|
@@ -19,6 +21,7 @@ | |||||
| #include "SDL3/SDL_gpu.h" | ||||||
| #include "SDL3/SDL_keycode.h" | ||||||
|
|
||||||
| import glsl_reflector; | ||||||
WSQS marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
| import sdl_wrapper; | ||||||
|
|
||||||
| struct CameraUniform | ||||||
|
|
@@ -229,22 +232,28 @@ void main() | |||||
| auto ptr = editor_data.raw; | ||||||
| for (int vertex_index = 0; vertex_index < editor_data.vertex_count; ++vertex_index) | ||||||
| { | ||||||
| for (const auto& format : editor_data.layout.get_vertex_format()) | ||||||
| for (const auto& format : editor_data.layout.get_vertex_reflection().inputs) | ||||||
| { | ||||||
| switch (format) | ||||||
| switch (format.basic_type) | ||||||
| { | ||||||
| case SDL_GPU_VERTEXELEMENTFORMAT_FLOAT3: | ||||||
| changed |= ImGui::DragFloat3(std::format("node{}", vertex_index).data(), | ||||||
| reinterpret_cast<float*>(ptr), 0.01f, -1.f, 1.f); | ||||||
| break; | ||||||
| case SDL_GPU_VERTEXELEMENTFORMAT_FLOAT4: | ||||||
| changed |= ImGui::DragFloat4(std::format("color{}", vertex_index).data(), | ||||||
| reinterpret_cast<float*>(ptr), 0.01f, -1.f, 1.f); | ||||||
| case sopho::BasicType::FLOAT: | ||||||
| { | ||||||
| switch (format.vector_size) | ||||||
| { | ||||||
| case 3: | ||||||
| changed |= ImGui::DragFloat3(std::format("{}{}", format.name, vertex_index).data(), | ||||||
| reinterpret_cast<float*>(ptr), 0.01f, -1.f, 1.f); | ||||||
| break; | ||||||
| case 4: | ||||||
| changed |= ImGui::DragFloat4(std::format("{}{}", format.name, vertex_index).data(), | ||||||
| reinterpret_cast<float*>(ptr), 0.01f, -1.f, 1.f); | ||||||
| } | ||||||
| } | ||||||
WSQS marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
| break; | ||||||
| default: | ||||||
| break; | ||||||
| } | ||||||
| auto size = sopho::get_size(format); | ||||||
| auto size = sopho::get_size(sopho::to_sdl_format(format.basic_type, format.vector_size)); | ||||||
| ptr += size; | ||||||
| } | ||||||
| } | ||||||
|
|
@@ -276,6 +285,12 @@ void main() | |||||
| SDL_LogError(SDL_LOG_CATEGORY_RENDER, "Failed to set vertex shader from editor, error = %d", | ||||||
| static_cast<int>(result.error())); | ||||||
| } | ||||||
| else | ||||||
| { | ||||||
| auto new_data = m_gpu->create_data(*m_renderable->procedural(), 3); | ||||||
| m_renderable->data() = std::make_shared<sopho::RenderData>(std::move(new_data.value())); | ||||||
| m_renderable->data()->upload(); | ||||||
| } | ||||||
| } | ||||||
| } | ||||||
| break; | ||||||
|
|
||||||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Incomplete type support in
to_basic_type.The function only handles
glslang::EbtFloat, returningBasicType::NONEfor all other types. Common GLSL types likeint,uint,bool, anddoubleare unhandled, which will cause vertex reflection to report incorrect types for non-float attributes.If this is intentional (e.g., your shaders only use floats), add a comment explaining the limitation. Otherwise, expand support:
auto to_basic_type(glslang::TBasicType gl_basic_type) { switch (gl_basic_type) { case glslang::EbtFloat: return BasicType::FLOAT; + case glslang::EbtInt: + return BasicType::INT; + case glslang::EbtUint: + return BasicType::UINT; + case glslang::EbtBool: + return BasicType::BOOL; + case glslang::EbtDouble: + return BasicType::DOUBLE; default: return BasicType::NONE; } }🤖 Prompt for AI Agents