Skip to content

Commit

Permalink
Merge pull request #275 from vsg-dev/CustomShaderSet
Browse files Browse the repository at this point in the history
Added vsgcustomshaderset example and state inhertance testing to vsgbuilder and vsgshadow
  • Loading branch information
robertosfield authored Nov 13, 2023
2 parents d198e23 + 065414e commit ad3a5f6
Show file tree
Hide file tree
Showing 15 changed files with 1,108 additions and 47 deletions.
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

0 comments on commit ad3a5f6

Please sign in to comment.