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

Added vsgcustomshaderset example and state inhertance testing to vsgbuilder and vsgshadow #275

Merged
merged 21 commits into from
Nov 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
a744a2b
Added begninnings of new vsgcustomshaderset example
robertosfield Oct 26, 2023
5a34849
Added loading and rendering of models
robertosfield Nov 1, 2023
695b20f
Added custom_pbr shaders
robertosfield Nov 1, 2023
247aaa0
Added define's for MATERIAL_DESCRIPTOR_SET and VIEW_DESCRIPTOR_SET in…
robertosfield Nov 1, 2023
4baf7ba
Added preliminary ShaderSet use of VIEW_DESCRIPTOR_SET & MATERIAL_DES…
robertosfield Nov 1, 2023
45b4ba0
Changed VIEW_DESCRIPTOR_SET and MATERIAL_DESCRIPTOR_SET to 0 and 1 re…
robertosfield Nov 1, 2023
16084e3
Added ColorModulator uniform to test out custom ShaderSet
robertosfield Nov 3, 2023
547e0e9
Implement test of providing the ColorModulation paramaters via root S…
robertosfield Nov 3, 2023
8ef72c5
Added --samples and --d32 command line options
robertosfield Nov 3, 2023
4945201
Fixed setup of ViewDependentState related bindings
robertosfield Nov 4, 2023
0efa526
Added passing of the root StateGroup's state onto the loader via vsg:…
robertosfield Nov 4, 2023
1569117
Changed to using #define's for setting the DescriptorSet set to use
robertosfield Nov 6, 2023
e470615
Adopted use of Options::inhertiedState
robertosfield Nov 6, 2023
b2278b9
Added --inherit support to test the vsg::Builder new Options::inherit…
robertosfield Nov 7, 2023
142d11e
Standaized --inherit implementation
robertosfield Nov 9, 2023
d7e0df5
Updated pbr, phong and flat shadeed ShaderSets and their shaders to u…
robertosfield Nov 9, 2023
73fc1cf
Fixed Phong ShaderSet setup
robertosfield Nov 10, 2023
0c34e61
Removed debug message
robertosfield Nov 10, 2023
adcbfc3
Introduced a Fog structure to provide a more instructive example of e…
robertosfield Nov 13, 2023
fbf771e
Added exponential fog
robertosfield Nov 13, 2023
065414e
Added header
robertosfield Nov 13, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
563 changes: 563 additions & 0 deletions data/shaders/custom_pbr.frag

Large diffs are not rendered by default.

107 changes: 107 additions & 0 deletions data/shaders/custom_pbr.vert
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
#version 450
#extension GL_ARB_separate_shader_objects : enable

#pragma import_defines (VSG_INSTANCE_POSITIONS, VSG_BILLBOARD, VSG_DISPLACEMENT_MAP)

layout(push_constant) uniform PushConstants {
mat4 projection;
mat4 modelView;
} pc;

#ifdef VSG_DISPLACEMENT_MAP
layout(binding = 6) uniform sampler2D displacementMap;
#endif

layout(location = 0) in vec3 vsg_Vertex;
layout(location = 1) in vec3 vsg_Normal;
layout(location = 2) in vec2 vsg_TexCoord0;
layout(location = 3) in vec4 vsg_Color;


#ifdef VSG_BILLBOARD
layout(location = 4) in vec4 vsg_position_scaleDistance;
#elif defined(VSG_INSTANCE_POSITIONS)
layout(location = 4) in vec3 vsg_position;
#endif

layout(location = 0) out vec3 eyePos;
layout(location = 1) out vec3 normalDir;
layout(location = 2) out vec4 vertexColor;
layout(location = 3) out vec2 texCoord0;

layout(location = 5) out vec3 viewDir;

out gl_PerVertex{ vec4 gl_Position; };

#ifdef VSG_BILLBOARD
mat4 computeBillboadMatrix(vec4 center_eye, float autoScaleDistance)
{
float distance = -center_eye.z;

float scale = (distance < autoScaleDistance) ? distance/autoScaleDistance : 1.0;
mat4 S = mat4(scale, 0.0, 0.0, 0.0,
0.0, scale, 0.0, 0.0,
0.0, 0.0, scale, 0.0,
0.0, 0.0, 0.0, 1.0);

mat4 T = mat4(1.0, 0.0, 0.0, 0.0,
0.0, 1.0, 0.0, 0.0,
0.0, 0.0, 1.0, 0.0,
center_eye.x, center_eye.y, center_eye.z, 1.0);
return T*S;
}
#endif

void main()
{
vec4 vertex = vec4(vsg_Vertex, 1.0);
vec4 normal = vec4(vsg_Normal, 0.0);

#ifdef VSG_DISPLACEMENT_MAP
// TODO need to pass as as uniform or per instance attributes
vec3 scale = vec3(1.0, 1.0, 1.0);

vertex.xyz = vertex.xyz + vsg_Normal * (texture(displacementMap, vsg_TexCoord0.st).s * scale.z);

float s_delta = 0.01;
float width = 0.0;

float s_left = max(vsg_TexCoord0.s - s_delta, 0.0);
float s_right = min(vsg_TexCoord0.s + s_delta, 1.0);
float t_center = vsg_TexCoord0.t;
float delta_left_right = (s_right - s_left) * scale.x;
float dz_left_right = (texture(displacementMap, vec2(s_right, t_center)).s - texture(displacementMap, vec2(s_left, t_center)).s) * scale.z;

// TODO need to handle different origins of displacementMap vs diffuseMap etc,
float t_delta = s_delta;
float t_bottom = max(vsg_TexCoord0.t - t_delta, 0.0);
float t_top = min(vsg_TexCoord0.t + t_delta, 1.0);
float s_center = vsg_TexCoord0.s;
float delta_bottom_top = (t_top - t_bottom) * scale.y;
float dz_bottom_top = (texture(displacementMap, vec2(s_center, t_top)).s - texture(displacementMap, vec2(s_center, t_bottom)).s) * scale.z;

vec3 dx = normalize(vec3(delta_left_right, 0.0, dz_left_right));
vec3 dy = normalize(vec3(0.0, delta_bottom_top, -dz_bottom_top));
vec3 dz = normalize(cross(dx, dy));

normal.xyz = normalize(dx * vsg_Normal.x + dy * vsg_Normal.y + dz * vsg_Normal.z);
#endif

#ifdef VSG_INSTANCE_POSITIONS
vertex.xyz = vertex.xyz + vsg_position;
#endif

#ifdef VSG_BILLBOARD
mat4 mv = computeBillboadMatrix(pc.modelView * vec4(vsg_position_scaleDistance.xyz, 1.0), vsg_position_scaleDistance.w);
#else
mat4 mv = pc.modelView;
#endif

gl_Position = (pc.projection * mv) * vertex;
eyePos = (mv * vertex).xyz;
viewDir = - (mv * vertex).xyz;
normalDir = (mv * normal).xyz;

vertexColor = vsg_Color;
texCoord0 = vsg_TexCoord0;
}
18 changes: 10 additions & 8 deletions data/shaders/standard_flat_shaded.frag
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,14 @@
#extension GL_ARB_separate_shader_objects : enable
#pragma import_defines (VSG_POINT_SPRITE, VSG_DIFFUSE_MAP, VSG_GREYSCALE_DIFFUSE_MAP)

#ifdef VSG_DIFFUSE_MAP
layout(binding = 0) uniform sampler2D diffuseMap;
#endif

layout(location = 2) in vec4 vertexColor;
#define VIEW_DESCRIPTOR_SET 0
#define MATERIAL_DESCRIPTOR_SET 1

#ifndef VSG_POINT_SPRITE
layout(location = 3) in vec2 texCoord0;
#ifdef VSG_DIFFUSE_MAP
layout(set = MATERIAL_DESCRIPTOR_SET, binding = 0) uniform sampler2D diffuseMap;
#endif

layout(binding = 10) uniform MaterialData
layout(set = MATERIAL_DESCRIPTOR_SET, binding = 10) uniform MaterialData
{
vec4 ambientColor;
vec4 diffuseColor;
Expand All @@ -23,6 +20,11 @@ layout(binding = 10) uniform MaterialData
float alphaMaskCutoff;
} material;

layout(location = 2) in vec4 vertexColor;
#ifndef VSG_POINT_SPRITE
layout(location = 3) in vec2 texCoord0;
#endif

layout(location = 0) out vec4 outColor;

void main()
Expand Down
22 changes: 13 additions & 9 deletions data/shaders/standard_pbr.frag
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,40 @@
#extension GL_ARB_separate_shader_objects : enable
#pragma import_defines (VSG_DIFFUSE_MAP, VSG_GREYSCALE_DIFFUSE_MAP, VSG_EMISSIVE_MAP, VSG_LIGHTMAP_MAP, VSG_NORMAL_MAP, VSG_METALLROUGHNESS_MAP, VSG_SPECULAR_MAP, VSG_TWO_SIDED_LIGHTING, VSG_WORKFLOW_SPECGLOSS, SHADOWMAP_DEBUG)

#define VIEW_DESCRIPTOR_SET 0
#define MATERIAL_DESCRIPTOR_SET 1

const float PI = 3.14159265359;
const float RECIPROCAL_PI = 0.31830988618;
const float RECIPROCAL_PI2 = 0.15915494;
const float EPSILON = 1e-6;
const float c_MinRoughness = 0.04;

#ifdef VSG_DIFFUSE_MAP
layout(binding = 0) uniform sampler2D diffuseMap;
layout(set = MATERIAL_DESCRIPTOR_SET, binding = 0) uniform sampler2D diffuseMap;
#endif

#ifdef VSG_METALLROUGHNESS_MAP
layout(binding = 1) uniform sampler2D mrMap;
layout(set = MATERIAL_DESCRIPTOR_SET, binding = 1) uniform sampler2D mrMap;
#endif

#ifdef VSG_NORMAL_MAP
layout(binding = 2) uniform sampler2D normalMap;
layout(set = MATERIAL_DESCRIPTOR_SET, binding = 2) uniform sampler2D normalMap;
#endif

#ifdef VSG_LIGHTMAP_MAP
layout(binding = 3) uniform sampler2D aoMap;
layout(set = MATERIAL_DESCRIPTOR_SET, binding = 3) uniform sampler2D aoMap;
#endif

#ifdef VSG_EMISSIVE_MAP
layout(binding = 4) uniform sampler2D emissiveMap;
layout(set = MATERIAL_DESCRIPTOR_SET, binding = 4) uniform sampler2D emissiveMap;
#endif

#ifdef VSG_SPECULAR_MAP
layout(binding = 5) uniform sampler2D specularMap;
layout(set = MATERIAL_DESCRIPTOR_SET, binding = 5) uniform sampler2D specularMap;
#endif

layout(binding = 10) uniform PbrData
layout(set = MATERIAL_DESCRIPTOR_SET, binding = 10) uniform PbrData
{
vec4 baseColorFactor;
vec4 emissiveFactor;
Expand All @@ -44,12 +47,13 @@ layout(binding = 10) uniform PbrData
float alphaMaskCutoff;
} pbr;

layout(set = 1, binding = 0) uniform LightData
// ViewDependentState
layout(set = VIEW_DESCRIPTOR_SET, binding = 0) uniform LightData
{
vec4 values[2048];
} lightData;

layout(set = 1, binding = 2) uniform sampler2DArrayShadow shadowMaps;
layout(set = VIEW_DESCRIPTOR_SET, binding = 2) uniform sampler2DArrayShadow shadowMaps;

layout(location = 0) in vec3 eyePos;
layout(location = 1) in vec3 normalDir;
Expand Down
19 changes: 11 additions & 8 deletions data/shaders/standard_phong.frag
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,30 @@
#extension GL_ARB_separate_shader_objects : enable
#pragma import_defines (VSG_POINT_SPRITE, VSG_DIFFUSE_MAP, VSG_GREYSCALE_DIFFUSE_MAP, VSG_EMISSIVE_MAP, VSG_LIGHTMAP_MAP, VSG_NORMAL_MAP, VSG_SPECULAR_MAP, VSG_TWO_SIDED_LIGHTING, SHADOWMAP_DEBUG)

#define VIEW_DESCRIPTOR_SET 0
#define MATERIAL_DESCRIPTOR_SET 1

#ifdef VSG_DIFFUSE_MAP
layout(set = 0, binding = 0) uniform sampler2D diffuseMap;
layout(set = MATERIAL_DESCRIPTOR_SET, binding = 0) uniform sampler2D diffuseMap;
#endif

#ifdef VSG_NORMAL_MAP
layout(set = 0, binding = 2) uniform sampler2D normalMap;
layout(set = MATERIAL_DESCRIPTOR_SET, binding = 2) uniform sampler2D normalMap;
#endif

#ifdef VSG_LIGHTMAP_MAP
layout(set = 0, binding = 3) uniform sampler2D aoMap;
layout(set = MATERIAL_DESCRIPTOR_SET, binding = 3) uniform sampler2D aoMap;
#endif

#ifdef VSG_EMISSIVE_MAP
layout(set = 0, binding = 4) uniform sampler2D emissiveMap;
layout(set = MATERIAL_DESCRIPTOR_SET, binding = 4) uniform sampler2D emissiveMap;
#endif

#ifdef VSG_SPECULAR_MAP
layout(set = 0, binding = 5) uniform sampler2D specularMap;
layout(set = MATERIAL_DESCRIPTOR_SET, binding = 5) uniform sampler2D specularMap;
#endif

layout(set = 0, binding = 10) uniform MaterialData
layout(set = MATERIAL_DESCRIPTOR_SET, binding = 10) uniform MaterialData
{
vec4 ambientColor;
vec4 diffuseColor;
Expand All @@ -33,13 +36,13 @@ layout(set = 0, binding = 10) uniform MaterialData
float alphaMaskCutoff;
} material;

layout(set = 1, binding = 0) uniform LightData
layout(set = VIEW_DESCRIPTOR_SET, binding = 0) uniform LightData
{
vec4 values[2048];
} lightData;


layout(set = 1, binding = 2) uniform sampler2DArrayShadow shadowMaps;
layout(set = VIEW_DESCRIPTOR_SET, binding = 2) uniform sampler2DArrayShadow shadowMaps;

layout(location = 0) in vec3 eyePos;
layout(location = 1) in vec3 normalDir;
Expand Down
27 changes: 26 additions & 1 deletion examples/nodes/vsgshadow/vsgshadow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,8 @@ int main(int argc, char** argv)
if (arguments.read({"--fullscreen", "--fs"})) windowTraits->fullscreen = true;
if (arguments.read({"--window", "-w"}, windowTraits->width, windowTraits->height)) { windowTraits->fullscreen = false; }
if (arguments.read("--IMMEDIATE")) windowTraits->swapchainPreferences.presentMode = VK_PRESENT_MODE_IMMEDIATE_KHR;
if (arguments.read("--d32")) windowTraits->depthFormat = VK_FORMAT_D32_SFLOAT;
arguments.read("--samples", windowTraits->samples);
if (arguments.read({"-t", "--test"}))
{
windowTraits->swapchainPreferences.presentMode = VK_PRESENT_MODE_IMMEDIATE_KHR;
Expand Down Expand Up @@ -293,12 +295,28 @@ int main(int argc, char** argv)
}
}

auto direction = arguments.value(vsg::dvec3(0.0, 0.0, -1.0), "--direction");
auto insertCullNode = arguments.read("--cull");
auto inherit = arguments.read("--inherit");
auto direction = arguments.value(vsg::dvec3(0.0, 0.0, -1.0), "--direction");
auto location = arguments.value<vsg::dvec3>({0.0, 0.0, 0.0}, "--location");
auto scale = arguments.value<double>(1.0, "--scale");
double viewingDistance = scale;

vsg::ref_ptr<vsg::StateGroup> stateGroup;
if (inherit)
{
auto shaderSet = vsg::createPhongShaderSet(options);
auto layout = shaderSet->createPipelineLayout({}, {0, 1});

stateGroup = vsg::StateGroup::create();

uint32_t vds_set = 0;
stateGroup->add(vsg::BindViewDescriptorSets::create(VK_PIPELINE_BIND_POINT_GRAPHICS, layout, vds_set));

vsg::info("Added state to inherit ");
options->inheritedState = stateGroup->stateCommands;
}

vsg::ref_ptr<vsg::Node> scene;
if (arguments.read("--large"))
{
Expand All @@ -321,6 +339,13 @@ int main(int argc, char** argv)
scene = createTestScene(options, textureFile, requiresBase, insertCullNode);
}

if (stateGroup)
{
// if setup place the StateGroup at the root of the scene graph
stateGroup->addChild(scene);
scene = stateGroup;
}

// compute the bounds of the scene graph to help position camera
auto bounds = vsg::visit<vsg::ComputeBounds>(scene).bounds;
viewingDistance = vsg::length(bounds.max - bounds.min) * 2.0;
Expand Down
1 change: 1 addition & 0 deletions examples/utils/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ add_subdirectory(vsggraphicspipelineconfigurator)
add_subdirectory(vsgshaderset)
add_subdirectory(vsgintersection)
add_subdirectory(vsgstoragebuffer)
add_subdirectory(vsgcustomshaderset)
16 changes: 15 additions & 1 deletion examples/utils/vsgbuilder/vsgbuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ int main(int argc, char** argv)
bool sphere = arguments.read("--sphere");
bool heightfield = arguments.read("--hf");
bool billboard = arguments.read("--billboard");
bool inherit = arguments.read("--inherit");

if (!(box || sphere || cone || capsule || quad || cylinder || disk || heightfield))
{
Expand Down Expand Up @@ -177,6 +178,7 @@ int main(int argc, char** argv)
{
if (floatColors)
{
stateInfo.instance_colors_vec4 = true;
auto colors = vsg::vec4Array::create(numVertices);
geomInfo.colors = colors;
for (auto& c : *(colors))
Expand All @@ -186,6 +188,7 @@ int main(int argc, char** argv)
}
else
{
stateInfo.instance_colors_vec4 = false;
auto colors = vsg::ubvec4Array::create(numVertices);
geomInfo.colors = colors;
for (auto& c : *(colors))
Expand All @@ -198,9 +201,20 @@ int main(int argc, char** argv)

if (box)
{
scene->addChild(builder->createBox(geomInfo, stateInfo));
auto node = builder->createBox(geomInfo, stateInfo);
bound.add(geomInfo.position);
geomInfo.position += geomInfo.dx * 1.5f;

if (auto sg = node.cast<vsg::StateGroup>(); sg && inherit)
{
options->inheritedState = sg->stateCommands;
scene = sg;
}
else
{
scene->addChild(node);

}
}

if (sphere)
Expand Down
15 changes: 15 additions & 0 deletions examples/utils/vsgcustomshaderset/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
set(SOURCES
custom_pbr.cpp
vsgcustomshaderset.cpp
)

add_executable(vsgcustomshaderset ${SOURCES})

target_link_libraries(vsgcustomshaderset vsg::vsg)

if (vsgXchange_FOUND)
target_compile_definitions(vsgcustomshaderset PRIVATE vsgXchange_FOUND)
target_link_libraries(vsgcustomshaderset vsgXchange::vsgXchange)
endif()

install(TARGETS vsgcustomshaderset RUNTIME DESTINATION bin)
Loading