Changeset 2911 for trunk/packages


Ignore:
Timestamp:
Apr 1, 2012, 12:26:04 PM (13 years ago)
Author:
ldelgass
Message:

Begin to isolate Cg code in NvShader? and subclasses. Add Cg profile defaults
and instance members to centralize profile. Add bind/unbind methods to
NvShader? that bind/unbind programs with the appropriate profile. Add a
private static method loadCgSourceProgram to replace the global
LoadCgSourceProgram? after all users are converted to using NvShader? and its
loader methods.

Location:
trunk/packages/vizservers/nanovis
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/packages/vizservers/nanovis/NvShader.cpp

    r2877 r2911  
    1111#include "Trace.h"
    1212
     13CGprofile NvShader::_defaultVertexProfile = CG_PROFILE_VP40;
     14CGprofile NvShader::_defaultFragmentProfile = CG_PROFILE_FP40;
    1315CGcontext NvShader::_cgContext = NULL;
    1416
    15 void NvShader::initCg()
     17void NvShader::initCg(CGprofile defaultVertexProfile,
     18                      CGprofile defaultFragmentProfile)
    1619{
     20    _defaultVertexProfile = defaultVertexProfile;
     21    _defaultFragmentProfile = defaultFragmentProfile;
    1722    _cgContext = cgCreateContext();
    1823}
     
    2328    printErrorInfo();
    2429    if (_cgContext != NULL) {
     30        TRACE("Before DestroyContext");
    2531        cgDestroyContext(_cgContext);
     32        TRACE("After DestroyContext");
    2633        _cgContext = NULL;
    2734    }
     
    4249
    4350CGprogram
    44 LoadCgSourceProgram(CGcontext context, const char *fileName, CGprofile profile,
    45                     const char *entryPoint)
     51LoadCgSourceProgram(CGcontext context, const char *fileName,
     52                    CGprofile profile, const char *entryPoint)
     53{
     54    return NvShader::loadCgSourceProgram(context, fileName,
     55                                         profile, entryPoint);
     56}
     57
     58CGprogram
     59NvShader::loadCgSourceProgram(CGcontext context, const char *fileName,
     60                              CGprofile profile, const char *entryPoint)
    4661{
    4762    const char *path = R2FilePath::getInstance()->getPath(fileName);
     
    6479
    6580NvShader::NvShader():
     81    _vertexProfile(_defaultVertexProfile),
     82    _fragmentProfile(_defaultFragmentProfile),
    6683    _cgVP(NULL),
    6784    _cgFP(NULL)
     
    7188NvShader::~NvShader()
    7289{
    73     resetPrograms();
     90    TRACE("In ~NvShader");
     91    if (_cgContext == NULL) {
     92        TRACE("Lost Cg context");
     93    } else {
     94        resetPrograms();
     95    }
    7496}
    7597
    7698void NvShader::loadVertexProgram(const char *fileName, const char *entryPoint)
    7799{
    78     resetPrograms();
    79 
    80     _cgVP = LoadCgSourceProgram(_cgContext, fileName, CG_PROFILE_VP40, entryPoint);
     100    if (_cgVP != NULL) {
     101        cgDestroyProgram(_cgVP);
     102    }
     103    _cgVP = loadCgSourceProgram(_cgContext, fileName,
     104                                _vertexProfile, entryPoint);
    81105}
    82106
    83107void NvShader::loadFragmentProgram(const char *fileName, const char *entryPoint)
    84108{
    85     _cgFP = LoadCgSourceProgram(_cgContext, fileName, CG_PROFILE_FP40, entryPoint);
     109    if (_cgFP != NULL) {
     110        cgDestroyProgram(_cgFP);
     111    }
     112    _cgFP = loadCgSourceProgram(_cgContext, fileName,
     113                                _fragmentProfile, entryPoint);
    86114}
    87115
  • trunk/packages/vizservers/nanovis/NvShader.h

    r2870 r2911  
    33#define NV_SHADER_H
    44
     5#include <GL/glew.h>
    56#include <Cg/cg.h>
     7#include <Cg/cgGL.h>
    68
    7 extern CGprogram LoadCgSourceProgram(CGcontext context, const char *filename,
    8                                      CGprofile profile, const char *entryPoint);
     9#include "Trace.h"
     10
     11extern CGprogram
     12LoadCgSourceProgram(CGcontext context, const char *fileName,
     13                    CGprofile profile, const char *entryPoint)
    914
    1015class NvShader
     
    4752    }
    4853
     54    void setTextureParameter(CGparameter param, GLuint texobj)
     55    {
     56        cgGLSetTextureParameter(param, texobj);
     57    }
     58
    4959    CGprogram getVP() const
    5060    {
     
    5767    }
    5868
    59     static void initCg();
     69    virtual void bind()
     70    {
     71        if (_cgVP) {
     72            cgGLBindProgram(_cgVP);
     73            enableVertexProfile();
     74        }
     75        if (_cgFP) {
     76            cgGLBindProgram(_cgFP);
     77            enableFragmentProfile();
     78        }
     79    }
     80
     81    virtual void unbind()
     82    {
     83        if (_cgVP)
     84            disableVertexProfile();
     85        if (_cgFP)
     86            disableFragmentProfile();
     87    }
     88
     89    void enableVertexProfile()
     90    {
     91        cgGLEnableProfile(_vertexProfile);
     92    }
     93
     94    void disableVertexProfile()
     95    {
     96        cgGLDisableProfile(_vertexProfile);
     97    }
     98
     99    void enableFragmentProfile()
     100    {
     101        cgGLEnableProfile(_fragmentProfile);
     102    }
     103
     104    void disableFragmentProfile()
     105    {
     106        cgGLDisableProfile(_fragmentProfile);
     107    }
     108
     109    static void initCg(CGprofile defaultVertexProfile = CG_PROFILE_VP40,
     110                       CGprofile defaultFragmentProfile = CG_PROFILE_FP40);
    60111
    61112    static void exitCg();
     
    73124    void resetPrograms();
    74125
     126    CGprofile _vertexProfile;
     127    CGprofile _fragmentProfile;
    75128    CGprogram _cgVP;
    76129    CGprogram _cgFP;
    77130
     131    static CGprofile _defaultVertexProfile;
     132    static CGprofile _defaultFragmentProfile;
    78133    static CGcontext _cgContext;
     134
     135private:
     136    static CGprogram
     137    loadCgSourceProgram(CGcontext context, const char *filename,
     138                        CGprofile profile, const char *entryPoint);
    79139};
    80140
Note: See TracChangeset for help on using the changeset viewer.