Skip to content

Commit eff2c0b

Browse files
author
Alin
committed
-added some point lights with light volume to the demo
-changed with point light shader
1 parent c7bcbbc commit eff2c0b

10 files changed

+333
-59
lines changed

example/ThreadedRenderingGL/Commands.cpp

-1
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,6 @@ namespace cmds
108108
glBindTexture(GL_TEXTURE_2D, cmd.skyboxSandTex);
109109
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
110110

111-
glDisable(GL_CULL_FACE);
112111
cmd.shader->enable();
113112
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
114113
cmd.shader->disable();

example/ThreadedRenderingGL/ThreadedRenderingGL.cpp

+184-30
Large diffs are not rendered by default.

example/ThreadedRenderingGL/ThreadedRenderingGL.h

+76-7
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ class ThreadedRenderingGL : public NvSampleAppGL
7474

7575
enum {
7676
MAX_ANIMATION_THREAD_COUNT = 8,
77+
MAX_LIGHTS_COUNT = 64,
7778
MAX_THREAD_COUNT = MAX_ANIMATION_THREAD_COUNT + 1,
7879
THREAD_STACK_SIZE = 8192U
7980
};
@@ -153,8 +154,16 @@ class ThreadedRenderingGL : public NvSampleAppGL
153154
nv::vec4f m_lightPosition;
154155
nv::vec4f m_lightAmbient;
155156
nv::vec4f m_lightDiffuse;
156-
float m_causticOffset;
157-
float m_causticTiling;
157+
union
158+
{
159+
float m_causticOffset;
160+
float m_linearAttenuation;
161+
};
162+
union
163+
{
164+
float m_causticTiling;
165+
float m_quadraticAttenuation;
166+
};
158167
};
159168

160169
/// Book-keeping structure holding data pertinent to a thread
@@ -245,6 +254,7 @@ class ThreadedRenderingGL : public NvSampleAppGL
245254
uint32_t setNumSchools(uint32_t numSchools);
246255
void updateSchoolTankSizes();
247256
uint32_t setAnimationThreadNum(uint32_t numThreads);
257+
uint32_t setNumLights(uint32_t numLights);
248258

249259
/// Updates the stats displayed by the UI
250260
void updateStats();
@@ -338,6 +348,7 @@ class ThreadedRenderingGL : public NvSampleAppGL
338348
NvGLSLProgram* m_shader_Skybox;
339349
NvGLSLProgram* m_shader_Fish;
340350
NvGLSLProgram* m_shader_DirectionalLight;
351+
NvGLSLProgram* m_shader_PointLight;
341352

342353
BRDF m_brdf;
343354

@@ -383,6 +394,11 @@ class ThreadedRenderingGL : public NvSampleAppGL
383394
GLuint m_lightingUBO_Id; // UBO Id
384395
GLint m_lightingUBO_Location; // Uniform Index
385396

397+
398+
LightingUBO m_lightsUBO_Data[MAX_LIGHTS_COUNT];
399+
std::vector<int> m_lightsSchoolIndex;
400+
GLuint m_lightsUBO_Id[MAX_LIGHTS_COUNT];
401+
386402
// define the volume that the fish will remain within
387403
static nv::vec3f ms_tankMin;
388404
static nv::vec3f ms_tankMax;
@@ -474,6 +490,7 @@ class ThreadedRenderingGL : public NvSampleAppGL
474490
typedef std::vector<School*> SchoolSet;
475491
SchoolSet m_schools;
476492
std::vector<uint32_t> m_schoolsDrawCount;
493+
std::vector<nv::vec3f> m_schoolsCentroid;
477494
uint32_t m_activeSchools;
478495

479496
// Scene wide and background shared textures
@@ -539,18 +556,21 @@ class ThreadedRenderingGL : public NvSampleAppGL
539556
, UIACTION_RESET_FISHPLOSION
540557
, UIACTION_RESET_FISHFIREWORKS
541558
, UIACTION_ANIMTHREADCOUNT
559+
, UIACTION_LIGHTSCOUNT
542560
, UIACTION_ANIMPAUSED
543561
, UIACTION_INSTCOUNT
544562
, UIACTION_BATCHSIZE
545563
, UIACTION_STATSTOGGLE
546564
, UIACTION_RENDERINGTECHNIQUE
547565
, UIACTION_TANKSIZE
566+
, UIACTION_UIBRDF
548567
};
549568

550569
uint32_t m_uiSchoolCount;
551570
uint32_t m_uiFishCount;
552571
uint32_t m_uiTankSize;
553572
bool m_bTankSizeChanged;
573+
uint32_t m_uiLightsCount;
554574
uint32_t m_uiThreadCount;
555575
uint32_t m_uiInstanceCount;
556576
uint32_t m_uiBatchSize;
@@ -679,8 +699,10 @@ class ThreadedRenderingGL : public NvSampleAppGL
679699

680700
glEnable(GL_DEPTH_TEST);
681701
glDepthMask(GL_TRUE);
682-
glEnable(GL_STENCIL_TEST);
702+
glDisable(GL_STENCIL_TEST);
683703
glDisable(GL_BLEND);
704+
glDisable(GL_CULL_FACE);
705+
glCullFace(GL_BACK);
684706
}
685707
};
686708

@@ -715,7 +737,7 @@ class ThreadedRenderingGL : public NvSampleAppGL
715737
}
716738
};
717739

718-
// Setups a new frame
740+
// Setups the deferred pass
719741
struct BeginDeferredCommand
720742
{
721743
static const cb::RenderContext::function_t kDispatchFunction;
@@ -728,10 +750,37 @@ class ThreadedRenderingGL : public NvSampleAppGL
728750

729751
glBindFramebuffer(GL_FRAMEBUFFER, cmd.mainFboId);
730752

731-
glClearDepth(1.0f);
732-
glClearStencil(0);
753+
glClear(GL_COLOR_BUFFER_BIT);
754+
755+
glDisable(GL_DEPTH_TEST);
756+
glDisable(GL_STENCIL_TEST);
757+
glDepthMask(GL_FALSE);
758+
}
759+
};
760+
761+
struct BeginPointLightPassCommand
762+
{
763+
static const cb::RenderContext::function_t kDispatchFunction;
764+
765+
uint32_t dummy;
766+
767+
static void execute(const void* data, cb::RenderContext* rc)
768+
{
769+
auto& cmd = *reinterpret_cast<const BeginPointLightPassCommand*>(data);
770+
771+
glEnable(GL_BLEND);
772+
glBlendEquation(GL_FUNC_ADD);
773+
glBlendFunc(GL_ONE, GL_ONE);
774+
775+
glEnable(GL_STENCIL_TEST);
776+
glDisable(GL_CULL_FACE);
777+
733778
glStencilMask(0xFF);
734-
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
779+
glClearStencil(0);
780+
781+
// we'll render the light volume faces once per light
782+
glStencilFunc(GL_EQUAL, 0, 0xFF);
783+
glStencilOp(GL_KEEP, GL_INCR, GL_INCR);
735784
}
736785
};
737786

@@ -754,6 +803,26 @@ class ThreadedRenderingGL : public NvSampleAppGL
754803
static void execute(const void* data, cb::RenderContext* rc);
755804
};
756805

806+
struct DrawPointLightCommand
807+
{
808+
static const cb::RenderContext::function_t kDispatchFunction;
809+
// hint that we dont care about ctr/dtr
810+
typedef void pod_hint_tag;
811+
812+
GLuint projUBO_Location;
813+
GLuint projUBO_Id;
814+
GLuint lightingUBO_Location;
815+
GLuint lightingUBO_Id;
816+
LightingUBO lightingUBO_Data;
817+
NvGLSLProgram* shader;
818+
GLuint texGBuffer[GBUFFER_COUNT];
819+
uint32_t brdf;
820+
821+
nv::matrix4f MVP;
822+
823+
static void execute(const void* data, cb::RenderContext* rc);
824+
};
825+
757826
GeometryCommandBuffer m_geometryCommands;
758827
DeferredCommandBuffer m_deferredCommands;
759828

example/ThreadedRenderingGL/ThreadedRenderingGL.vcxproj

+2-2
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@
211211
</ClCompile>
212212
<Link>
213213
<AdditionalOptions>/DEBUG /MACHINE:x86 /LARGEADDRESSAWARE /NOLOGO /OPT:REF /OPT:ICF /INCREMENTAL:NO</AdditionalOptions>
214-
<AdditionalDependencies>xinput9_1_0.lib;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;opengl32.lib;glew32sd.lib;glfw3d.lib;%(AdditionalDependencies)</AdditionalDependencies>
214+
<AdditionalDependencies>xinput9_1_0.lib;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;opengl32.lib;glew32sd.lib;glfw3d.lib;glu32.lib;%(AdditionalDependencies)</AdditionalDependencies>
215215
<OutputFile>$(OutDir)ThreadedRenderingGLD.exe</OutputFile>
216216
<AdditionalLibraryDirectories>./../GraphicsSamples/extensions/externals/lib/vs2015x86;./../GraphicsSamples/extensions/lib/vs2015x86;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
217217
<ProgramDatabaseFile>$(OutDir)/ThreadedRenderingGLD.exe.pdb</ProgramDatabaseFile>
@@ -450,7 +450,7 @@
450450
<ClInclude Include="VertexFormatBinder.h" />
451451
</ItemGroup>
452452
<ItemGroup>
453-
<None Include="assets\src_shaders\DirectionalLighting_FS.glsl" />
453+
<None Include="assets\src_shaders\Lighting_FS.glsl" />
454454
<None Include="assets\src_shaders\Lighting_VS.glsl" />
455455
<None Include="assets\src_shaders\groundplane_FS.glsl" />
456456
<None Include="assets\src_shaders\groundplane_VS.glsl" />

example/ThreadedRenderingGL/ThreadedRenderingGL.vcxproj.filters

+3-3
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,6 @@
8383
</ClInclude>
8484
</ItemGroup>
8585
<ItemGroup>
86-
<None Include="assets\src_shaders\DirectionalLighting_FS.glsl">
87-
<Filter>src_shaders</Filter>
88-
</None>
8986
<None Include="assets\src_shaders\Lighting_VS.glsl">
9087
<Filter>src_shaders</Filter>
9188
</None>
@@ -107,5 +104,8 @@
107104
<None Include="assets\src_shaders\groundplane_FS.glsl">
108105
<Filter>src_shaders</Filter>
109106
</None>
107+
<None Include="assets\src_shaders\Lighting_FS.glsl">
108+
<Filter>src_shaders</Filter>
109+
</None>
110110
</ItemGroup>
111111
</Project>

example/ThreadedRenderingGL/assets/src_shaders/DirectionalLighting_FS.glsl renamed to example/ThreadedRenderingGL/assets/src_shaders/Lighting_FS.glsl

+32-5
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,14 @@ layout(binding=0) uniform ProjBlock {
5151
layout(binding=1) uniform LightingBlock {
5252
vec4 u_vLightPosition;
5353
vec4 u_vLightAmbient;
54-
vec4 u_vLightDiffuse;
54+
vec4 u_vLightDiffuse;
55+
#ifdef USE_POINT_LIGHT
56+
float u_fLinearAttenuation;
57+
float u_fQuadraticAttenuation;
58+
#else
5559
float u_fCausticOffset;
5660
float u_fCausticTiling;
61+
#endif
5762
};
5863

5964
void main(void)
@@ -81,7 +86,14 @@ void main(void)
8186
if (normLength > 0.0)
8287
{
8388
vec3 normal = worldNormal / normLength;
84-
vec3 dirToLight = normalize(u_vLightPosition.xyz);
89+
#ifdef USE_POINT_LIGHT
90+
vec3 dirToLight = worldPos - u_vLightPosition.xyz;
91+
float distance = length(dirToLight);
92+
dirToLight = dirToLight / distance;
93+
#else
94+
// directional light
95+
vec3 dirToLight = normalize(u_vLightPosition.xyz);
96+
#endif
8597
vec3 lightAmbient = u_vLightAmbient.rgb;
8698
vec3 lightColor = u_vLightDiffuse.rgb;
8799
switch (uLightingModel)
@@ -95,11 +107,26 @@ void main(void)
95107
color += max(0.0, GGX(dirToLight, toEyeVector, normal, roughness, 0.6));
96108
break;
97109
}
110+
111+
#ifdef USE_POINT_LIGHT
112+
float Kc = 1.0;
113+
float Kl = u_fLinearAttenuation;
114+
float Kd = u_fQuadraticAttenuation;
115+
float attenuation = Kc + Kl * distance + Kd * distance * distance;
116+
attenuation = max(1.0, attenuation);
117+
color /= attenuation;
118+
#endif
119+
98120
}
99121
else
100122
{
101-
// No geometry rendered, so use just the color
102-
color = mtlColor;
123+
#ifdef USE_POINT_LIGHT
124+
color = vec3(0.0);
125+
#else
126+
// No geometry rendered, so use just the color
127+
color = mtlColor * u_vLightAmbient.rgb * 1.5;
128+
#endif
103129
}
104-
outColor = vec4(color, 1.0);
130+
131+
outColor = vec4(color, 1.0);
105132
}

example/ThreadedRenderingGL/assets/src_shaders/Lighting_FS_Shared.h

+7-7
Original file line numberDiff line numberDiff line change
@@ -44,17 +44,16 @@ vec3 LambertDiffuse(vec3 dirToLight, vec3 surfaceNormal, vec3 ambientColor, vec3
4444

4545
float GGX(vec3 L, vec3 V, vec3 N, float roughness, float F0)
4646
{
47-
48-
float alpha = roughness * roughness;
47+
float roughSqr = roughness * roughness;
4948
vec3 H = normalize(L - V);
5049
float dotLH = max(0.0, dot(L, H));
5150
float dotNH = max(0.0, dot(N, H));
5251
float dotNL = max(0.0, dot(N, L));
53-
float alphaSqr = alpha * alpha;
52+
float alphaSqr = roughSqr * roughSqr;
5453
float denom = dotNH * dotNH * (alphaSqr - 1.0) + 1.0;
5554
float D = alphaSqr / (3.141592653589793 * denom * denom);
5655
float F = F0 + (1.0 - F0) * pow(1.0 - dotLH, 5.0);
57-
float k = 0.5 * alpha;
56+
float k = 0.5 * roughSqr;
5857
float k2 = k * k;
5958
return dotNL * D * F / (dotLH*dotLH*(1.0 - k2) + k2);
6059
}
@@ -83,10 +82,11 @@ vec3 OrenNayar(vec3 dirToLight, vec3 dirToEye, vec3 surfaceNormal, vec3 lightCol
8382
vec3 projEye = normalize(dirToEye - (surfaceNormal * clamp(dot(dirToEye, surfaceNormal), 0.0, 1.0))); // approaches ||0,0,0|| ,
8483

8584
float gamma = dot(projEye, projLight);
86-
float roughSqr = roughness * roughness;
8785

88-
float A = 1.0 - (0.5 * (roughSqr / (roughSqr + 0.57)));
89-
float B = 0.45 * (roughSqr / (roughSqr + 0.09));
86+
float alphaSqr = 1.0 - roughness * roughness;
87+
88+
float A = 1.0 - (0.5 * (alphaSqr / (alphaSqr + 0.57)));
89+
float B = 0.45 * (alphaSqr / (alphaSqr + 0.09));
9090
float C = clamp(sin(alpha), 0.0, 1.0) * max(0.0, tan(beta));
9191

9292
float final = A + B * max(0.0, gamma) * C;

example/ThreadedRenderingGL/assets/src_shaders/Lighting_VS.glsl

+2-2
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,6 @@ uniform mat4 uModelViewMatrix;
4242

4343
void main(void)
4444
{
45-
outProjPos = inVertexPosition;
46-
gl_Position = uModelViewMatrix * inVertexPosition;
45+
outProjPos = uModelViewMatrix * inVertexPosition;
46+
gl_Position = outProjPos;
4747
}

example/ThreadedRenderingGL/assets/src_shaders/staticfish_FS.glsl

+2-2
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,8 @@ void main()
9393
outDiffuseRoughness.rgb += vec3(getCausticIntensity(v_vPosWorldSpace));
9494
float fog = fogIntensity(length(v_vPosEyeSpace.xyz));
9595
outDiffuseRoughness.rgb = mix(outDiffuseRoughness.rgb, g_fogColor, fog);
96-
outDiffuseRoughness.a = 0.5;
97-
96+
outDiffuseRoughness.a = 0.25;
97+
9898
outNormalDepth.xyz = normalize(v_vNormalWorldSpace);
9999
outNormalDepth.w = v_vPosEyeSpace.z / 100.0;
100100
}

example/license.txt

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
Copyright 2016 NVIDIA Corporation
2+
3+
BY DOWNLOADING THE SOFTWARE AND OTHER AVAILABLE MATERIALS, YOU ("DEVELOPER") AGREE TO BE BOUND BY THE FOLLOWING TERMS AND CONDITIONS
4+
5+
The materials available for download to Developers may include software in both sample source ("Source Code") and object code ("Object Code") versions, documentation ("Documentation"), certain art work ("Art Assets") and other materials (collectively, these materials referred to herein as "Materials"). Except as expressly indicated herein, all terms and conditions of this Agreement apply to all of the Materials.
6+
7+
Except as expressly set forth herein, NVIDIA owns all of the Materials and makes them available to Developer only under the terms and conditions set forth in this Agreement.
8+
9+
License: Subject to the terms of this Agreement, NVIDIA hereby grants to Developer a royalty-free, non-exclusive license to possess and to use the Materials. The following terms apply to the specified type of Material:
10+
11+
Source Code: Developer shall have the right to modify and create derivative works with the Source Code. Developer shall own any derivative works ("Derivatives") it creates to the Source Code, provided that Developer uses the Materials in accordance with the terms of this Agreement. Developer may distribute the Derivatives, provided that all NVIDIA copyright notices and trademarks are used properly and the Derivatives include the following statement: "This software contains source code provided by NVIDIA Corporation."
12+
13+
Object Code: Developer agrees not to disassemble, decompile or reverse engineer the Object Code versions of any of the Materials. Developer acknowledges that certain of the Materials provided in Object Code version may contain third party components that may be subject to restrictions, and expressly agrees not to attempt to modify or distribute such Materials without first receiving consent from NVIDIA.
14+
15+
Art Assets: Developer shall have the right to modify and create Derivatives of the Art Assets, but may not distribute any of the Art Assets or Derivatives created therefrom without NVIDIA�s prior written consent.
16+
17+
Government End Users: If you are acquiring the Software on behalf of any unit or agency of the United States Government, the following provisions apply. The Government agrees the Software and documentation were developed at private expense and are provided with �RESTRICTED RIGHTS�. Use, duplication, or disclosure by the Government is subject to restrictions as set forth in DFARS 227.7202-1(a) and 227.7202-3(a) (1995), DFARS 252.227-7013(c)(1)(ii) (Oct 1988), FAR 12.212(a)(1995), FAR 52.227-19, (June 1987) or FAR 52.227-14(ALT III) (June 1987),as amended from time to time. In the event that this License, or any part thereof, is deemed inconsistent with the minimum rights identified in the Restricted Rights provisions, the minimum rights shall prevail.
18+
No Other License. No rights or licenses are granted by NVIDIA under this License, expressly or by implication, with respect to any proprietary information or patent, copyright, trade secret or other intellectual property right owned or controlled by NVIDIA, except as expressly provided in this License.
19+
Term: This License is effective until terminated. NVIDIA may terminate this Agreement (and with it, all of Developer�s right to the Materials) immediately upon written notice (which may include email) to Developer, with or without cause.
20+
21+
Support: NVIDIA has no obligation to support or to continue providing or updating any of the Materials.
22+
23+
No Warranty: THE SOFTWARE AND ANY OTHER MATERIALS PROVIDED BY NVIDIA TO DEVELOPER HEREUNDER ARE PROVIDED "AS IS." NVIDIA DISCLAIMS ALL WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF TITLE, MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
24+
25+
LIMITATION OF LIABILITY: NVIDIA SHALL NOT BE LIABLE TO DEVELOPER, DEVELOPER�S CUSTOMERS, OR ANY OTHER PERSON OR ENTITY CLAIMING THROUGH OR UNDER DEVELOPER FOR ANY LOSS OF PROFITS, INCOME, SAVINGS, OR ANY OTHER CONSEQUENTIAL, INCIDENTAL, SPECIAL, PUNITIVE, DIRECT OR INDIRECT DAMAGES (WHETHER IN AN ACTION IN CONTRACT, TORT OR BASED ON A WARRANTY), EVEN IF NVIDIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. THESE LIMITATIONS SHALL APPLY NOTWITHSTANDING ANY FAILURE OF THE ESSENTIAL PURPOSE OF ANY LIMITED REMEDY. IN NO EVENT SHALL NVIDIA�S AGGREGATE LIABILITY TO DEVELOPER OR ANY OTHER PERSON OR ENTITY CLAIMING THROUGH OR UNDER DEVELOPER EXCEED THE AMOUNT OF MONEY ACTUALLY PAID BY DEVELOPER TO NVIDIA FOR THE SOFTWARE OR ANY OTHER MATERIALS.

0 commit comments

Comments
 (0)