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-2006 Torus Knot Software Ltd |
---|
8 | Also see acknowledgements in Readme.html |
---|
9 | |
---|
10 | This program is free software; you can redistribute it and/or modify it under |
---|
11 | the terms of the GNU Lesser General Public License as published by the Free Software |
---|
12 | Foundation; either version 2 of the License, or (at your option) any later |
---|
13 | version. |
---|
14 | |
---|
15 | This program is distributed in the hope that it will be useful, but WITHOUT |
---|
16 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
---|
17 | FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. |
---|
18 | |
---|
19 | You should have received a copy of the GNU Lesser General Public License along with |
---|
20 | this program; if not, write to the Free Software Foundation, Inc., 59 Temple |
---|
21 | Place - Suite 330, Boston, MA 02111-1307, USA, or go to |
---|
22 | http://www.gnu.org/copyleft/lesser.txt. |
---|
23 | |
---|
24 | You may alternatively use this source under the terms of a specific version of |
---|
25 | the OGRE Unrestricted License provided you have obtained such a license from |
---|
26 | Torus Knot Software Ltd. |
---|
27 | ----------------------------------------------------------------------------- |
---|
28 | */ |
---|
29 | |
---|
30 | #include "ps_1_4.h" |
---|
31 | #include "OgreException.h" |
---|
32 | #include "OgreRoot.h" |
---|
33 | #include "OgreRenderSystem.h" |
---|
34 | #include "OgreRenderSystemCapabilities.h" |
---|
35 | #include "OgreLogManager.h" |
---|
36 | #include "ATI_FS_GLGpuProgram.h" |
---|
37 | |
---|
38 | using namespace Ogre; |
---|
39 | |
---|
40 | |
---|
41 | ATI_FS_GLGpuProgram::ATI_FS_GLGpuProgram(ResourceManager* creator, |
---|
42 | const String& name, ResourceHandle handle, |
---|
43 | const String& group, bool isManual, ManualResourceLoader* loader) : |
---|
44 | GLGpuProgram(creator, name, handle, group, isManual, loader) |
---|
45 | { |
---|
46 | mProgramType = GL_FRAGMENT_SHADER_ATI; |
---|
47 | mProgramID = glGenFragmentShadersATI(1); |
---|
48 | } |
---|
49 | |
---|
50 | ATI_FS_GLGpuProgram::~ATI_FS_GLGpuProgram() |
---|
51 | { |
---|
52 | // have to call this here reather than in Resource destructor |
---|
53 | // since calling virtual methods in base destructors causes crash |
---|
54 | unload(); |
---|
55 | } |
---|
56 | |
---|
57 | void ATI_FS_GLGpuProgram::bindProgram(void) |
---|
58 | { |
---|
59 | glEnable(mProgramType); |
---|
60 | glBindFragmentShaderATI(mProgramID); |
---|
61 | } |
---|
62 | |
---|
63 | void ATI_FS_GLGpuProgram::unbindProgram(void) |
---|
64 | { |
---|
65 | glDisable(mProgramType); |
---|
66 | } |
---|
67 | |
---|
68 | |
---|
69 | void ATI_FS_GLGpuProgram::bindProgramParameters(GpuProgramParametersSharedPtr params) |
---|
70 | { |
---|
71 | |
---|
72 | // only supports float constants |
---|
73 | const GpuLogicalBufferStruct* floatStruct = params->getFloatLogicalBufferStruct(); |
---|
74 | |
---|
75 | for (GpuLogicalIndexUseMap::const_iterator i = floatStruct->map.begin(); |
---|
76 | i != floatStruct->map.end(); ++i) |
---|
77 | { |
---|
78 | size_t logicalIndex = i->first; |
---|
79 | const float* pFloat = params->getFloatPointer(i->second.physicalIndex); |
---|
80 | // Iterate over the params, set in 4-float chunks (low-level) |
---|
81 | for (size_t j = 0; j < i->second.currentSize; j+=4) |
---|
82 | { |
---|
83 | glSetFragmentShaderConstantATI(GL_CON_0_ATI + logicalIndex, pFloat); |
---|
84 | pFloat += 4; |
---|
85 | ++logicalIndex; |
---|
86 | } |
---|
87 | } |
---|
88 | |
---|
89 | } |
---|
90 | |
---|
91 | void ATI_FS_GLGpuProgram::bindProgramPassIterationParameters(GpuProgramParametersSharedPtr params) |
---|
92 | { |
---|
93 | if (params->hasPassIterationNumber()) |
---|
94 | { |
---|
95 | size_t physicalIndex = params->getPassIterationNumberIndex(); |
---|
96 | size_t logicalIndex = params->getFloatLogicalIndexForPhysicalIndex(physicalIndex); |
---|
97 | const float* pFloat = params->getFloatPointer(physicalIndex); |
---|
98 | glSetFragmentShaderConstantATI( GL_CON_0_ATI + (GLuint)logicalIndex, pFloat); |
---|
99 | } |
---|
100 | } |
---|
101 | |
---|
102 | |
---|
103 | void ATI_FS_GLGpuProgram::unloadImpl(void) |
---|
104 | { |
---|
105 | glDeleteFragmentShaderATI(mProgramID); |
---|
106 | } |
---|
107 | |
---|
108 | |
---|
109 | void ATI_FS_GLGpuProgram::loadFromSource(void) |
---|
110 | { |
---|
111 | |
---|
112 | PS_1_4 PS1_4Assembler; |
---|
113 | // attempt to compile the source |
---|
114 | #ifdef _DEBUG |
---|
115 | PS1_4Assembler.test(); // run compiler tests in debug mode |
---|
116 | #endif |
---|
117 | |
---|
118 | bool Error = !PS1_4Assembler.compile(mSource.c_str()); |
---|
119 | |
---|
120 | if(!Error) { |
---|
121 | glBindFragmentShaderATI(mProgramID); |
---|
122 | glBeginFragmentShaderATI(); |
---|
123 | // compile was successfull so send the machine instructions thru GL to GPU |
---|
124 | Error = !PS1_4Assembler.bindAllMachineInstToFragmentShader(); |
---|
125 | glEndFragmentShaderATI(); |
---|
126 | |
---|
127 | // check GL for GPU machine instruction bind erros |
---|
128 | if (Error) |
---|
129 | { |
---|
130 | OGRE_EXCEPT(Exception::ERR_INTERNAL_ERROR, |
---|
131 | "Cannot Bind ATI fragment shader :" + mName, mName); |
---|
132 | } |
---|
133 | |
---|
134 | } |
---|
135 | else |
---|
136 | { |
---|
137 | // an error occured when compiling the ps_1_4 source code |
---|
138 | char buff[50]; |
---|
139 | sprintf(buff,"error on line %d in pixel shader source\n", PS1_4Assembler.mCurrentLine); |
---|
140 | |
---|
141 | LogManager::getSingleton().logMessage("Warning: atifs compiler reported the following errors:"); |
---|
142 | LogManager::getSingleton().logMessage(buff + mName); |
---|
143 | |
---|
144 | OGRE_EXCEPT(Exception::ERR_INTERNAL_ERROR, |
---|
145 | "Cannot Compile ATI fragment shader : " + mName + "\n\n" + buff , mName);// + |
---|
146 | } |
---|
147 | |
---|
148 | |
---|
149 | } |
---|
150 | |
---|