Browse Source

Metallic support

Panagiotis Christopoulos Charitos 10 years ago
parent
commit
aaa8fd4075

+ 12 - 4
shaders/IsLp.frag.glsl

@@ -82,10 +82,17 @@ void main()
 	float roughness;
 	float subsurface;
 	float emission;
-
-	readGBuffer(
-		u_msRt0, u_msRt1, u_msRt2,
-		in_texCoord, diffCol, normal, specCol, roughness, subsurface, emission);
+	float metallic;
+
+	GbufferInfo gbuffer;
+	readGBuffer(u_msRt0, u_msRt1, u_msRt2, in_texCoord, gbuffer);
+	diffCol = gbuffer.diffuse;
+	specCol = gbuffer.specular;
+	normal = gbuffer.normal;
+	roughness = gbuffer.roughness;
+	metallic = gbuffer.metallic;
+	subsurface = gbuffer.subsurface;
+	emission = gbuffer.emission;
 
 	float a2 = pow(max(EPSILON, roughness), 2.0);
 
@@ -156,6 +163,7 @@ void main()
 			* (att * spot * max(subsurface, lambert * shadow));
 	}
 
+	//out_color = diffCol;
 #if 0
 	if(pointLightsCount == 0)
 	{

+ 9 - 2
shaders/MsCommonFrag.glsl

@@ -220,7 +220,14 @@ void writeRts(
 	in float emission,
 	in float metallic)
 {
-	writeGBuffer(diffColor, normal, specularColor, roughness, subsurface,
-		emission, metallic, out_msRt0, out_msRt1, out_msRt2);
+	GbufferInfo g;
+	g.diffuse = diffColor;
+	g.normal = normal;
+	g.specular = specularColor;
+	g.roughness = roughness;
+	g.subsurface = subsurface;
+	g.emission = emission;
+	g.metallic = metallic;
+	writeGBuffer(g, out_msRt0, out_msRt1, out_msRt2);
 }
 #endif

+ 47 - 59
shaders/Pack.glsl

@@ -26,7 +26,7 @@ vec3 unpackNormal(in vec2 enc)
 	vec3 normal;
 	normal.xy = g * nn.xy;
 	normal.z = g - 1.0;
-	return normal;
+	return normalize(normal);
 }
 
 #if GL_ES || __VERSION__ < 400
@@ -82,90 +82,78 @@ float packUnorm2ToUnorm1(in vec2 comp)
 // Unpack a single float to vec2. Does the oposite of packUnorm2ToUnorm1.
 vec2 unpackUnorm1ToUnorm2(in float c)
 {
+#if 1
 	float temp = c * (255.0 / 16.0);
 	float a = floor(temp);
 	float b = temp - a; // b = fract(temp)
 	return vec2(a, b) * vec2(1.0 / 15.0, 16.0 / 15.0);
+#else
+	uint temp = uint(c * 255.0);
+	uint a = temp >> 4;
+	uint b = temp & 0xF;
+	return vec2(a, b) / 15.0;
+#endif
 }
 
 // Max emission. Keep as low as possible
 const float MAX_EMISSION = 10.0;
 
+// G-Buffer structure
+struct GbufferInfo
+{
+	vec3 diffuse;
+	vec3 specular;
+	vec3 normal;
+	float roughness;
+	float metallic;
+	float subsurface;
+	float emission;
+};
+
 // Populate the G buffer
-void writeGBuffer(
-	in vec3 diffColor,
-	in vec3 normal,
-	in vec3 specColor,
-	in float roughness,
-	in float subsurface,
-	in float emission,
-	in float metallic,
+void writeGBuffer(in GbufferInfo g, 
 	out vec4 rt0,
 	out vec4 rt1,
 	out vec4 rt2)
 {
-	rt0 = vec4(diffColor, subsurface);
-	rt1 = vec4(packUnorm2ToUnorm1(specColor.rg), specColor.b, roughness,
-		emission / MAX_EMISSION);
-	rt2 = vec4(normal * 0.5 + 0.5, 0.0);
+	rt0 = vec4(g.diffuse, g.subsurface);
+	rt1 = vec4(g.specular, g.emission / MAX_EMISSION);
+	rt2 = vec4(packNormal(g.normal), g.roughness, g.metallic);
 }
 
-// Read from the G buffer
-void readGBuffer(
-	in sampler2D rt0,
-	in sampler2D rt1,
-	in sampler2D rt2,
-	in vec2 uv_,
-	out vec3 diffColor,
-	out vec3 normal,
-	out vec3 specColor,
-	out float roughness,
-	out float subsurface,
-	out float emission)
+// Read from G-buffer
+void readNormalRoughnessMetallicFromGBuffer(
+	in sampler2D rt2, in vec2 uv, inout GbufferInfo g)
 {
-	ivec2 uv = ivec2(gl_FragCoord.xy);
-
-	vec4 comp = texelFetch(rt0, uv, 0);
-	diffColor = comp.xyz;
-	subsurface = comp.w;
-
-	comp = texelFetch(rt1, uv, 0);
-	specColor = vec3(unpackUnorm1ToUnorm2(comp.x), comp.y);
-	roughness = comp.z;
-	emission = comp.w * MAX_EMISSION;
-
-	normal = texelFetch(rt2, uv, 0).xyz;
-	normal = normalize(normal * 2.0 - 1.0);
+	vec4 comp = texture(rt2, uv);
+	g.normal = unpackNormal(comp.xy);
+	g.roughness = comp.z;
+	g.metallic = comp.w;
 }
 
-// Rear roughness from G-Buffer
-void readRoughnessFromGBuffer(in sampler2D rt1, in vec2 uv, out float r)
+// Read from G-buffer
+void readNormalFromGBuffer(in sampler2D rt2,  in vec2 uv, out vec3 normal)
 {
-	r = texture(rt1, uv).z;
+	vec2 comp = texture(rt2, uv).rg;
+	normal = unpackNormal(comp);
 }
 
-// Read only normal from G buffer
-void readNormalFromGBuffer(
-	in sampler2D fai2,
+// Read from the G buffer
+void readGBuffer(in sampler2D rt0,
+	in sampler2D rt1,
+	in sampler2D rt2,
 	in vec2 uv,
-	out vec3 normal)
+	out GbufferInfo g)
 {
-	normal = normalize(texture(fai2, uv).rgb * 2.0 - 1.0);
-}
+	vec4 comp = texture(rt0, uv);
+	g.diffuse = comp.xyz;
+	g.subsurface = comp.w;
 
-// Read only normal and specular color from G buffer
-void readNormalSpecularColorFromGBuffer(
-	in sampler2D fai1,
-	in sampler2D fai2,
-	in vec2 uv,
-	out vec3 normal,
-	out vec3 specColor)
-{
-	normal = normalize(textureLod(fai2, uv, 0.0).rgb * 2.0 - 1.0);
+	comp = texture(rt1, uv);
+	g.specular = comp.xyz;
+	g.emission = comp.w * MAX_EMISSION;
 
-	vec2 tmp = textureLod(fai1, uv, 0.0).xy;
-	specColor.rg = unpackUnorm1ToUnorm2(tmp.x);
-	specColor.b = tmp.y;
+	readNormalRoughnessMetallicFromGBuffer(rt2, uv, g);
 }
 
 #endif

+ 8 - 12
shaders/Refl.frag.glsl

@@ -8,10 +8,9 @@
 
 // Common
 layout(TEX_BINDING(0, 0)) uniform sampler2D u_depthRt;
-layout(TEX_BINDING(0, 1)) uniform sampler2D u_msRt1;
-layout(TEX_BINDING(0, 2)) uniform sampler2D u_msRt2;
+layout(TEX_BINDING(0, 1)) uniform sampler2D u_msRt2;
 
-layout(std140, UBO_BINDING(0, 0)) uniform _blk0
+layout(std140, UBO_BINDING(0, 0)) uniform u0_
 {
 	vec4 u_projectionParams;
 	mat4 u_projectionMat;
@@ -49,15 +48,12 @@ void main()
 	posVSpace.xy =
 		(2.0 * in_texCoord - 1.0) * u_projectionParams.xy * posVSpace.z;
 
-	float roughness;
-	readRoughnessFromGBuffer(u_msRt1, in_texCoord, roughness);
-
-	vec3 normal;
-	readNormalFromGBuffer(u_msRt2, in_texCoord, normal);
+	GbufferInfo gbuffer;
+	readNormalRoughnessMetallicFromGBuffer(u_msRt2, in_texCoord, gbuffer);
 
 	// Compute relflection vector
 	vec3 eye = normalize(posVSpace);
-	vec3 r = reflect(eye, normal);
+	vec3 r = reflect(eye, gbuffer.normal);
 
 	//
 	// SSLR
@@ -66,7 +62,7 @@ void main()
 	float sslrContribution;
 
 	// Don't bother for very rough surfaces
-	if(roughness > SSLR_START_ROUGHNESS)
+	if(gbuffer.roughness > SSLR_START_ROUGHNESS)
 	{
 		sslrContribution = 1.0;
 		out_color = vec3(1.0, 0.0, 1.0);
@@ -83,10 +79,10 @@ void main()
 	// IR
 	//
 #if IR_ENABLED
-	float reflLod = float(IR_MIPMAP_COUNT) * roughness;
+	float reflLod = float(IR_MIPMAP_COUNT) * gbuffer.roughness;
 	vec3 imgRefl = doImageReflections(posVSpace, r, reflLod);
 	out_color = mix(imgRefl, out_color, sslrContribution);
 #endif
 
-	out_color *= (1.0 - roughness);
+	out_color *= gbuffer.metallic;
 }

+ 3 - 4
src/renderer/Ms.cpp

@@ -18,7 +18,7 @@ namespace anki
 const Array<PixelFormat, Ms::ATTACHMENT_COUNT> Ms::RT_PIXEL_FORMATS = {
 	{PixelFormat(ComponentFormat::R8G8B8A8, TransformFormat::UNORM),
 		PixelFormat(ComponentFormat::R8G8B8A8, TransformFormat::UNORM),
-		PixelFormat(ComponentFormat::R10G10B10A2, TransformFormat::UNORM)}};
+		PixelFormat(ComponentFormat::R8G8B8A8, TransformFormat::UNORM)}};
 
 const PixelFormat Ms::DEPTH_RT_PIXEL_FORMAT(
 	ComponentFormat::D24, TransformFormat::FLOAT);
@@ -55,7 +55,7 @@ Error Ms::createRt(U32 index, U32 samples)
 		RT_PIXEL_FORMATS[1],
 		samples,
 		SamplingFilter::NEAREST,
-		2,
+		1,
 		plane.m_rt1);
 
 	m_r->createRenderTarget(m_r->getWidth(),
@@ -63,7 +63,7 @@ Error Ms::createRt(U32 index, U32 samples)
 		RT_PIXEL_FORMATS[2],
 		samples,
 		SamplingFilter::NEAREST,
-		2,
+		3,
 		plane.m_rt2);
 
 	AttachmentLoadOperation loadop = AttachmentLoadOperation::DONT_CARE;
@@ -181,7 +181,6 @@ void Ms::generateMipmaps(CommandBufferPtr& cmdb)
 {
 	U planeId = (m_r->getSamples() == 1) ? 1 : 0;
 	cmdb->generateMipmaps(m_planes[planeId].m_depthRt);
-	cmdb->generateMipmaps(m_planes[planeId].m_rt1);
 	cmdb->generateMipmaps(m_planes[planeId].m_rt2);
 }
 

+ 1 - 4
src/renderer/Refl.cpp

@@ -135,12 +135,9 @@ Error Refl::init1stPass(const ConfigSet& config)
 	rcInit.m_textures[0].m_texture = m_r->getMs().getDepthRt();
 	rcInit.m_textures[0].m_sampler = gr.newInstance<Sampler>(sinit);
 
-	rcInit.m_textures[1].m_texture = m_r->getMs().getRt1();
+	rcInit.m_textures[1].m_texture = m_r->getMs().getRt2();
 	rcInit.m_textures[1].m_sampler = gr.newInstance<Sampler>(sinit);
 
-	rcInit.m_textures[2].m_texture = m_r->getMs().getRt2();
-	rcInit.m_textures[2].m_sampler = gr.newInstance<Sampler>(sinit);
-
 	if(m_sslrEnabled)
 	{
 		rcInit.m_textures[3].m_texture = m_r->getIs().getRt();

+ 1 - 1
src/renderer/Ssao.cpp

@@ -237,7 +237,6 @@ Error Ssao::initInternal(const ConfigSet& config)
 	sinit.m_minMagFilter = SamplingFilter::LINEAR;
 	sinit.m_mipmapFilter = SamplingFilter::NEAREST;
 	sinit.m_repeat = false;
-	sinit.m_minLod = 0.0;
 
 	rcinit.m_textures[0].m_texture = m_r->getMs().getDepthRt();
 	rcinit.m_textures[0].m_sampler = gr.newInstance<Sampler>(sinit);
@@ -246,6 +245,7 @@ Error Ssao::initInternal(const ConfigSet& config)
 	rcinit.m_textures[1].m_sampler = rcinit.m_textures[0].m_sampler;
 
 	rcinit.m_textures[2].m_texture = m_noiseTex;
+
 	rcinit.m_uniformBuffers[0].m_buffer = m_uniformsBuff;
 	m_rcFirst = gr.newInstance<ResourceGroup>(rcinit);