source: trunk/packages/vizservers/nanovis/NvShader.cpp @ 2911

Last change on this file since 2911 was 2911, checked in by ldelgass, 12 years ago

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.

  • Property svn:eol-style set to native
File size: 3.3 KB
Line 
1/* -*- mode: c++; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2#include <stdio.h>
3
4#include <GL/glew.h>
5#include <Cg/cg.h>
6#include <Cg/cgGL.h>
7
8#include <R2/R2FilePath.h>
9
10#include "NvShader.h"
11#include "Trace.h"
12
13CGprofile NvShader::_defaultVertexProfile = CG_PROFILE_VP40;
14CGprofile NvShader::_defaultFragmentProfile = CG_PROFILE_FP40;
15CGcontext NvShader::_cgContext = NULL;
16
17void NvShader::initCg(CGprofile defaultVertexProfile,
18                      CGprofile defaultFragmentProfile)
19{
20    _defaultVertexProfile = defaultVertexProfile;
21    _defaultFragmentProfile = defaultFragmentProfile;
22    _cgContext = cgCreateContext();
23}
24
25void NvShader::exitCg()
26{
27    setErrorCallback(NULL);
28    printErrorInfo();
29    if (_cgContext != NULL) {
30        TRACE("Before DestroyContext");
31        cgDestroyContext(_cgContext);
32        TRACE("After DestroyContext");
33        _cgContext = NULL;
34    }
35}
36
37bool NvShader::printErrorInfo()
38{
39    CGerror lastError = cgGetError();
40
41    if (lastError) {
42        TRACE("Cg Error: %s\n", cgGetErrorString(lastError));
43        if (getCgContext())
44            TRACE("%s\n", cgGetLastListing(getCgContext()));
45        return false;
46    }
47    return true;
48}
49
50CGprogram
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)
61{
62    const char *path = R2FilePath::getInstance()->getPath(fileName);
63    if (path == NULL) {
64        ERROR("can't find program \"%s\"\n", fileName);
65    }
66    TRACE("cg program compiling: %s\n", path);
67    CGprogram program;
68    program = cgCreateProgramFromFile(context, CG_SOURCE, path, profile,
69                                      entryPoint, NULL);
70    cgGLLoadProgram(program);
71    CGerror LastError = cgGetError();
72    if (LastError) {
73        ERROR("Error message: %s\n", cgGetLastListing(context));
74    }
75    TRACE("successfully compiled program: %s\n", path);
76    delete [] path;
77    return program;
78}
79
80NvShader::NvShader():
81    _vertexProfile(_defaultVertexProfile),
82    _fragmentProfile(_defaultFragmentProfile),
83    _cgVP(NULL),
84    _cgFP(NULL)
85{
86}
87
88NvShader::~NvShader()
89{
90    TRACE("In ~NvShader");
91    if (_cgContext == NULL) {
92        TRACE("Lost Cg context");
93    } else {
94        resetPrograms();
95    }
96}
97
98void NvShader::loadVertexProgram(const char *fileName, const char *entryPoint)
99{
100    if (_cgVP != NULL) {
101        cgDestroyProgram(_cgVP);
102    }
103    _cgVP = loadCgSourceProgram(_cgContext, fileName,
104                                _vertexProfile, entryPoint);
105}
106
107void NvShader::loadFragmentProgram(const char *fileName, const char *entryPoint)
108{
109    if (_cgFP != NULL) {
110        cgDestroyProgram(_cgFP);
111    }
112    _cgFP = loadCgSourceProgram(_cgContext, fileName,
113                                _fragmentProfile, entryPoint);
114}
115
116void NvShader::resetPrograms()
117{
118    if (_cgVP != NULL) {
119        cgDestroyProgram(_cgVP);
120    }
121
122    if (_cgFP != NULL) {
123        cgDestroyProgram(_cgFP);
124    }
125}
126
127void NvShader::setErrorCallback(NvCgCallbackFunction callback)
128{
129    TRACE("NvShader setting error callback to: %p\n", callback);
130    cgSetErrorCallback(callback);
131}
Note: See TracBrowser for help on using the repository browser.