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

Adding support for shader includes pre-processor directives #269

Open
wants to merge 12 commits into
base: develop
Choose a base branch
from

Conversation

adriengivry
Copy link
Owner

@adriengivry adriengivry commented Nov 23, 2023

Description

Added support for shader includes, which allow us to reuse code between shaders:

The Overload/Resources/ folder has been added to premake as a separate project, so this way we can see and edit resources (such as engine and editor shaders) directly from VS (or any other IDE of your choice).

Review Guidance

Now passing an optional path to realPath conversion function to the ShaderLoader class, to be able to parse and load included files based on rules set by the caller (OvCore). This way OvRendering doesn't have to be aware of our folder architecture.
If a file starting with : is included, ie:

#include ":Shaders/Vertex/Basic.ovfxh"

The ShaderLoader will resolve the file path and look into the Engine folder.
If no : is provided, it will look into the user project's assets, ie:

#include "Shaders/MyCustomShader.ovfxh"

Some conventions have been introduced:

  • Shader files are named *.ovfx
  • Partial shader files (included) are named *.ovfxh

Example

// Unlit.ovfx

#shader vertex
#version 430 core

#include ":Shaders/Common/Buffers/EngineUBO.ovfxh"
#include ":Shaders/Common/Utils.ovfxh"

layout (location = 0) in vec3 geo_Pos;
layout (location = 1) in vec2 geo_TexCoords;

out VS_OUT
{
    vec3 FragPos;
    vec2 TexCoords;
} vs_out;

void main()
{
    vs_out.FragPos = vec3(ubo_Model * vec4(geo_Pos, 1.0));
    vs_out.TexCoords = geo_TexCoords;

    gl_Position = ubo_Projection * ubo_View * vec4(vs_out.FragPos, 1.0);
}

#shader fragment
#version 430 core

#include ":Shaders/Common/Buffers/EngineUBO.ovfxh"
#include ":Shaders/Common/Utils.ovfxh"

in VS_OUT
{
    vec3 FragPos;
    vec2 TexCoords;
} fs_in;

uniform vec4 u_Diffuse = vec4(1.0);
uniform sampler2D u_DiffuseMap;
uniform vec2 u_TextureTiling = vec2(1.0);
uniform vec2 u_TextureOffset = vec2(0.0);

out vec4 FRAGMENT_COLOR;

void main()
{
    vec2 texCoords = TileAndOffsetTexCoords(fs_in.TexCoords, u_TextureTiling, u_TextureOffset);
    FRAGMENT_COLOR = texture(u_DiffuseMap, texCoords) * u_Diffuse;
}

Limitations

The current implementation doesn't support including the same shader file multiple time, and doing so will potentially result into compilation errors.

Screenshots

image
Addition of the "Resources" project (SharedItems)

Related Issues

Fixes #104

@adriengivry adriengivry changed the title Adding support for shader includes pre-processor directives WIP: Adding support for shader includes pre-processor directives Nov 23, 2023
@adriengivry adriengivry added Feature New feature to the engine Graphics Graphical feature Project Configuration Anything related to setuping our projects labels Nov 23, 2023
@adriengivry adriengivry changed the title WIP: Adding support for shader includes pre-processor directives Adding support for shader includes pre-processor directives Nov 23, 2023
@adriengivry adriengivry marked this pull request as ready for review November 23, 2023 22:05
@adriengivry adriengivry self-assigned this Dec 6, 2023
@adriengivry
Copy link
Owner Author

Rebased the PR following the merge of #277

@adriengivry
Copy link
Owner Author

adriengivry commented Mar 19, 2024

@maxbrundev @litelawliet I might need some help on this one to define the default extension for partial shader files (shader files that are included, but cannot be compiled by themselves).

Right now it's:

  • .glsl for shaders
  • .frag.glsl for fragment shaders
  • .vert.glsl for vertex shaders
  • .part.glsl for other shader parts that can be included

The reason I kept .glsl everywhere is so that the asset browser still consider these file shaders, so they are properly displayed.

We could also change the extension, or have 2 extension: .ovfx and .ovfxh (With .ovfxh being a shader part that cannot be compiled by itself, such as a fragment, a vertex, or some utils functions).

@adriengivry
Copy link
Owner Author

@litelawliet @maxbrundev I went with .ovfx and .ovfxh for now. I've also improved standard shader code to be more consistent and easier to work with when creating new shaders.

I've found one current limitation with this implementation, which might need further work (maybe in another PR?).

I'm also considering using #pragma statements instead of #shader [vertex/fragment], as it's better handled by most IDEs when it comes to syntax highlighting, and might be more standard.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Feature New feature to the engine Graphics Graphical feature Project Configuration Anything related to setuping our projects
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support for shader includes
1 participant