X-Git-Url: http://review.tizen.org/git/?a=blobdiff_plain;f=examples%2Fdeferred-shading%2Fdeferred-shading.cpp;h=28cf835f74fa74d3ad7a24afd9c75e2428426d1a;hb=422d91612187d1a575b3ed00f7d33cb2d19e02ed;hp=02d6ef4d8a696fa1e6c490f21425c4cbeadad5ee;hpb=39b38842b9558a3615accb8808888d369735428a;p=platform%2Fcore%2Fuifw%2Fdali-demo.git diff --git a/examples/deferred-shading/deferred-shading.cpp b/examples/deferred-shading/deferred-shading.cpp index 02d6ef4..28cf835 100644 --- a/examples/deferred-shading/deferred-shading.cpp +++ b/examples/deferred-shading/deferred-shading.cpp @@ -21,6 +21,11 @@ #include "dali/public-api/actors/actor.h" #include "dali/public-api/rendering/renderer.h" +#include "generated/deferred-shading-mainpass-vert.h" +#include "generated/deferred-shading-mainpass-frag.h" +#include "generated/deferred-shading-prepass-vert.h" +#include "generated/deferred-shading-prepass-frag.h" + using namespace Dali; namespace @@ -38,181 +43,6 @@ namespace #define DEFINE_MAX_LIGHTS "const int kMaxLights = " QUOTE(MAX_LIGHTS) ";" -#define DEFINE(x) "#define " DALI_COMPOSE_SHADER(x) DALI_COMPOSE_SHADER(\n) - -// clang-format off - -//============================================================================= -// PRE-PASS -//============================================================================= -const char* const PREPASS_VSH = DALI_COMPOSE_SHADER(#version 300 es\n -precision mediump float;) - DALI_COMPOSE_SHADER( - -// DALI uniforms -uniform mat4 uMvpMatrix; -uniform mat3 uNormalMatrix; -uniform vec3 uSize; - -uniform vec3 uDepth_InvDepth_Near;\n) - DEFINE(DEPTH uDepth_InvDepth_Near.x) - DEFINE(INV_DEPTH uDepth_InvDepth_Near.y) - DEFINE(NEAR uDepth_InvDepth_Near.z) - DALI_COMPOSE_SHADER( - -in vec3 aPosition; -in vec3 aNormal; - -out vec4 vPosition; -out vec3 vNormal; - -vec4 Map(vec4 v) // projection space -> texture -{ - return vec4(v.xyz / (2.f * v.w) + vec3(.5f), (v.w - NEAR) * INV_DEPTH); -} - -void main() -{ - vec4 position = uMvpMatrix * vec4(aPosition * uSize, 1.f); - vPosition = Map(position); - gl_Position = position; - - vNormal = normalize(uNormalMatrix * aNormal); -}); - -//============================================================================= -const char* const PREPASS_FSH = DALI_COMPOSE_SHADER(#version 300 es\n -precision mediump float; - -// DALI uniform -uniform vec4 uColor; - -in vec4 vPosition; -in vec3 vNormal; - -// These are our outputs. -layout(location = 0) out vec3 oNormal; -layout(location = 1) out vec4 oPosition; -layout(location = 2) out vec3 oColor; - -void main() -{ - oColor = uColor.rgb; - oPosition = vPosition; - oNormal = normalize(vNormal) * .5f + .5f; -}); - -//============================================================================= -// MAIN (LIGHTING) PASS -//============================================================================= -const char* const MAINPASS_VSH = DALI_COMPOSE_SHADER(#version 300 es\n -precision mediump float; - -// DALI uniforms -uniform mat4 uMvpMatrix; -uniform vec3 uSize; - -in vec3 aPosition; -in vec2 aTexCoord; - -out vec2 vUv; - -void main() -{ - vec4 position = uMvpMatrix * vec4(aPosition * uSize, 1.f); - vUv = aTexCoord; - - gl_Position = position; -}); - -//============================================================================= -const char* const MAINPASS_FSH = DALI_COMPOSE_SHADER(#version 300 es\n -precision mediump float;\n) - DEFINE_MAX_LIGHTS - DALI_COMPOSE_SHADER( - -const float kAttenuationConst = .05f; -const float kAttenuationLinear = .1f; -const float kAttenuationQuadratic = .15f; - -// G-buffer -uniform sampler2D uTextureNormal; -uniform sampler2D uTexturePosition; -uniform sampler2D uTextureColor; - -uniform mat4 uInvProjection; - -uniform vec3 uDepth_InvDepth_Near;\n) - DEFINE(DEPTH uDepth_InvDepth_Near.x) - DEFINE(INV_DEPTH uDepth_InvDepth_Near.y) - DEFINE(NEAR uDepth_InvDepth_Near.z) - DALI_COMPOSE_SHADER( - -// Light source uniforms -struct Light -{ - vec3 position; // view space - float radius; - vec3 color; -}; - -uniform Light uLights[kMaxLights]; - -in vec2 vUv; - -out vec4 oColor; - -vec4 Unmap(vec4 m) // texture -> projection -{ - m.w = m.w * DEPTH + NEAR; - m.xyz = (m.xyz - vec3(.5)) * (2.f * m.w); - return m; -} - -vec3 CalculateLighting(vec3 pos, vec3 normal) -{ - vec3 viewDir = normalize(pos); - vec3 viewDirRefl = -reflect(viewDir, normal); - - vec3 light = vec3(0.04f); // fake ambient term - for (int i = 0; i < kMaxLights; ++i) - { - vec3 rel = pos - uLights[i].position; - float distance = length(rel); - rel /= distance; - - float a = uLights[i].radius / (kAttenuationConst + kAttenuationLinear * distance + - kAttenuationQuadratic * distance * distance); // attenuation - - float l = max(0.f, dot(normal, rel)); // lambertian - float s = pow(max(0.f, dot(viewDirRefl, rel)), 256.f); // specular - - light += (uLights[i].color * (l + s)) * a; - } - - return light; -} - -void main() -{ - vec3 normSample = texture(uTextureNormal, vUv).xyz; - if (dot(normSample, normSample) == 0.f) - { - discard; // if we didn't write this texel, don't bother lighting it. - } - - vec3 normal = normalize(normSample - .5f); - - vec4 posSample = texture(uTexturePosition, vUv); - vec3 pos = (uInvProjection * Unmap(posSample)).xyz; - - vec3 color = texture(uTextureColor, vUv).rgb; - vec3 finalColor = color * CalculateLighting(pos, normal); - - oColor = vec4(finalColor, 1.f); -}); -// clang-format on - //============================================================================= // PRNG for floats. struct FloatRand @@ -542,7 +372,7 @@ private: Geometry mesh = CreateOctahedron(false); // Create main actors - Shader preShader = Shader::New(PREPASS_VSH, PREPASS_FSH); + Shader preShader = Shader::New(SHADER_DEFERRED_SHADING_PREPASS_VERT, SHADER_DEFERRED_SHADING_PREPASS_FRAG); TextureSet noTexturesThanks = TextureSet::New(); Renderer meshRenderer = CreateRenderer(noTexturesThanks, mesh, preShader, OPTION_DEPTH_TEST | OPTION_DEPTH_WRITE); meshRenderer.SetProperty(Renderer::Property::FACE_CULLING_MODE, FaceCullingMode::BACK); @@ -631,7 +461,7 @@ private: finalImageTextures.SetSampler(1, sampler); finalImageTextures.SetSampler(2, sampler); - Shader shdMain = Shader::New(MAINPASS_VSH, MAINPASS_FSH); + Shader shdMain = Shader::New(SHADER_DEFERRED_SHADING_MAINPASS_VERT, SHADER_DEFERRED_SHADING_MAINPASS_FRAG); Geometry finalImageGeom = CreateTexturedQuadGeometry(true); Renderer finalImageRenderer = CreateRenderer(finalImageTextures, finalImageGeom, shdMain); RegisterDepthProperties(depth, zNear, finalImageRenderer);