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

Last change on this file since 3611 was 3611, checked in by ldelgass, 11 years ago

Use nv namespace for classes in nanovis rather than prefixing class names with
Nv (still need to convert shader classes).

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