Skip to content

Commit

Permalink
added rendergraph shaders
Browse files Browse the repository at this point in the history
  • Loading branch information
m0dB authored and m0dB committed Dec 14, 2024
1 parent d273b5a commit 07bca3d
Show file tree
Hide file tree
Showing 26 changed files with 433 additions and 2 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4573,7 +4573,7 @@ if(VINYLCONTROL)
endif()

# rendergraph
add_subdirectory(src/rendergraph/opengl)
add_subdirectory(src/rendergraph)
target_link_libraries(mixxx-lib PUBLIC rendergraph_gl)
target_compile_definitions(mixxx-lib PRIVATE rendergraph=rendergraph_gl)

Expand Down
2 changes: 1 addition & 1 deletion src/rendergraph/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,5 @@ set(
)

add_subdirectory(opengl)
add_subdirectory(scenegraph)
# add_subdirectory(scenegraph)
add_subdirectory(shaders)
53 changes: 53 additions & 0 deletions src/rendergraph/shaders/rendergraph/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# included from src/rendergraph/CMakeLists.txt

set(
shaders
pattern.frag
pattern.vert
rgb.frag
rgb.vert
rgba.frag
rgba.vert
texture.frag
texture.vert
unicolor.frag
unicolor.vert
)

qt6_add_shaders(rendergraph_sg "shaders-qsb"
BATCHABLE
PRECOMPILE
OPTIMIZED
PREFIX
/shaders/rendergraph
FILES
${shaders}
)

# USE_QSHADER_FOR_GL is set in src/rendergraph/CMakeLists.txt when Qt >= 6.6
if(USE_QSHADER_FOR_GL)
# Add the .qsb shader bundles; rendergraph::MaterialShader will use
# QShader to extract the GLSL shader from the bundle.
message(STATUS "Adding qsb shaders to rendergraph_gl")
qt6_add_shaders(rendergraph_gl "shaders-qsb"
BATCHABLE
PRECOMPILE
OPTIMIZED
PREFIX
/shaders/rendergraph
FILES
${shaders}
)
else()
# Use GLSL shaders extracted from the .qsb shader bundles using
# generate_shaders_gl.pl
message(STATUS "Adding gl shaders to rendergraph_gl")
include(generated_shaders_gl.cmake)

qt_add_resources(rendergraph_gl "shaders-gl"
PREFIX
/shaders/rendergraph
FILES
${generated_shaders_gl}
)
endif()
29 changes: 29 additions & 0 deletions src/rendergraph/shaders/rendergraph/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# rendergraph shaders

The CMakeLists.txt in this folder generates qsb shader bundles from spirv shaders, to
be used by `rendergraph::MaterialShader`.

## For use with QML / Qt scene graph

The qsb files can be used directly through `QSGShader`. This includes the scenegraph
implementation of rendergraph.

## For use with OpenGL

Depending on the Qt version, the opengl implementation of `rendergraph::MaterialShader`
uses either the .qsb shader bundles directly, or the extracted GLSL shaders:

### Qt >= 6.6

The GLSL shaders are extracted programmatically with `QShader` and then used with
`QOpenGLShader`.

### Qt < 6.6

The GLSL shader have to extracted from the qsb shader bundles to be used by `QOpenGLShader`.
This can be done using the script `generate_shaders_gl.pl`. To use this script, make sure
that the qsb and spirv commands are in your path. qsb is part of Qt. spirv is part of the
Vulkan SDK and can be downloaded from <https://vulkan.org>

The script also generates the file ```generated_shaders_gl.cmake``` which sets a cmake
variable containing a list of all GLSL shaders, used by the CMakeLists.txt in this folder.
68 changes: 68 additions & 0 deletions src/rendergraph/shaders/rendergraph/generate_shaders_gl.pl
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
#!/usr/bin/perl

my @files = (glob("*.vert"),glob("*.frag"));

open(GENERATED,">generated_shaders_gl.cmake");
print(GENERATED "set(generated_shaders_gl\n");
for $file (@files)
{
system("qsb","--glsl","120",$file,"-o","/tmp/$$-$file.qsb");
open(INFILE,"qsb --dump /tmp/$$-$file.qsb|");
open(OUTFILE,">$file.gl");
$ok = 0;
$comment_added = 0;
print "Generating $file.gl from $file\n";
while (<INFILE>)
{
if ($in_shader_block == 2)
{
if (m/^\*\*/)
{
$in_shader_block = 0;
$ok = 1;
}
else
{
if (!$comment_added)
{
if (!m/^#/)
{
print(OUTFILE "//// GENERATED - EDITS WILL BE OVERWRITTEN\n");
$comment_added = 1;
}
}
print OUTFILE "$_";
}
}
elsif ($in_shader_block == 1)
{
chomp($_);
if ($_ eq "Contents:")
{
$in_shader_block = 2;
}
}
else
{
chomp($_);
if ($_ eq "Shader 1: GLSL 120 [Standard]")
{
$in_shader_block = 1;
}
}
}
close INFILE;
close OUTFILE;
if($ok)
{
print(GENERATED " $file.gl\n");
}
else
{
print STDERR "Failed to generated $file.gl";
unlink("$file.gl")
}
unlink("/tmp/$$-$file.qsb");
}
print(GENERATED ")\n");
close GENERATED;
13 changes: 13 additions & 0 deletions src/rendergraph/shaders/rendergraph/generated_shaders_gl.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
set(
generated_shaders_gl
pattern.vert.gl
rgb.vert.gl
rgba.vert.gl
texture.vert.gl
unicolor.vert.gl
pattern.frag.gl
rgb.frag.gl
rgba.frag.gl
texture.frag.gl
unicolor.frag.gl
)
9 changes: 9 additions & 0 deletions src/rendergraph/shaders/rendergraph/pattern.frag
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#version 440

layout(binding = 1) uniform sampler2D texture1;
layout(location = 0) in vec2 vTexcoord;
layout(location = 0) out vec4 fragColor;

void main() {
fragColor = texture(texture1, fract(vTexcoord));
}
11 changes: 11 additions & 0 deletions src/rendergraph/shaders/rendergraph/pattern.frag.gl
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#version 120
//// GENERATED - EDITS WILL BE OVERWRITTEN

uniform sampler2D texture1;

varying vec2 vTexcoord;

void main()
{
gl_FragData[0] = texture2D(texture1, fract(vTexcoord));
}
15 changes: 15 additions & 0 deletions src/rendergraph/shaders/rendergraph/pattern.vert
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#version 440

layout(std140, binding = 0) uniform buf {
mat4 matrix;
}
ubuf;

layout(location = 0) in vec4 position;
layout(location = 1) in vec2 texcoord;
layout(location = 0) out vec2 vTexcoord;

void main() {
vTexcoord = texcoord;
gl_Position = ubuf.matrix * position;
}
19 changes: 19 additions & 0 deletions src/rendergraph/shaders/rendergraph/pattern.vert.gl
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#version 120
//// GENERATED - EDITS WILL BE OVERWRITTEN

struct buf
{
mat4 matrix;
};

uniform buf ubuf;

varying vec2 vTexcoord;
attribute vec2 texcoord;
attribute vec4 position;

void main()
{
vTexcoord = texcoord;
gl_Position = ubuf.matrix * position;
}
8 changes: 8 additions & 0 deletions src/rendergraph/shaders/rendergraph/rgb.frag
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#version 440

layout(location = 0) in vec3 vColor;
layout(location = 0) out vec4 fragColor;

void main() {
fragColor = vec4(vColor, 1.0);
}
9 changes: 9 additions & 0 deletions src/rendergraph/shaders/rendergraph/rgb.frag.gl
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#version 120
//// GENERATED - EDITS WILL BE OVERWRITTEN

varying vec3 vColor;

void main()
{
gl_FragData[0] = vec4(vColor, 1.0);
}
15 changes: 15 additions & 0 deletions src/rendergraph/shaders/rendergraph/rgb.vert
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#version 440

layout(std140, binding = 0) uniform buf {
mat4 matrix;
}
ubuf;

layout(location = 0) in vec4 position;
layout(location = 1) in vec3 color;
layout(location = 0) out vec3 vColor;

void main() {
vColor = color;
gl_Position = ubuf.matrix * position;
}
19 changes: 19 additions & 0 deletions src/rendergraph/shaders/rendergraph/rgb.vert.gl
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#version 120
//// GENERATED - EDITS WILL BE OVERWRITTEN

struct buf
{
mat4 matrix;
};

uniform buf ubuf;

varying vec3 vColor;
attribute vec3 color;
attribute vec4 position;

void main()
{
vColor = color;
gl_Position = ubuf.matrix * position;
}
8 changes: 8 additions & 0 deletions src/rendergraph/shaders/rendergraph/rgba.frag
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#version 440

layout(location = 0) in vec4 vColor;
layout(location = 0) out vec4 fragColor;

void main() {
fragColor = vec4(vColor.xyz * vColor.w, vColor.w); // premultiple alpha
}
9 changes: 9 additions & 0 deletions src/rendergraph/shaders/rendergraph/rgba.frag.gl
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#version 120
//// GENERATED - EDITS WILL BE OVERWRITTEN

varying vec4 vColor;

void main()
{
gl_FragData[0] = vec4(vColor.xyz * vColor.w, vColor.w);
}
15 changes: 15 additions & 0 deletions src/rendergraph/shaders/rendergraph/rgba.vert
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#version 440

layout(std140, binding = 0) uniform buf {
mat4 matrix;
}
ubuf;

layout(location = 0) in vec4 position;
layout(location = 1) in vec4 color;
layout(location = 0) out vec4 vColor;

void main() {
vColor = color;
gl_Position = ubuf.matrix * position;
}
19 changes: 19 additions & 0 deletions src/rendergraph/shaders/rendergraph/rgba.vert.gl
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#version 120
//// GENERATED - EDITS WILL BE OVERWRITTEN

struct buf
{
mat4 matrix;
};

uniform buf ubuf;

varying vec4 vColor;
attribute vec4 color;
attribute vec4 position;

void main()
{
vColor = color;
gl_Position = ubuf.matrix * position;
}
9 changes: 9 additions & 0 deletions src/rendergraph/shaders/rendergraph/texture.frag
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#version 440

layout(binding = 1) uniform sampler2D texture1;
layout(location = 0) in vec2 vTexcoord;
layout(location = 0) out vec4 fragColor;

void main() {
fragColor = texture(texture1, vTexcoord);
}
11 changes: 11 additions & 0 deletions src/rendergraph/shaders/rendergraph/texture.frag.gl
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#version 120
//// GENERATED - EDITS WILL BE OVERWRITTEN

uniform sampler2D texture1;

varying vec2 vTexcoord;

void main()
{
gl_FragData[0] = texture2D(texture1, vTexcoord);
}
15 changes: 15 additions & 0 deletions src/rendergraph/shaders/rendergraph/texture.vert
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#version 440

layout(std140, binding = 0) uniform buf {
mat4 matrix;
}
ubuf;

layout(location = 0) in vec4 position;
layout(location = 1) in vec2 texcoord;
layout(location = 0) out vec2 vTexcoord;

void main() {
vTexcoord = texcoord;
gl_Position = ubuf.matrix * position;
}
Loading

0 comments on commit 07bca3d

Please sign in to comment.