Browse Source

Renderer: Forward shading is HDR-friendly now

Panagiotis Christopoulos Charitos 9 years ago
parent
commit
671345e86d

+ 4 - 5
shaders/FsCommonFrag.glsl

@@ -80,9 +80,9 @@ void particleSoftTextureAlpha(in sampler2D depthMap, in sampler2D tex, in float
 
 #if PASS == COLOR
 #define particleTextureAlpha_DEFINED
-void particleTextureAlpha(in sampler2D tex, in float alpha)
+void particleTextureAlpha(in sampler2D tex, vec4 mulColor, vec4 addColor, in float alpha)
 {
-	vec4 color = texture(tex, gl_PointCoord);
+	vec4 color = texture(tex, gl_PointCoord) * mulColor + addColor;
 	color.a *= alpha;
 
 	writeGBuffer(color);
@@ -220,10 +220,10 @@ void particleTextureAlphaLight(in sampler2D tex, in float alpha)
 void particleAnimatedTextureAlphaLight(sampler2DArray tex, float alpha, float layerCount, float period)
 {
 	vec4 color = readAnimatedTextureRgba(tex, layerCount, period, gl_PointCoord, anki_u_time);
-	color.a *= alpha;
 
 	color.rgb = computeLightColor(color.rgb);
 
+	color.a *= alpha;
 	writeGBuffer(color);
 }
 #endif
@@ -248,8 +248,7 @@ void fog(in sampler2D depthMap, in vec3 color, in float fogScale)
 	}
 	else
 	{
-		// The depth buffer is cleared at this place. Set the diff to zero to
-		// avoid weird pop ups
+		// The depth buffer is cleared at this place. Set the diff to zero to avoid weird pop ups
 		diff = 0.0;
 	}
 

+ 4 - 3
shaders/FsUpscale.frag.glsl

@@ -30,14 +30,15 @@ void main()
 	// Get the depth of the current fragment
 	vec3 color = nearestDepthUpscale(in_uv, u_depthFullTex, u_depthHalfTex, u_fsRt, DEPTH_THRESHOLD);
 #else
-	vec3 color =
+	vec4 color =
 		bilateralUpsample(u_depthFullTex, u_depthHalfTex, u_fsRt, 1.0 / vec2(SRC_SIZE), in_uv, u_linearizeCfPad2.xy);
 #endif
 
 #if SSAO_ENABLED
 	float ssao = textureLod(u_ssaoTex, in_uv, 0.0).r;
-	out_color = vec4(color, ssao);
+	out_color = color;
+	out_color.a *= ssao;
 #else
-	out_color = vec4(color, 1.0);
+	out_color = color;
 #endif
 }

+ 4 - 4
shaders/Functions.glsl

@@ -128,7 +128,7 @@ float _calcDepthWeight(sampler2D depthLow, vec2 uv, float ref, vec2 linearDepthC
 	return 1.0 / (EPSILON + abs(ref - linearD));
 }
 
-vec3 _sampleAndWeight(sampler2D depthLow,
+vec4 _sampleAndWeight(sampler2D depthLow,
 	sampler2D colorLow,
 	vec2 lowInvSize,
 	vec2 uv,
@@ -140,19 +140,19 @@ vec3 _sampleAndWeight(sampler2D depthLow,
 {
 	uv += offset * lowInvSize;
 	float dw = _calcDepthWeight(depthLow, uv, ref, linearDepthCf);
-	vec3 v = texture(colorLow, uv).rgb;
+	vec4 v = texture(colorLow, uv);
 	normalize += weight * dw;
 	return v * dw * weight;
 }
 
-vec3 bilateralUpsample(
+vec4 bilateralUpsample(
 	sampler2D depthHigh, sampler2D depthLow, sampler2D colorLow, vec2 lowInvSize, vec2 uv, vec2 linearDepthCf)
 {
 	const vec3 WEIGHTS = vec3(0.25, 0.125, 0.0625);
 	float depthRef = linearizeDepthOptimal(texture(depthHigh, uv).r, linearDepthCf.x, linearDepthCf.y);
 	float normalize = 0.0;
 
-	vec3 sum = _sampleAndWeight(
+	vec4 sum = _sampleAndWeight(
 		depthLow, colorLow, lowInvSize, uv, vec2(0.0, 0.0), depthRef, WEIGHTS.x, linearDepthCf, normalize);
 	sum += _sampleAndWeight(
 		depthLow, colorLow, lowInvSize, uv, vec2(-1.0, 0.0), depthRef, WEIGHTS.y, linearDepthCf, normalize);

+ 3 - 5
shaders/LfSpritePass.frag.glsl

@@ -12,12 +12,10 @@ layout(ANKI_TEX_BINDING(0, 0)) uniform sampler2DArray u_tex;
 layout(location = 0) in vec3 in_uv;
 layout(location = 1) flat in vec4 in_color;
 
-layout(location = 0) out vec3 outColor;
+layout(location = 0) out vec4 out_color;
 
 void main()
 {
-	vec3 col = texture(u_tex, in_uv).rgb;
-
-	outColor = col * in_color.rgb;
-	outColor *= in_color.a;
+	vec4 col = texture(u_tex, in_uv);
+	out_color = col * in_color;
 }

+ 1 - 1
shaders/Ssao.frag.glsl

@@ -13,7 +13,7 @@ const vec3 KERNEL[KERNEL_SIZE] = KERNEL_ARRAY; // This will be appended in C++
 const float RANGE_CHECK_RADIUS = RADIUS * 2.0;
 
 // Initial is 1.0 but the bigger it is the more darker the SSAO factor gets
-const float DARKNESS_MULTIPLIER = 1.5;
+const float DARKNESS_MULTIPLIER = 1.9;
 
 // The algorithm will chose the number of samples depending on the distance
 const float MAX_DISTANCE = 40.0;

+ 1 - 1
shaders/VolumetricUpscale.frag.glsl

@@ -25,6 +25,6 @@ void main()
 	out_color = nearestDepthUpscale(in_uv, u_depthFullTex, u_depthHalfTex, u_colorTex, DEPTH_THRESHOLD);
 #else
 	out_color =
-		bilateralUpsample(u_depthFullTex, u_depthHalfTex, u_colorTex, 1.0 / SRC_SIZE, in_uv, u_linearizeCfPad2.xy);
+		bilateralUpsample(u_depthFullTex, u_depthHalfTex, u_colorTex, 1.0 / SRC_SIZE, in_uv, u_linearizeCfPad2.xy).rgb;
 #endif
 }

+ 3 - 2
src/anki/gr/gl/StateTracker.h

@@ -386,10 +386,11 @@ public:
 	{
 		auto& att = m_colorAtt[attidx];
 
-		Bool wantBlend = !(att.m_blendSrcFactorRgb == BlendFactor::ONE && att.m_blendDstFactorRgb == BlendFactor::ZERO)
-			&& !(att.m_blendSrcFactorA == BlendFactor::ONE && att.m_blendDstFactorA == BlendFactor::ZERO)
+		Bool dontWantBlend = att.m_blendSrcFactorRgb == BlendFactor::ONE && att.m_blendDstFactorRgb == BlendFactor::ZERO
+			&& att.m_blendSrcFactorA == BlendFactor::ONE && att.m_blendDstFactorA == BlendFactor::ZERO
 			&& (att.m_blendOpRgb == BlendOperation::ADD || att.m_blendOpRgb == BlendOperation::SUBTRACT)
 			&& (att.m_blendOpA == BlendOperation::ADD || att.m_blendOpA == BlendOperation::SUBTRACT);
+		Bool wantBlend = !dontWantBlend;
 
 		if(wantBlend != att.m_enableBlend)
 		{

+ 16 - 0
src/anki/gr/gl/TextureImpl.cpp

@@ -157,6 +157,22 @@ static void convertTextureInformation(const PixelFormat& pf,
 			ANKI_ASSERT(0 && "TODO");
 		}
 		break;
+	case ComponentFormat::R16G16B16A16:
+		if(pf.m_transform == TransformFormat::FLOAT)
+		{
+			format = GL_RGBA;
+			internalFormat = GL_RGBA16F;
+			type = GL_FLOAT;
+		}
+		else if(pf.m_transform == TransformFormat::UINT)
+		{
+			ANKI_ASSERT(!"TODO");
+		}
+		else
+		{
+			ANKI_ASSERT(!"TODO");
+		}
+		break;
 	case ComponentFormat::R32G32B32:
 		if(pf.m_transform == TransformFormat::FLOAT)
 		{

+ 2 - 0
src/anki/renderer/Common.h

@@ -68,6 +68,8 @@ const PixelFormat MS_DEPTH_ATTACHMENT_PIXEL_FORMAT(ComponentFormat::D24S8, Trans
 
 const PixelFormat IS_COLOR_ATTACHMENT_PIXEL_FORMAT(ComponentFormat::R11G11B10, TransformFormat::FLOAT);
 
+const PixelFormat FS_COLOR_ATTACHMENT_PIXEL_FORMAT(ComponentFormat::R16G16B16A16, TransformFormat::FLOAT);
+
 const PixelFormat DBG_COLOR_ATTACHMENT_PIXEL_FORMAT(ComponentFormat::R8G8B8, TransformFormat::UNORM);
 /// @}
 

+ 5 - 2
src/anki/renderer/Fs.cpp

@@ -41,7 +41,7 @@ Error Fs::initInternal(const ConfigSet&)
 	// Create RT
 	m_r->createRenderTarget(m_width,
 		m_height,
-		IS_COLOR_ATTACHMENT_PIXEL_FORMAT,
+		FS_COLOR_ATTACHMENT_PIXEL_FORMAT,
 		TextureUsageBit::SAMPLED_FRAGMENT | TextureUsageBit::FRAMEBUFFER_ATTACHMENT_READ_WRITE,
 		SamplingFilter::LINEAR,
 		1,
@@ -51,6 +51,7 @@ Error Fs::initInternal(const ConfigSet&)
 	fbInit.m_colorAttachmentCount = 1;
 	fbInit.m_colorAttachments[0].m_texture = m_rt;
 	fbInit.m_colorAttachments[0].m_loadOperation = AttachmentLoadOperation::CLEAR;
+	fbInit.m_colorAttachments[0].m_clearValue.m_colorf = {{0.0, 0.0, 0.0, 1.0}};
 	fbInit.m_depthStencilAttachment.m_texture = m_r->getDepthDownscale().m_hd.m_depthRt;
 	fbInit.m_depthStencilAttachment.m_loadOperation = AttachmentLoadOperation::LOAD;
 	fbInit.m_depthStencilAttachment.m_aspect = DepthStencilAspectBit::DEPTH;
@@ -139,7 +140,9 @@ Error Fs::buildCommandBuffers(RenderingContext& ctx, U threadId, U threadCount)
 	cmdb->bindStorageBuffer(1, 1, ctx.m_is.m_lightIndicesToken);
 
 	cmdb->setViewport(0, 0, m_width, m_height);
-	cmdb->setBlendFactors(0, BlendFactor::SRC_ALPHA, BlendFactor::ONE_MINUS_SRC_ALPHA);
+	cmdb->setBlendFactors(
+		0, BlendFactor::SRC_ALPHA, BlendFactor::ONE_MINUS_SRC_ALPHA, BlendFactor::DST_ALPHA, BlendFactor::ONE);
+	cmdb->setBlendOperation(0, BlendOperation::ADD, BlendOperation::REVERSE_SUBTRACT);
 	cmdb->setDepthWrite(false);
 
 	// Start drawing

+ 4 - 1
src/anki/renderer/Lf.cpp

@@ -242,7 +242,9 @@ void Lf::run(RenderingContext& ctx, CommandBufferPtr cmdb)
 	cmdb->bindShaderProgram(m_realProg);
 	cmdb->setDepthWrite(false);
 	cmdb->setDepthCompareOperation(CompareOperation::ALWAYS);
-	cmdb->setBlendFactors(0, BlendFactor::ONE, BlendFactor::ONE);
+	cmdb->setBlendFactors(
+		0, BlendFactor::SRC_ALPHA, BlendFactor::ONE_MINUS_SRC_ALPHA, BlendFactor::DST_ALPHA, BlendFactor::ONE);
+	cmdb->setBlendOperation(0, BlendOperation::ADD, BlendOperation::REVERSE_SUBTRACT);
 
 	for(U i = 0; i < count; ++i)
 	{
@@ -293,6 +295,7 @@ void Lf::run(RenderingContext& ctx, CommandBufferPtr cmdb)
 	cmdb->setDepthWrite(true);
 	cmdb->setDepthCompareOperation(CompareOperation::LESS);
 	cmdb->setBlendFactors(0, BlendFactor::ONE, BlendFactor::ZERO);
+	cmdb->setBlendOperation(0, BlendOperation::ADD);
 }
 
 } // end namespace anki

+ 2 - 1
src/anki/scene/ParticleEmitter.cpp

@@ -424,12 +424,13 @@ Error ParticleEmitter::frameUpdate(F32 prevUpdateTime, F32 crntTime)
 			// Set alpha
 			if(m_particle.m_alphaAnimation)
 			{
-				verts[4] = sin((lifePercent)*PI) * p->m_alpha;
+				verts[4] = sin(lifePercent * PI) * p->m_alpha;
 			}
 			else
 			{
 				verts[4] = p->m_alpha;
 			}
+			verts[4] = clamp(verts[4], 0.0f, 1.0f);
 
 			++m_aliveParticlesCount;
 			verts += 5;

+ 1 - 2
src/anki/scene/Visibility.cpp

@@ -485,10 +485,9 @@ void CombineResultsTask::combine()
 		visible->getEnd(VisibilityGroupType::RENDERABLES_MS),
 		comp);
 
-	// TODO: Reverse the sort
 	std::sort(visible->getBegin(VisibilityGroupType::RENDERABLES_FS),
 		visible->getEnd(VisibilityGroupType::RENDERABLES_FS),
-		comp);
+		RevDistanceSortFunctor());
 
 	std::sort(visible->getBegin(VisibilityGroupType::REFLECTION_PROBES),
 		visible->getEnd(VisibilityGroupType::REFLECTION_PROBES),

+ 10 - 0
src/anki/scene/VisibilityInternal.h

@@ -33,6 +33,16 @@ public:
 	}
 };
 
+class RevDistanceSortFunctor
+{
+public:
+	Bool operator()(const VisibleNode& a, const VisibleNode& b)
+	{
+		ANKI_ASSERT(a.m_node && b.m_node);
+		return a.m_frustumDistanceSquared > b.m_frustumDistanceSquared;
+	}
+};
+
 /// Sort renderable scene nodes on material
 class MaterialSortFunctor
 {