1 /*
2 -----------------------------------------------------------------------------
3 This source file is part of OGRE
4 (Object-oriented Graphics Rendering Engine)
5 For the latest info, see http://www.ogre3d.org/
6 
7 Copyright (c) 2000-2014 Torus Knot Software Ltd
8 
9 Permission is hereby granted, free of charge, to any person obtaining a copy
10 of this software and associated documentation files (the "Software"), to deal
11 in the Software without restriction, including without limitation the rights
12 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 copies of the Software, and to permit persons to whom the Software is
14 furnished to do so, subject to the following conditions:
15 
16 The above copyright notice and this permission notice shall be included in
17 all copies or substantial portions of the Software.
18 
19 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 THE SOFTWARE.
26 -----------------------------------------------------------------------------
27 */
28 
29 #include "OgreGLGpuNvparseProgram.h"
30 #include "OgreException.h"
31 #include "OgreRoot.h"
32 #include "OgreRenderSystem.h"
33 #include "OgreRenderSystemCapabilities.h"
34 #include "OgreLogManager.h"
35 #include "nvparse.h"
36 
37 namespace Ogre {
38 
GLGpuNvparseProgram(ResourceManager * creator,const String & name,ResourceHandle handle,const String & group,bool isManual,ManualResourceLoader * loader)39 GLGpuNvparseProgram::GLGpuNvparseProgram(ResourceManager* creator,
40         const String& name, ResourceHandle handle,
41         const String& group, bool isManual, ManualResourceLoader* loader)
42         : GLGpuProgram(creator, name, handle, group, isManual, loader)
43 {
44     mProgramID = glGenLists(1);
45 }
46 
~GLGpuNvparseProgram()47 GLGpuNvparseProgram::~GLGpuNvparseProgram()
48 {
49     // have to call this here reather than in Resource destructor
50     // since calling virtual methods in base destructors causes crash
51     unload();
52 }
53 
bindProgram(void)54 void GLGpuNvparseProgram::bindProgram(void)
55 {
56      glCallList(mProgramID);
57      glEnable(GL_TEXTURE_SHADER_NV);
58      glEnable(GL_REGISTER_COMBINERS_NV);
59      glEnable(GL_PER_STAGE_CONSTANTS_NV);
60 }
61 
unbindProgram(void)62 void GLGpuNvparseProgram::unbindProgram(void)
63 {
64 
65     glDisable(GL_TEXTURE_SHADER_NV);
66     glDisable(GL_REGISTER_COMBINERS_NV);
67     glDisable(GL_PER_STAGE_CONSTANTS_NV);
68 }
69 
bindProgramParameters(GpuProgramParametersSharedPtr params,uint16 mask)70 void GLGpuNvparseProgram::bindProgramParameters(GpuProgramParametersSharedPtr params, uint16 mask)
71 {
72     // NB, register combiners uses 2 constants per texture stage (0 and 1)
73     // We have stored these as (stage * 2) + const_index in the physical buffer
74     // There are no other parameters in a register combiners shader
75     const FloatConstantList& floatList =
76         params->getFloatConstantList();
77     size_t index = 0;
78     for (FloatConstantList::const_iterator i = floatList.begin();
79         i != floatList.end(); ++i, ++index)
80     {
81         GLenum combinerStage = GL_COMBINER0_NV + (unsigned int)(index / 2);
82         GLenum pname = GL_CONSTANT_COLOR0_NV + (index % 2);
83         glCombinerStageParameterfvNV(combinerStage, pname, &(*i));
84 
85     }
86 
87 }
unloadImpl(void)88 void GLGpuNvparseProgram::unloadImpl(void)
89 {
90     glDeleteLists(mProgramID,1);
91 }
92 
loadFromSource(void)93 void GLGpuNvparseProgram::loadFromSource(void)
94 {
95     glNewList(mProgramID, GL_COMPILE);
96 
97     String::size_type pos = mSource.find("!!");
98 
99     while (pos != String::npos) {
100         String::size_type newPos = mSource.find("!!", pos + 1);
101 
102         String script = mSource.substr(pos, newPos - pos);
103         nvparse(script.c_str(), 0);
104 
105         for (char* const * errors= nvparse_get_errors(); *errors; errors++)
106         {
107             LogManager::getSingleton().logMessage("Warning: nvparse reported the following errors:");
108             LogManager::getSingleton().logMessage("\t" + String(*errors));
109         }
110 
111         pos = newPos;
112     }
113 
114     glEndList();
115 }
116 
117 }