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 | CGcontext NvShader::_cgContext = NULL; |
---|
14 | |
---|
15 | void NvShader::initCg() |
---|
16 | { |
---|
17 | _cgContext = cgCreateContext(); |
---|
18 | } |
---|
19 | |
---|
20 | void NvShader::exitCg() |
---|
21 | { |
---|
22 | setErrorCallback(NULL); |
---|
23 | printErrorInfo(); |
---|
24 | if (_cgContext != NULL) { |
---|
25 | cgDestroyContext(_cgContext); |
---|
26 | _cgContext = NULL; |
---|
27 | } |
---|
28 | } |
---|
29 | |
---|
30 | bool NvShader::printErrorInfo() |
---|
31 | { |
---|
32 | CGerror lastError = cgGetError(); |
---|
33 | |
---|
34 | if (lastError) { |
---|
35 | TRACE("Cg Error: %s\n", cgGetErrorString(lastError)); |
---|
36 | if (getCgContext()) |
---|
37 | TRACE("%s\n", cgGetLastListing(getCgContext())); |
---|
38 | return false; |
---|
39 | } |
---|
40 | return true; |
---|
41 | } |
---|
42 | |
---|
43 | CGprogram |
---|
44 | LoadCgSourceProgram(CGcontext context, const char *fileName, CGprofile profile, |
---|
45 | const char *entryPoint) |
---|
46 | { |
---|
47 | const char *path = R2FilePath::getInstance()->getPath(fileName); |
---|
48 | if (path == NULL) { |
---|
49 | ERROR("can't find program \"%s\"\n", fileName); |
---|
50 | } |
---|
51 | TRACE("cg program compiling: %s\n", path); |
---|
52 | CGprogram program; |
---|
53 | program = cgCreateProgramFromFile(context, CG_SOURCE, path, profile, |
---|
54 | entryPoint, NULL); |
---|
55 | cgGLLoadProgram(program); |
---|
56 | CGerror LastError = cgGetError(); |
---|
57 | if (LastError) { |
---|
58 | ERROR("Error message: %s\n", cgGetLastListing(context)); |
---|
59 | } |
---|
60 | TRACE("successfully compiled program: %s\n", path); |
---|
61 | delete [] path; |
---|
62 | return program; |
---|
63 | } |
---|
64 | |
---|
65 | NvShader::NvShader(): |
---|
66 | _cgVP(NULL), |
---|
67 | _cgFP(NULL) |
---|
68 | { |
---|
69 | } |
---|
70 | |
---|
71 | NvShader::~NvShader() |
---|
72 | { |
---|
73 | resetPrograms(); |
---|
74 | } |
---|
75 | |
---|
76 | void NvShader::loadVertexProgram(const char *fileName, const char *entryPoint) |
---|
77 | { |
---|
78 | resetPrograms(); |
---|
79 | |
---|
80 | _cgVP = LoadCgSourceProgram(_cgContext, fileName, CG_PROFILE_VP40, entryPoint); |
---|
81 | } |
---|
82 | |
---|
83 | void NvShader::loadFragmentProgram(const char *fileName, const char *entryPoint) |
---|
84 | { |
---|
85 | _cgFP = LoadCgSourceProgram(_cgContext, fileName, CG_PROFILE_FP40, entryPoint); |
---|
86 | } |
---|
87 | |
---|
88 | void NvShader::resetPrograms() |
---|
89 | { |
---|
90 | if (_cgVP != NULL) { |
---|
91 | cgDestroyProgram(_cgVP); |
---|
92 | } |
---|
93 | |
---|
94 | if (_cgFP != NULL) { |
---|
95 | cgDestroyProgram(_cgFP); |
---|
96 | } |
---|
97 | } |
---|
98 | |
---|
99 | void NvShader::setErrorCallback(NvCgCallbackFunction callback) |
---|
100 | { |
---|
101 | TRACE("NvShader setting error callback to: %p\n", callback); |
---|
102 | cgSetErrorCallback(callback); |
---|
103 | } |
---|