Horde3D

Next-Generation Graphics Engine
It is currently 19.03.2024, 09:07

All times are UTC + 1 hour




Post new topic Reply to topic  [ 18 posts ]  Go to page 1, 2  Next
Author Message
 Post subject: Lightshaft shader
PostPosted: 03.06.2011, 20:17 
Offline

Joined: 05.04.2011, 18:53
Posts: 6
Hi!

I am currently working on a Lightshaft shader made with horde3D. It looks like this:

Image

The code is not final, but I still put a preliminary version of it on my blog at http://probesys.blogspot.com/

Does anyone have made a similar shader or have any comments or improvement suggestion?


Top
 Profile  
Reply with quote  
 Post subject: Re: Lightshaft shader
PostPosted: 06.06.2011, 02:16 
Offline

Joined: 05.03.2007, 19:38
Posts: 167
Location: Romania
I don't have any technical advice, I just wanted to say that it is nice to see this being done with Horde3D :)

_________________
Paul


Top
 Profile  
Reply with quote  
 Post subject: Re: Lightshaft shader
PostPosted: 06.06.2011, 03:11 
Offline

Joined: 08.11.2006, 03:10
Posts: 384
Location: Australia
Very nice effect -- but I'm guessing that with 100 samples per pixel it's probably quite expensive?

Perhaps if you down-sample the input texture you can get away with using less samples.


Top
 Profile  
Reply with quote  
 Post subject: Re: Lightshaft shader
PostPosted: 16.06.2011, 16:47 
Offline

Joined: 05.04.2011, 18:53
Posts: 6
Thanks guys for your comments! Yes you're right DarkAngel, 100 samples is quite fill-rate intensive. I did a few experiments with less samples, like 50 or 30, and it creates some artifacts, that are even more noticeable when the lightshafts are moving. But as you mentionned, using smaller buffers helps because it blurs most the artifacts. I am still working on it to get the best quality/performance ratio, but I will post my final code if some people are interested. As of now the performances are 310 fps with 100 sample, 500 fps with 50 samples, and 665 fps with 30 samples all in 1024x768 with my GTX 470.

Thanks DarkAngel for your very good advices!


Top
 Profile  
Reply with quote  
 Post subject: Re: Lightshaft shader
PostPosted: 17.06.2011, 13:47 
Offline

Joined: 23.07.2009, 21:03
Posts: 51
Location: Germany
probesys wrote:
... but I will post my final code if some people are interested.


Im interested :D


Top
 Profile  
Reply with quote  
 Post subject: Re: Lightshaft shader
PostPosted: 14.10.2011, 22:26 
Offline

Joined: 08.06.2010, 14:17
Posts: 63
I tried to make similar shader, but mine only uses 20 samples. It does two passes, first one works similar to yours, except only using 10 samples, writes result to a half resolution buffer (not necessary, but speeds up things a bit and looks almost identical), and second pass is just a simple 10-sample radial blur. In the end it looks more or less same as one 100-sample pass.
Video


Top
 Profile  
Reply with quote  
 Post subject: Re: Lightshaft shader
PostPosted: 20.10.2011, 19:30 
Offline
Engine Developer

Joined: 10.09.2006, 15:52
Posts: 1217
Great work, it's nice to see how your project progresses! I especially like your lit particles...


Top
 Profile  
Reply with quote  
 Post subject: Re: Lightshaft shader
PostPosted: 10.12.2016, 18:41 
Offline

Joined: 02.12.2016, 12:30
Posts: 23
as i late to this forum..... i don't know worstplayer is regular or not to this forum now, it would be really cool if i get your shader...... :|

_________________
~@AlmahmudRony@~


Top
 Profile  
Reply with quote  
 Post subject: Re: Lightshaft shader
PostPosted: 13.12.2016, 09:09 
Offline

Joined: 02.12.2016, 12:30
Posts: 23
Hi

I use the lightshaft shader from probesys's blog, and rewrite the HDR pipeline :( but i didn't getting the exact result :oops: here how i setup the shader on HDR pipeline....
Quote:
hdr.pipeline.xml

Code:
<!-- High Dynamic Range (HDR) Forward Shading Pipeline -->
<Pipeline>
   <Setup>
      <RenderTarget id="HDRBUF" depthBuf="true" numColBufs="1" format="RGBA16F" scale="1.0" maxSamples="16" />
      <RenderTarget id="BLURBUF1" depthBuf="false" numColBufs="1" format="RGBA8" scale="0.25" />
      <RenderTarget id="BLURBUF2" depthBuf="false" numColBufs="1" format="RGBA8" scale="0.25" />
      <RenderTarget id="GODRAY" depthBuf="false" numColBufs="1" format="RGBA16F" scale="1.0" />
   </Setup>
   
   <CommandQueue>
      <Stage id="Ambient" link="pipelines/globalSettings.material.xml">
         <SwitchTarget target="HDRBUF" />
         <ClearTarget depthBuf="true" colBuf0="true" />
         <DrawGeometry context="AMBIENT" class="~Translucent" />
      </Stage>
      
      <Stage id="Lighting">
         <DoForwardLightLoop class="~Translucent" />
      </Stage>
      
      <Stage id="Translucent">
         <DrawGeometry context="TRANSLUCENT" class="Translucent" order="BACK_TO_FRONT" />
      </Stage>
      
      <!-- HDR post processing -->
      <Stage id="BrightPass">
         <SwitchTarget target="BLURBUF1" />
         <BindBuffer sampler="buf0" sourceRT="HDRBUF" bufIndex="0" />
         <DrawQuad material="pipelines/postHDR.material.xml" context="BRIGHTPASS" />
         <UnbindBuffers />
      </Stage>
      
      <Stage id="Bloom">
         <SwitchTarget target="BLURBUF2" />
         <BindBuffer sampler="buf0" sourceRT="BLURBUF1" bufIndex="0" />
         <SetUniform material="pipelines/postHDR.material.xml" uniform="blurParams" a="0.0" />
         <DrawQuad material="pipelines/postHDR.material.xml" context="BLUR" />
         <SwitchTarget target="BLURBUF1" />
         <BindBuffer sampler="buf0" sourceRT="BLURBUF2" bufIndex="0" />
         <SetUniform material="pipelines/postHDR.material.xml" uniform="blurParams" a="1.0" />
         <DrawQuad material="pipelines/postHDR.material.xml" context="BLUR" />
         <SwitchTarget target="BLURBUF2" />
         <BindBuffer sampler="buf0" sourceRT="BLURBUF1" bufIndex="0" />
         <SetUniform material="pipelines/postHDR.material.xml" uniform="blurParams" a="2.0" />
         <DrawQuad material="pipelines/postHDR.material.xml" context="BLUR" />
         <SwitchTarget target="BLURBUF1" />
         <BindBuffer sampler="buf0" sourceRT="BLURBUF2" bufIndex="0" />
         <SetUniform material="pipelines/postHDR.material.xml" uniform="blurParams" a="3.0" />
         <DrawQuad material="pipelines/postHDR.material.xml" context="BLUR" />
         <UnbindBuffers />
      </Stage>

      <Stage id="GodRayStage">
         <!--Volumetric Light PostProcess-->
         <SwitchTarget target="GODRAY" />
         <ClearTarget colBuf0="true" />
         <BindBuffer sampler="sceneTex" sourceRT="HDRBUF" bufIndex="0" />
         <DrawQuad material="pipelines/postHDR.material.xml" context="LIGHTSHAFT" />
         <!--Volumetric Light PostProcess End-->
         <UnbindBuffers />
      </Stage>
      
      <Stage id="Combination">
         <SwitchTarget target="" />
         <BindBuffer sampler="buf0" sourceRT="HDRBUF" bufIndex="0" />
         <BindBuffer sampler="buf1" sourceRT="BLURBUF1" bufIndex="0" />
         <BindBuffer sampler="GodRayBuff" sourceRT="GODRAY" bufIndex="0" />
         <DrawQuad material="pipelines/postHDR.material.xml" context="FINALPASS" />
         <UnbindBuffers />
      </Stage>
      
      <!-- Overlays -->
      <Stage id="Overlays">
         <DrawOverlays context="OVERLAY" />
      </Stage>
   </CommandQueue>
</Pipeline>



and i put the shader in postHDR.shader.SHADER file
Quote:
postHDR.shader.SHADER

Code:
[[FX]]

// Samplers
sampler2D buf0 = sampler_state
{
   Address = Clamp;
};

sampler2D buf1 = sampler_state
{
   Address = Clamp;
};
sampler2D sceneTex = sampler_state
{
   Address = Clamp;
};
sampler2D GodRayBuff = sampler_state
{
   Address = Clamp;
};
// Uniforms
float hdrExposure = 2.0;       // Exposure (higher values make scene brighter)
float hdrBrightThres = 0.6;    // Brightpass threshold (intensity where blooming begins)
float hdrBrightOffset = 0.06;  // Brightpass offset (smaller values produce stronger blooming)
float4 blurParams = {0, 0, 0, 0};

// Contexts
context BRIGHTPASS
{
   VertexShader = compile GLSL VS_FSQUAD;
   PixelShader = compile GLSL FS_BRIGHTPASS;
   
   ZWriteEnable = false;
}

context BLUR
{
   VertexShader = compile GLSL VS_FSQUAD;
   PixelShader = compile GLSL FS_BLUR;
   
   ZWriteEnable = false;
}

context FINALPASS
{
   VertexShader = compile GLSL VS_FSQUAD;
   PixelShader = compile GLSL FS_FINALPASS;
   
   ZWriteEnable = false;
}
context LIGHTSHAFT
{
   VertexShader = compile GLSL VS_LIGHTSHAFT;
   PixelShader = compile GLSL FS_LIGHTSHAFT;
}

[[VS_FSQUAD]]
// =================================================================================================

uniform mat4 projMat;
attribute vec3 vertPos;
varying vec2 texCoords;
            
void main( void )
{
   texCoords = vertPos.xy;
   gl_Position = projMat * vec4( vertPos, 1 );
}


[[FS_BRIGHTPASS]]
// =================================================================================================

#include "shaders/utilityLib/fragPostProcess.glsl"

uniform sampler2D buf0;
uniform vec2 frameBufSize;
//uniform float hdrExposure;
uniform float hdrBrightThres;
uniform float hdrBrightOffset;
varying vec2 texCoords;

void main( void )
{
   vec2 texSize = frameBufSize * 4.0;
   vec2 coord2 = texCoords + vec2( 2, 2 ) / texSize;
   
   // Average using bilinear filtering
   vec4 sum = getTex2DBilinear( buf0, texCoords, texSize );
   sum += getTex2DBilinear( buf0, coord2, texSize );
   sum += getTex2DBilinear( buf0, vec2( coord2.x, texCoords.y ), texSize );
   sum += getTex2DBilinear( buf0, vec2( texCoords.x, coord2.y ), texSize );
   sum /= 4.0;
   
   // Tonemap
   //sum = 1.0 - exp2( -hdrExposure * sum );
   
   // Extract bright values
   sum = max( sum - hdrBrightThres, 0.0 );
   sum /= hdrBrightOffset + sum;
   
   gl_FragColor = sum;
}

   
[[FS_BLUR]]
// =================================================================================================

#include "shaders/utilityLib/fragPostProcess.glsl"

uniform sampler2D buf0;
uniform vec2 frameBufSize;
uniform vec4 blurParams;
varying vec2 texCoords;

void main( void )
{
   gl_FragColor = blurKawase( buf0, texCoords, frameBufSize, blurParams.x );
}
   

[[FS_FINALPASS]]
// =================================================================================================

uniform sampler2D buf0, buf1,GodRayBuff;
uniform vec2 frameBufSize;
uniform float hdrExposure;
varying vec2 texCoords;

void main( void )
{
   vec4 col0 = texture2D( buf0, texCoords );   // HDR color
   vec4 col1 = texture2D( buf1, texCoords );   // Bloom
   vec4 col2 = texture2D( GodRayBuff, texCoords ); // Godray
   
   // Tonemap (using photographic exposure mapping)
   vec4 col = 1.0 - exp2( -hdrExposure * col0 );
   
   gl_FragColor = col + col1 + col2;
}
[[VS_LIGHTSHAFT]]
// =================================================================================================
varying vec3 lightPos;
void main(void)
{
   vec4 v = (gl_ProjectionMatrix * gl_ModelViewMatrix *  gl_LightSource[0].position);
   lightPos = v.xyz / v.w;
   gl_Position = ftransform();
   gl_TexCoord[0] = gl_MultiTexCoord0;
}

[[FS_LIGHTSHAFT]]
// =================================================================================================
uniform sampler2D sceneTex;
varying vec3 lightPos;
void main()
{
   int NUM_SAMPLES = 100;
   float density = 0.9;
   float illuminationDecay = 1.0;
   float weight = 0.010;
   float decay = 0.99;
 
   vec2 uv = gl_TexCoord[0].xy;
   //lightPos.xy = (lightPos.xy + vec2(1.0, 1.0)) / 2.0;
   //vec2 deltaTexCoord = uv - lightPos.xy;
  vec2 deltaTexCoord = uv - ((lightPos.xy + vec2(1.0, 1.0)) / 2.0);

   deltaTexCoord = deltaTexCoord / (float(NUM_SAMPLES) * density);
     
   vec3 color = texture2D(sceneTex, uv).xyz;
 
   for(int i=0; i<NUM_SAMPLES; ++i)
   {
      uv -= deltaTexCoord;
      vec3 sample = texture2D(sceneTex, uv).xyz;
      sample = sample * illuminationDecay * weight;
      color += sample;
      illuminationDecay = illuminationDecay * decay;
   }
   
   gl_FragColor = vec4(color, 1.0);
}



can anybody please tell me the correct way to setup this shader to pipeline....

Thanks :|

_________________
~@AlmahmudRony@~


Top
 Profile  
Reply with quote  
 Post subject: Re: Lightshaft shader
PostPosted: 13.12.2016, 10:03 
Offline

Joined: 17.11.2009, 17:00
Posts: 205
Location: Russia, Moscow
Umm, you are probably having glitches because of this line:
vec4 v = (gl_ProjectionMatrix * gl_ModelViewMatrix * gl_LightSource[0].position); // in VS_LIGHTSHAFT
Edit: the whole vertex shader is incorrect. You should use built-in horde's variables for positions and texture coordinates. Check documentation: link (check Pipeline specification, particularly the end of the page)

gl_LightSource - it is a variable that is used if you use fixed function pipeline. Horde does not use fixed function pipeline, so this variable is always 0.
You should use only horde's built-in variables or your custom one.
Please note that this shader was built for earlier version of Horde (it seems that it was written for Beta 2), so some things are different in shaders now.
Also variables that are used for lighting are only available during light pass, so variables like lightPos will only be available when you <DoForwardLightLoop class="~Translucent" /> and not on later stages.


Top
 Profile  
Reply with quote  
 Post subject: Re: Lightshaft shader
PostPosted: 13.12.2016, 11:06 
Offline

Joined: 02.12.2016, 12:30
Posts: 23
So it's problem with the lightPos right?, i have read the manual doc, it's says something {uniform vec4 lightPos} is reserved by the engine, and if i write it correctly the engine will send the right value, so i write {varying vec3 lightPos} instead of {uniform vec4 lightPos} but it's not working again :cry: ......

_________________
~@AlmahmudRony@~


Top
 Profile  
Reply with quote  
 Post subject: Re: Lightshaft shader
PostPosted: 13.12.2016, 12:34 
Offline

Joined: 17.11.2009, 17:00
Posts: 205
Location: Russia, Moscow
gl_ProjectionMatrix * gl_ModelViewMatrix * gl_LightSource[0].position
All these variables are incorrect (they are 0, because they are not set by the engine).
Check VS_General
You should use
uniform mat4 viewProjMat;
uniform vec3 viewerPos;
attribute vec3 vertPos;
attribute vec2 texCoords0;
varying vec4 pos, vsPos;
varying vec2 texCoords;

void main()
{
...
pos = calcWorldPos( vec4( vertPos, 1.0 ) );
// Calculate texture coordinates and clip space position
texCoords = texCoords0;
gl_Position = viewProjMat * pos;
}

This shader is for models in 3d space. If lightshaft is a post-processing effect, which it likely is, and is drawn using fullscreen quad, than take a look at VS_QUAD shader (in deferredLighting.shader).


Top
 Profile  
Reply with quote  
 Post subject: Re: Lightshaft shader
PostPosted: 13.12.2016, 12:56 
Offline

Joined: 02.12.2016, 12:30
Posts: 23
Yep! it's a post process...
I just changed the shader, i thing its some kind of working but its flashing the screen badly when i rotating the camera :cry:
Here what i rewrote... :oops:
Code:
[[VS_LIGHTSHAFT]]
// =================================================================================================
uniform mat4 projMat;
uniform vec4 lightPos;
varying vec3 lPos;
attribute vec3 vertPos;
varying vec2 texCoords;

void main(void)
{
   vec4 v = lightPos.xyzw;
   lPos = v.xyz / v.w;
   //gl_Position = ftransform();
   gl_Position = projMat * vec4( vertPos, 1 );
   texCoords = vertPos.xy;
   }

[[FS_LIGHTSHAFT]]
// =================================================================================================
uniform sampler2D sceneTex;
//uniform vec4 lPos;
varying vec3 lPos; //Edited  :mrgreen:
varying vec2 texCoords;
void main()
{
   int NUM_SAMPLES = 100;
   float density = 0.9;
   float illuminationDecay = 1.0;
   float weight = 0.010;
   float decay = 0.99;
 
   vec2 uv = texCoords.xy;
   
  vec2 deltaTexCoord = uv - ((lPos.xy + vec2(1.0, 1.0)) / 2.0);

   deltaTexCoord = deltaTexCoord / (float(NUM_SAMPLES) * density);
     
   vec3 color = texture2D(sceneTex, uv).xyz;
 
   for(int i=0; i<NUM_SAMPLES; ++i)
   {
      uv -= deltaTexCoord;
      vec3 sample = texture2D(sceneTex, uv).xyz;
      sample = sample * illuminationDecay * weight;
      color += sample;
      illuminationDecay = illuminationDecay * decay;
   }
   
   gl_FragColor = vec4(color, 1.0);
}

_________________
~@AlmahmudRony@~


Last edited by Almahmudrony on 13.12.2016, 13:10, edited 1 time in total.

Top
 Profile  
Reply with quote  
 Post subject: Re: Lightshaft shader
PostPosted: 13.12.2016, 13:07 
Offline

Joined: 02.12.2016, 12:30
Posts: 23
Holly! cow the bug has been fixed :lol:
:D
here is the problem... :mrgreen:
it should be {varying vec3 lPos;} not this {uniform vec4 lPos;}...
:wink:
Thank you....

_________________
~@AlmahmudRony@~


Top
 Profile  
Reply with quote  
 Post subject: Re: Lightshaft shader
PostPosted: 13.12.2016, 13:11 
Offline

Joined: 17.11.2009, 17:00
Posts: 205
Location: Russia, Moscow
You should probably add a separate light in your scene with the lighting context LIGHTSHAFT in order to draw correctly.
Otherwise lightPos is not available at the time of rendering (it is always zero).

You only have lightPos during this stage:
<Stage id="Lighting">
<DoForwardLightLoop class="~Translucent" />
</Stage>

But you are trying to use it in
<Stage id="GodRayStage">
<!--Volumetric Light PostProcess-->
<SwitchTarget target="GODRAY" />
<ClearTarget colBuf0="true" />
<BindBuffer sampler="sceneTex" sourceRT="HDRBUF" bufIndex="0" />
<DrawQuad material="pipelines/postHDR.material.xml" context="LIGHTSHAFT" />
<!--Volumetric Light PostProcess End-->
<UnbindBuffers />
</Stage>

Either add <DoForwardLightLoop class="..." /> where ... is the class of your lightshaft light, or you'll have to determine your light position some other way.


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 18 posts ]  Go to page 1, 2  Next

All times are UTC + 1 hour


Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
Powered by phpBB® Forum Software © phpBB Group