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 | |
---|
13 | using namespace nv::util; |
---|
14 | |
---|
15 | CGprofile NvShader::_defaultVertexProfile = CG_PROFILE_VP40; |
---|
16 | CGprofile NvShader::_defaultFragmentProfile = CG_PROFILE_FP40; |
---|
17 | CGcontext NvShader::_cgContext = NULL; |
---|
18 | |
---|
19 | void NvShader::initCg(CGprofile defaultVertexProfile, |
---|
20 | CGprofile defaultFragmentProfile) |
---|
21 | { |
---|
22 | _defaultVertexProfile = defaultVertexProfile; |
---|
23 | _defaultFragmentProfile = defaultFragmentProfile; |
---|
24 | _cgContext = cgCreateContext(); |
---|
25 | } |
---|
26 | |
---|
27 | void NvShader::exitCg() |
---|
28 | { |
---|
29 | setErrorCallback(NULL); |
---|
30 | printErrorInfo(); |
---|
31 | if (_cgContext != NULL) { |
---|
32 | TRACE("Before DestroyContext"); |
---|
33 | cgDestroyContext(_cgContext); |
---|
34 | TRACE("After DestroyContext"); |
---|
35 | _cgContext = NULL; |
---|
36 | } |
---|
37 | } |
---|
38 | |
---|
39 | bool NvShader::printErrorInfo() |
---|
40 | { |
---|
41 | CGerror lastError = cgGetError(); |
---|
42 | |
---|
43 | if (lastError) { |
---|
44 | TRACE("Cg Error: %s", cgGetErrorString(lastError)); |
---|
45 | if (getCgContext()) |
---|
46 | TRACE("%s", cgGetLastListing(getCgContext())); |
---|
47 | return false; |
---|
48 | } |
---|
49 | return true; |
---|
50 | } |
---|
51 | |
---|
52 | CGprogram |
---|
53 | NvShader::loadCgSourceProgram(CGcontext context, const char *fileName, |
---|
54 | CGprofile profile, const char *entryPoint) |
---|
55 | { |
---|
56 | std::string path = FilePath::getInstance()->getPath(fileName); |
---|
57 | if (path.empty()) { |
---|
58 | ERROR("can't find program \"%s\"", fileName); |
---|
59 | } |
---|
60 | TRACE("cg program compiling: %s", path.c_str()); |
---|
61 | CGprogram program; |
---|
62 | program = cgCreateProgramFromFile(context, CG_SOURCE, path.c_str(), profile, |
---|
63 | entryPoint, NULL); |
---|
64 | cgGLLoadProgram(program); |
---|
65 | CGerror LastError = cgGetError(); |
---|
66 | if (LastError) { |
---|
67 | ERROR("Error message: %s", cgGetLastListing(context)); |
---|
68 | } |
---|
69 | TRACE("successfully compiled program: %s", path.c_str()); |
---|
70 | return program; |
---|
71 | } |
---|
72 | |
---|
73 | NvShader::NvShader(): |
---|
74 | _vertexProfile(_defaultVertexProfile), |
---|
75 | _fragmentProfile(_defaultFragmentProfile), |
---|
76 | _cgVP(NULL), |
---|
77 | _cgFP(NULL) |
---|
78 | { |
---|
79 | } |
---|
80 | |
---|
81 | NvShader::~NvShader() |
---|
82 | { |
---|
83 | TRACE("In ~NvShader"); |
---|
84 | if (_cgContext == NULL) { |
---|
85 | TRACE("Lost Cg context: vp: %s, fp: %s", _vpFile.c_str(), _fpFile.c_str()); |
---|
86 | } else { |
---|
87 | resetPrograms(); |
---|
88 | } |
---|
89 | } |
---|
90 | |
---|
91 | void NvShader::loadVertexProgram(const char *fileName, const char *entryPoint) |
---|
92 | { |
---|
93 | if (_cgVP != NULL) { |
---|
94 | cgDestroyProgram(_cgVP); |
---|
95 | } |
---|
96 | _cgVP = loadCgSourceProgram(_cgContext, fileName, |
---|
97 | _vertexProfile, entryPoint); |
---|
98 | _vpFile = fileName; |
---|
99 | } |
---|
100 | |
---|
101 | void NvShader::loadFragmentProgram(const char *fileName, const char *entryPoint) |
---|
102 | { |
---|
103 | if (_cgFP != NULL) { |
---|
104 | cgDestroyProgram(_cgFP); |
---|
105 | } |
---|
106 | _cgFP = loadCgSourceProgram(_cgContext, fileName, |
---|
107 | _fragmentProfile, entryPoint); |
---|
108 | _fpFile = fileName; |
---|
109 | } |
---|
110 | |
---|
111 | void NvShader::resetPrograms() |
---|
112 | { |
---|
113 | if (_cgVP != NULL) { |
---|
114 | TRACE("Destroying vertex program: %s", _vpFile.c_str()); |
---|
115 | cgDestroyProgram(_cgVP); |
---|
116 | } |
---|
117 | |
---|
118 | if (_cgFP != NULL) { |
---|
119 | TRACE("Destroying fragment program: %s", _fpFile.c_str()); |
---|
120 | cgDestroyProgram(_cgFP); |
---|
121 | } |
---|
122 | } |
---|
123 | |
---|
124 | void NvShader::setErrorCallback(NvCgCallbackFunction callback) |
---|
125 | { |
---|
126 | TRACE("NvShader setting error callback to: %p", callback); |
---|
127 | cgSetErrorCallback(callback); |
---|
128 | } |
---|