Changeset 2966


Ignore:
Timestamp:
09/23/08 15:41:14 (16 years ago)
Author:
mattausch
Message:

started reinhard tonemapping implementation but slow

Location:
GTP/trunk/App/Demos/Vis/FriendlyCulling/src
Files:
7 edited

Legend:

Unmodified
Added
Removed
  • GTP/trunk/App/Demos/Vis/FriendlyCulling/src/DeferredRenderer.cpp

    r2965 r2966  
    99#include "ShadowMapping.h" 
    1010#include "Light.h" 
     11#include "ToneMapper.h" 
    1112 
    1213 
     
    9697static CGparameter sLightDirShadowParam; 
    9798 
     99static CGparameter sImageKeyParam; 
     100static CGparameter sImageKeyShadowParam; 
    98101 
    99102//#define USE_3D_SSAO 
     
    108111static Sample2 samples2[NUM_SAMPLES]; 
    109112#endif 
    110 Sample2 pcfSamples[16]; 
     113 
     114// number of pcf tabs 
     115Sample2 pcfSamples[NUM_PCF_TABS]; 
    111116 
    112117static int colorBufferIdx = 0; 
     
    281286                sNormalsTexDeferredParam = cgGetNamedParameter(sCgDeferredProgram, "normals"); 
    282287                sLightDirParam = cgGetNamedParameter(sCgDeferredProgram, "lightDir"); 
     288 
     289                sImageKeyParam = cgGetNamedParameter(sCgDeferredProgram, "imageKey"); 
    283290        } 
    284291        else 
     
    433440                sLightDirShadowParam = cgGetNamedParameter(sCgDeferredShadowProgram, "lightDir"); 
    434441 
    435                 PoissonDiscSampleGenerator2 poisson(16, 1.0f); 
     442                sImageKeyShadowParam = cgGetNamedParameter(sCgDeferredShadowProgram, "imageKey"); 
     443 
     444                PoissonDiscSampleGenerator2 poisson(NUM_PCF_TABS, 1.0f); 
    436445                poisson.Generate((float *)pcfSamples); 
    437446 
    438                 cgGLSetParameterArray2f(sSamplesShadowParam, 0, 16, (const float *)pcfSamples); 
     447                cgGLSetParameterArray2f(sSamplesShadowParam, 0, NUM_PCF_TABS, (const float *)pcfSamples); 
    439448        } 
    440449        else 
     
    699708void DeferredRenderer::FirstPass(FrameBufferObject *fbo, DirectionalLight *light) 
    700709{ 
     710        const float imageKey = ToneMapper().CalcImageKey(fbo->GetColorBuffer(0)); 
     711 
    701712        GLuint colorsTex = fbo->GetColorBuffer(0)->GetTexture(); 
    702713        GLuint positionsTex = fbo->GetColorBuffer(1)->GetTexture(); 
     
    725736        Vector3 lightDir = -light->GetDirection(); 
    726737        cgGLSetParameter3f(sLightDirParam, lightDir.x, lightDir.y, lightDir.z); 
     738 
     739        cgGLSetParameter1f(sImageKeyParam, imageKey); 
    727740 
    728741        glColor3f(1.0f, 1.0f, 1.0f); 
     
    965978                                                                           ShadowMap *shadowMap) 
    966979{ 
     980        const float imageKey = ToneMapper().CalcImageKey(fbo->GetColorBuffer(0)); 
     981 
    967982        GLuint colorsTex = fbo->GetColorBuffer(0)->GetTexture(); 
    968983 
     
    10091024        cgGLSetParameter3f(sLightDirShadowParam, lightDir.x, lightDir.y, lightDir.z); 
    10101025 
     1026        // the average log luminance for tone mapping 
     1027        cgGLSetParameter1f(sImageKeyShadowParam, imageKey); 
    10111028 
    10121029        glColor3f(1.0f, 1.0f, 1.0f); 
  • GTP/trunk/App/Demos/Vis/FriendlyCulling/src/FrameBufferObject.h

    r2965 r2966  
    4141        ~ColorBufferObject(); 
    4242 
    43         unsigned int GetTexture() const { return mTexId; } 
     43        inline unsigned int GetTexture() const { return mTexId; } 
    4444 
     45        inline int GetWidth() { return mWidth; } 
     46 
     47        inline int GetHeight() { return mHeight; } 
    4548        /** Returns texture data. 
    4649        */ 
    4750        void *ReadTexture() const; 
    4851 
     52         
    4953 
    5054protected: 
  • GTP/trunk/App/Demos/Vis/FriendlyCulling/src/ToneMapper.cpp

    r2965 r2966  
    66{ 
    77 
    8 ToneMapper::ToneMapper(): mImageKey(.0f), mPreviousImageKey(.0f), mCurrentImageKey(.0f) 
     8 
     9ToneMapper::ToneMapper() 
    910{ 
    1011} 
    1112 
    1213 
    13 bool ToneMapper::CalcLuminance(ColorBufferObject *colorBuffer) 
     14float ToneMapper::CalcImageKey(ColorBufferObject *colorBuffer) const 
    1415{ 
    15         int i = 0, j = 0; 
    16         float Rfloat, Gfloat, Bfloat; 
     16        float totalLum = 0; 
    1717 
    18         int pixRGBE; 
    19  
    20         float pixLum = 0;  
    21         float totalLum = 0; 
    22         float pixNum = 0; 
    23          
     18        // we assume that we have a floating point rgba texture 
    2419        float *pixels = (float *)colorBuffer->ReadTexture(); 
    2520 
    26         /* 
    27         V_RETURN( pd3dDevice->GetRenderTargetData( pSourceRenderTarget, pPixelData ) ); 
    28           
    29         D3DSURFACE_DESC descSrc; 
    30     pPixelData->GetDesc( &descSrc ); 
    31          
    32         number_of_pixels = (float)(descSrc.Height * descSrc.Width); 
     21        const int w = colorBuffer->GetHeight(); 
     22        const int h = colorBuffer->GetWidth(); 
    3323 
    34         // Lock the source texture for reading 
    35     D3DLOCKED_RECT rcSrc; 
    36     V_RETURN( pPixelData->LockRect( &rcSrc, NULL, D3DLOCK_READONLY ) ); 
    37          
    38         // Location of texture data 
    39         D3DXFLOAT16* pSourcePixels = (D3DXFLOAT16*)rcSrc.pBits; 
     24        // Get full scene luminance 
     25        for (int i = 0; i < h; ++ i) 
     26        { 
     27                for (int j = 0; j < w; ++ j) 
     28                { 
     29                        const int idx = i * w + j; 
    4030 
    41         int localwidth = descSrc.Width; 
    42         int localheight = descSrc.Height;        
    43         g_start = g_myTimer.GetTime(); 
    44          
    45         // Get full scene luminance 
    46         for( i = 0; i < localheight; i++ ) 
    47         { 
    48                 for( j = 0; j < localwidth; j++ ) 
    49                 { 
    50                         RGBEpixel = i * (localwidth) + j; 
    51                         Rfloat =(float)pSourcePixels[4*RGBEpixel + 0]/255; 
    52                         Gfloat =(float)pSourcePixels[4*RGBEpixel + 1]/255; 
    53                         Bfloat =(float)pSourcePixels[4*RGBEpixel + 2]/255; 
     31                        float r = pixels[4 * idx + 0]; 
     32                        float g = pixels[4 * idx + 1]; 
     33                        float b = pixels[4 * idx + 2]; 
    5434 
    55                         PixelLuminance = 0.2125f * Rfloat + 0.7154f * Gfloat + 0.0721f * Bfloat; 
     35                        float pixLum = 0.2125f * r + 0.7154f * g + 0.0721f * b; 
    5636 
    57                         TotalLuminance += (PixelLuminance + 0.001f); 
     37                        totalLum += log(pixLum + 1e-3f); 
    5838                } 
    5939        } 
    6040 
    61         g_stop = g_myTimer.GetTime(); 
     41        const float key = exp(totalLum) / (float)(w * h); 
    6242 
    63         g_dLumCalcTime = (double)g_stop - (double)g_start;       
     43        static float previousKey = key; 
    6444 
    65         pPixelData->UnlockRect();        
     45        //float delta = imageKey - mPreviousImageKey; 
     46        // Constaint change for temporal coherence 
     47        //const float maxChange = 5-e2f; 
     48        //Clamp(delta, -maxChange, maxChange); 
     49 
     50        const float factor = 0.5f; 
     51 
     52        const float imageKey = key * factor + previousKey * (1.0f - factor); 
     53        previousKey = imageKey; 
    6654         
     55        delete [] pixels; 
    6756 
    68         g_fCurrentImageKey = (1/number_of_pixels) * TotalLuminance; 
    69  
    70         if(g_firstrun) 
    71         { 
    72                 g_fPreviousImageKey = g_fCurrentImageKey; 
    73                 g_firstrun = !g_firstrun; 
    74         } 
    75  
    76         float diff = (g_fCurrentImageKey - g_fPreviousImageKey); 
    77  
    78         // Constrain the luminance change to be more realistic 
    79         if (diff > 0.05f) 
    80         { 
    81                 g_fImageKey = g_fPreviousImageKey + 0.05f; 
    82                 g_fPreviousImageKey = g_fImageKey; 
    83         } 
    84         else if (diff < -0.05f) 
    85         { 
    86                 g_fImageKey = g_fPreviousImageKey - 0.05f; 
    87                 g_fPreviousImageKey = g_fImageKey; 
    88         } 
    89         else 
    90         { 
    91                 g_fImageKey = g_fCurrentImageKey; 
    92                 g_fPreviousImageKey = g_fImageKey; 
    93         } 
    94          
    95         return S_OK; 
    96         */ 
    97         return true; 
     57        return imageKey; 
    9858} 
    9959 
  • GTP/trunk/App/Demos/Vis/FriendlyCulling/src/ToneMapper.h

    r2965 r2966  
    1616         
    1717        ToneMapper(); 
    18         /** Caculates scene luminance. 
     18        /** Caculates average log lumincance. 
    1919        */ 
    20         bool CalcLuminance(ColorBufferObject *colorBuffer); 
    21  
    22  
    23 protected: 
    24  
    25         float mImageKey; 
    26         float mPreviousImageKey; 
    27         float mCurrentImageKey; 
     20        float CalcImageKey(ColorBufferObject *colorBuffer) const; 
    2821}; 
    2922 
  • GTP/trunk/App/Demos/Vis/FriendlyCulling/src/common.h

    r2961 r2966  
    293293 
    294294 
    295 Real Random(Real max); 
    296 int Random(int max); 
     295Real Random(Real maxValue); 
     296int Random(int maxValue); 
    297297void Randomize(); 
    298298void Randomize(unsigned int seed); 
     
    310310} 
    311311   
    312 void InitTiming(); 
    313 long GetTime(); 
    314 long GetRealTime(); 
    315 Real TimeDiff(long t1, long t2); 
    316 char *TimeString(); 
    317  
     312inline Real Clamp(float x, float minVal, float maxVal) 
     313{ 
     314        if (x < minVal) return minVal; 
     315        if (x > maxVal) return maxVal; 
     316 
     317        return x; 
     318} 
    318319 
    319320 
  • GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaderenv.h

    r2964 r2966  
    11#ifndef __SHADERENV_H 
    22#define __SHADERENV_H 
     3 
     4 
     5//////////// 
     6//-- ssao + gi parameters 
    37 
    48#define NUM_SAMPLES 8 
    59//#define NUM_SAMPLES 16 
    610 
    7  
    811// for linear falloff 
    912//#define SAMPLE_INTENSITY 500.0f 
    10 //#define SAMPLE_INTENSITY 40.0f 
    1113 
    1214// for quadratic falloff 
    1315#define SAMPLE_INTENSITY 0.32f 
    1416//#define SAMPLE_INTENSITY 0.85f 
    15 //#define SAMPLE_INTENSITY 5.0f 
    1617 
    1718//#define AREA_SIZE 15e-1f 
    18 //#define AREA_SIZE 25e-1f 
    1919#define AREA_SIZE 8e-1f 
    2020 
     
    2424//#define DISTANCE_SCALE 1e-4f 
    2525#define DISTANCE_SCALE 1e-6f 
    26 //#define DISTANCE_SCALE 1e-8f 
    2726 
    2827#define ILLUM_INTENSITY 5e-1f; 
     
    3231#define GI_MIPMAP_LEVEL 2 
    3332 
     33 
     34///////// 
     35//-- tone mapping 
     36 
     37//#define MIDDLE_GRAY 0.36f 
     38#define MIDDLE_GRAY 0.18f 
     39 
     40 
     41///////// 
     42//-- shadowing 
     43 
     44#define NUM_PCF_TABS 16 
     45 
     46 
     47 
    3448#endif // __SHADERENV_H 
  • GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/deferred.cg

    r2965 r2966  
     1#include "../shaderenv.h" 
     2 
     3 
    14struct fragment 
    25{ 
     
    1316}; 
    1417 
    15 #define NUM_SAMPLES 16 
     18 
     19 
     20float3 ToneMap(const float imageKey, float3 color) 
     21{ 
     22        float3 outCol; 
     23        const float pixLum = 0.2125f * color.x + 0.7154f * color.y + 0.0721f * color.z; 
     24         
     25        // adjust to middle gray 
     26        const float lum = MIDDLE_GRAY * pixLum / imageKey; 
     27          
     28    // map to range 
     29        const float scaledLum = lum / (1.0f + lum); 
     30 
     31    /*outCol *= MIDDLE_GRAY / (vLum + 1e-3f); 
     32    outCol *= (1.0f + vColor / LUM_WHITE); 
     33        outCol /= (1.0f + vColor);*/ 
     34         
     35        outCol = color / scaledLum; 
     36        //vColor.rgb += 0.6f * vBloom; 
     37         
     38        return outCol; 
     39} 
    1640 
    1741 
     
    4064        float3 light2 = normalize(lightDir2.xyz); 
    4165        float diffuseLight2 = saturate(dot(normal, light2)); 
    42 */ 
     66        */ 
    4367        // diffuse intensity 
    4468        const float angle = saturate(dot(normal, lightDir));  
     
    6286                   uniform sampler2D positions, 
    6387                   uniform sampler2D normals, 
    64                    uniform float3 lightDir 
     88                   uniform float3 lightDir, 
     89                   uniform float imageKey 
    6590                   ) 
    6691{ 
     
    90115                                         uniform float2 lightSpacePos, 
    91116                                         uniform float depth, 
    92                                          uniform float2 samples[NUM_SAMPLES], 
     117                                         uniform float2 samples[NUM_PCF_TABS], 
    93118                                         uniform sampler2D noiseTexture 
    94119                                         ) 
     
    99124        float total_d = 0.0; 
    100125         
    101         for (int i = 0; i < NUM_SAMPLES; ++ i)  
     126        for (int i = 0; i < NUM_PCF_TABS; ++ i)  
    102127        { 
    103128                const float2 offset = samples[i]; 
     
    135160                                  uniform float sampleWidth, 
    136161                                  uniform sampler2D noiseTexture, 
    137                                   uniform float2 samples[NUM_SAMPLES], 
    138                                   uniform float3 lightDir 
     162                                  uniform float2 samples[NUM_PCF_TABS], 
     163                                  uniform float3 lightDir, 
     164                                  uniform float imageKey 
    139165                                  ) 
    140166{ 
     
    181207        OUT.color = (emmisive < 0.95) ? (ambient + diffuse) * color : color; 
    182208 
     209        OUT.color.xyz = ToneMap(imageKey, OUT.color.xyz); 
     210 
    183211        // also write out depth component 
    184212        OUT.color.w = color.w; 
     
    187215} 
    188216 
    189  
    190 /*float4 FinalPass( SkyboxVS_Output Input ) : COLOR 
    191 { 
    192         float4 vColor = tex2D( s0, Input.Tex ); 
    193         float3 vBloom = tex2D( s1, Input.Tex ); 
    194  
    195         float vLum = g_fImageKey; 
    196         vColor.rgb = vColor;  
    197           
    198     // Tone mapping 
    199     vColor.rgb *= g_fMiddleGray /(vLum + 0.001f); 
    200     vColor.rgb *= (1.0f + vColor/LUM_WHITE); 
    201         vColor.rgb /= (1.0f + vColor); 
    202          
    203         vColor.rgb += 0.6f * vBloom; 
    204         vColor.a = 1.0f; 
    205          
    206         return vColor; 
    207 }*/ 
Note: See TracChangeset for help on using the changeset viewer.