Ignore:
Timestamp:
08/28/08 21:17:15 (16 years ago)
Author:
mattausch
Message:

improved performance

File:
1 edited

Legend:

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

    r2875 r2879  
    99using namespace std; 
    1010 
    11 static GLenum mymrt[] = {GL_COLOR_ATTACHMENT0_EXT, GL_COLOR_ATTACHMENT1_EXT, GL_COLOR_ATTACHMENT2_EXT}; 
     11static GLenum mymrt[] = {GL_COLOR_ATTACHMENT0_EXT, GL_COLOR_ATTACHMENT1_EXT, GL_COLOR_ATTACHMENT2_EXT, GL_COLOR_ATTACHMENT3_EXT}; 
    1212 
    1313namespace CHCDemoEngine 
     
    7272static CGparameter sNormalsTexAntiAliasingParam; 
    7373 
    74  
    75 static GLuint noiseTex; 
     74static GLuint noiseTex = 0; 
    7675 
    7776// ssao random spherical samples 
     
    9897        static PoissonDiscSampleGenerator poisson(NUM_SAMPLES, 1.0f); 
    9998        poisson.Generate((Sample2 *)samples); 
     99} 
     100 
     101 
     102static void CreateNoiseTex2D(int w, int h) 
     103{ 
     104        // hack: should be able to recalc noise texture 
     105        //if (noiseTex > 0) return; 
     106 
     107        //GLubyte *randomNormals = new GLubyte[mWidth * mHeight * 3]; 
     108        float *randomNormals = new float[w * h * 3]; 
     109 
     110        for (int i = 0; i < w * h * 3; i += 3) 
     111        { 
     112                // create random samples on a circle 
     113                const float rx = RandomValue(0, 1); 
     114                const float theta = 2.0f * acos(sqrt(1.0f - rx)); 
     115 
     116                //randomNormals[i + 0] = (GLubyte)((cos(theta) * 0.5f + 0.5f) * 255.0f); 
     117                //randomNormals[i + 1] = (GLubyte)((sin(theta) * 0.5f + 0.5f) * 255.0f); 
     118                randomNormals[i + 0] = cos(theta); 
     119                randomNormals[i + 1] = sin(theta); 
     120                randomNormals[i + 2] = 0; 
     121        } 
     122 
     123        glEnable(GL_TEXTURE_2D); 
     124        glGenTextures(1, &noiseTex); 
     125        glBindTexture(GL_TEXTURE_2D, noiseTex); 
     126                 
     127        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); 
     128        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); 
     129        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); 
     130        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); 
     131 
     132        //glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB8, mWidth, mHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, randomNormals); 
     133        glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA16F_ARB, w, h, 0, GL_RGB, GL_FLOAT, randomNormals); 
     134 
     135        glBindTexture(GL_TEXTURE_2D, 0); 
     136        glDisable(GL_TEXTURE_2D); 
     137 
     138        delete [] randomNormals; 
     139 
     140        cout << "created noise texture" << endl; 
     141 
     142        PrintGLerror("noisetexture"); 
    100143} 
    101144 
     
    108151mUseGlobIllum(false) 
    109152{ 
     153        // create noise texture for ssao 
     154        CreateNoiseTex2D(w, h); 
     155 
    110156        /////////// 
    111157        //-- the flip-flop fbos 
    112158 
    113159        mNewFbo = new FrameBufferObject(w, h, FrameBufferObject::DEPTH_NONE); 
    114         // the diffuse color buffer 
    115160        mNewFbo->AddColorBuffer(ColorBufferObject::BUFFER_FLOAT_32, ColorBufferObject::WRAP_CLAMP_TO_EDGE, ColorBufferObject::FILTER_LINEAR, false); 
    116161        mNewFbo->AddColorBuffer(ColorBufferObject::BUFFER_FLOAT_32, ColorBufferObject::WRAP_CLAMP_TO_EDGE, ColorBufferObject::FILTER_LINEAR, false); 
     
    118163         
    119164        mOldFbo = new FrameBufferObject(w, h, FrameBufferObject::DEPTH_NONE); 
    120         // the diffuse color buffer 
    121165        mOldFbo->AddColorBuffer(ColorBufferObject::BUFFER_FLOAT_32, ColorBufferObject::WRAP_CLAMP_TO_EDGE, ColorBufferObject::FILTER_LINEAR, false); 
    122166        mOldFbo->AddColorBuffer(ColorBufferObject::BUFFER_FLOAT_32, ColorBufferObject::WRAP_CLAMP_TO_EDGE, ColorBufferObject::FILTER_LINEAR, false); 
    123167        mOldFbo->AddColorBuffer(ColorBufferObject::BUFFER_FLOAT_32, ColorBufferObject::WRAP_CLAMP_TO_EDGE, ColorBufferObject::FILTER_LINEAR, false); 
    124168         
    125         mFbo3 = new FrameBufferObject(w, h, FrameBufferObject::DEPTH_NONE); 
    126         // the diffuse color buffer 
    127         mFbo3->AddColorBuffer(ColorBufferObject::BUFFER_FLOAT_32, ColorBufferObject::WRAP_CLAMP_TO_EDGE, ColorBufferObject::FILTER_LINEAR, false); 
    128                  
    129  
    130         // create noise texture for ssao 
    131         CreateNoiseTex2D(); 
     169        //mFbo = new FrameBufferObject(w, h, FrameBufferObject::DEPTH_NONE); 
     170        //mFbo->AddColorBuffer(ColorBufferObject::BUFFER_FLOAT_32, ColorBufferObject::WRAP_CLAMP_TO_EDGE, ColorBufferObject::FILTER_LINEAR, false);*/ 
    132171} 
    133172 
    134173 
    135174SsaoShader::~SsaoShader()  
    136 { 
    137         if (sCgSsaoProgram) 
    138                 cgDestroyProgram(sCgSsaoProgram); 
     175 
     176{ 
     177        if (sCgSsaoProgram)     cgDestroyProgram(sCgSsaoProgram); 
     178        if (sCgDeferredProgram) cgDestroyProgram(sCgDeferredProgram); 
     179        if (sCgSsaoProgram)     cgDestroyProgram(sCgSsaoProgram); 
     180        if (sCgGiProgram) cgDestroyProgram(sCgGiProgram); 
     181        if (sCgAntiAliasingProgram) cgDestroyProgram(sCgAntiAliasingProgram); 
    139182 
    140183        DEL_PTR(mNewFbo); 
    141184        DEL_PTR(mOldFbo); 
     185        //DEL_PTR(mFbo); 
    142186 
    143187        glDeleteTextures(1, &noiseTex); 
     
    295339        FrameBufferObject::Release(); 
    296340 
    297         glDrawBuffers(1, mymrt); 
    298  
    299341        cgGLEnableProfile(RenderState::sCgFragmentProfile); 
    300342 
     
    345387        cgGLSetMatrixParameterfc(sOldModelViewProjMatrixParam, (const float *)oldProjViewMatrix.x); 
    346388 
    347         GLuint colorsTex = mFbo3->GetColorBuffer(0)->GetTexture(); 
     389//      GLuint colorsTex = mFbo->GetColorBuffer(0)->GetTexture(); 
     390        GLuint colorsTex = fbo->GetColorBuffer(3)->GetTexture(); 
    348391        GLuint positionsTex = fbo->GetColorBuffer(1)->GetTexture(); 
    349392        GLuint normalsTex = fbo->GetColorBuffer(2)->GetTexture(); 
     
    462505 
    463506 
    464 void SsaoShader::CreateNoiseTex2D() 
    465 { 
    466         //GLubyte *randomNormals = new GLubyte[mWidth * mHeight * 3]; 
    467         float *randomNormals = new float[mWidth * mHeight * 3]; 
    468  
    469         for (int i = 0; i < mWidth * mHeight * 3; i += 3) 
    470         { 
    471                 // create random samples on a circle 
    472                 const float rx = RandomValue(0, 1); 
    473                 const float theta = 2.0f * acos(sqrt(1.0f - rx)); 
    474  
    475                 //randomNormals[i + 0] = (GLubyte)((cos(theta) * 0.5f + 0.5f) * 255.0f); 
    476                 //randomNormals[i + 1] = (GLubyte)((sin(theta) * 0.5f + 0.5f) * 255.0f); 
    477                 randomNormals[i + 0] = cos(theta); 
    478                 randomNormals[i + 1] = sin(theta); 
    479                 randomNormals[i + 2] = 0; 
    480         } 
    481  
    482         glEnable(GL_TEXTURE_2D); 
    483         glGenTextures(1, &noiseTex); 
    484         glBindTexture(GL_TEXTURE_2D, noiseTex); 
    485                  
    486         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); 
    487         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); 
    488         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); 
    489         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); 
    490  
    491         //glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB8, mWidth, mHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, randomNormals); 
    492         glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA16F_ARB, mWidth, mHeight, 0, GL_RGB, GL_FLOAT, randomNormals); 
    493  
    494         glBindTexture(GL_TEXTURE_2D, 0); 
    495         glDisable(GL_TEXTURE_2D); 
    496  
    497         delete [] randomNormals; 
    498  
    499         cout << "created noise texture" << endl; 
    500  
    501         PrintGLerror("noisetexture"); 
    502 } 
    503  
    504  
    505507 
    506508static void SetVertex(float x, float y, float x_offs, float y_offs) 
     
    521523void SsaoShader::AntiAliasing(FrameBufferObject *fbo) 
    522524{ 
    523         //GLuint colorsTex = mFbo4->GetColorBuffer(0)->GetTexture(); 
    524525        GLuint colorsTex = mNewFbo->GetColorBuffer(1)->GetTexture(); 
    525526        GLuint normalsTex = fbo->GetColorBuffer(2)->GetTexture(); 
     
    566567        GLuint normalsTex = fbo->GetColorBuffer(2)->GetTexture(); 
    567568 
    568         mFbo3->Bind(); 
    569         //mNewFbo->Bind(); 
     569        fbo->Bind(); 
     570 
     571        glDrawBuffers(1, mymrt + 3); 
    570572 
    571573        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 
    572574         
    573         glDrawBuffers(1, mymrt); 
    574  
    575575        cgGLEnableProfile(RenderState::sCgFragmentProfile); 
    576576 
     
    618618        cgGLSetMatrixParameterfc(sOldModelViewProjMatrixGiParam, (const float *)oldProjViewMatrix.x); 
    619619 
    620         GLuint colorsTex = mFbo3->GetColorBuffer(0)->GetTexture(); 
     620        //GLuint colorsTex = mFbo->GetColorBuffer(0)->GetTexture(); 
     621        GLuint colorsTex = fbo->GetColorBuffer(3)->GetTexture(); 
    621622        GLuint positionsTex = fbo->GetColorBuffer(1)->GetTexture(); 
    622623        GLuint normalsTex = fbo->GetColorBuffer(2)->GetTexture(); 
Note: See TracChangeset for help on using the changeset viewer.