/src/ogre/OgreMain/src/OgreGpuProgramUsage.cpp
Line | Count | Source (jump to first uncovered line) |
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 | | #include "OgreStableHeaders.h" |
29 | | #include "OgreGpuProgramUsage.h" |
30 | | #include "OgreGpuProgramManager.h" |
31 | | |
32 | | namespace Ogre |
33 | | { |
34 | | //----------------------------------------------------------------------------- |
35 | | GpuProgramUsage::GpuProgramUsage(GpuProgramType gptype, Pass* parent) : |
36 | 0 | mParent(parent), mProgram(), mRecreateParams(false),mType(gptype) |
37 | 0 | { |
38 | 0 | } |
39 | | //----------------------------------------------------------------------------- |
40 | | GpuProgramUsage::GpuProgramUsage(const GpuProgramUsage& oth, Pass* parent) |
41 | 0 | : mParent(parent) |
42 | 0 | , mProgram(oth.mProgram) |
43 | | // nfz: parameters should be copied not just use a shared ptr to the original |
44 | 0 | , mParameters(OGRE_NEW GpuProgramParameters(*oth.mParameters)) |
45 | 0 | , mRecreateParams(false) |
46 | 0 | , mType(oth.mType) |
47 | 0 | { |
48 | 0 | } |
49 | | //--------------------------------------------------------------------- |
50 | | GpuProgramUsage::~GpuProgramUsage() |
51 | 0 | { |
52 | 0 | if (mProgram) |
53 | 0 | mProgram->removeListener(this); |
54 | 0 | } |
55 | | //----------------------------------------------------------------------------- |
56 | | |
57 | | GpuProgramPtr GpuProgramUsage::_getProgramByName(const String& name, const String& group, |
58 | | GpuProgramType type) |
59 | 0 | { |
60 | 0 | GpuProgramPtr program = |
61 | 0 | GpuProgramManager::getSingleton().getByName(name, group); |
62 | | |
63 | | //Look again without the group if not found |
64 | 0 | if (!program) |
65 | 0 | program = GpuProgramManager::getSingleton().getByName( |
66 | 0 | name, ResourceGroupManager::AUTODETECT_RESOURCE_GROUP_NAME); |
67 | |
|
68 | 0 | if (!program) |
69 | 0 | { |
70 | 0 | String progType = GpuProgram::getProgramTypeName(type); |
71 | 0 | OGRE_EXCEPT(Exception::ERR_ITEM_NOT_FOUND, |
72 | 0 | "Unable to locate " + progType + " program called " + name); |
73 | 0 | } |
74 | | |
75 | 0 | return program; |
76 | 0 | } |
77 | | |
78 | | void GpuProgramUsage::setProgramName(const String& name, bool resetParams) |
79 | 0 | { |
80 | 0 | setProgram(_getProgramByName(name, mParent->getResourceGroup(), mType), resetParams); |
81 | 0 | } |
82 | | //----------------------------------------------------------------------------- |
83 | | void GpuProgramUsage::setParameters(const GpuProgramParametersSharedPtr& params) |
84 | 0 | { |
85 | 0 | mParameters = params; |
86 | 0 | } |
87 | | //----------------------------------------------------------------------------- |
88 | | const GpuProgramParametersSharedPtr& GpuProgramUsage::getParameters(void) const |
89 | 0 | { |
90 | 0 | if (!mParameters) |
91 | 0 | { |
92 | 0 | OGRE_EXCEPT(Exception::ERR_INVALIDPARAMS, "You must specify a program before " |
93 | 0 | "you can retrieve parameters.", "GpuProgramUsage::getParameters"); |
94 | 0 | } |
95 | | |
96 | 0 | return mParameters; |
97 | 0 | } |
98 | | //----------------------------------------------------------------------------- |
99 | | void GpuProgramUsage::setProgram(const GpuProgramPtr& prog, bool resetParams) |
100 | 0 | { |
101 | 0 | if (mProgram) |
102 | 0 | { |
103 | 0 | mProgram->removeListener(this); |
104 | 0 | mRecreateParams = true; |
105 | 0 | } |
106 | |
|
107 | 0 | mProgram = prog; |
108 | | |
109 | | // Reset parameters |
110 | 0 | if (resetParams || !mParameters || mRecreateParams) |
111 | 0 | { |
112 | 0 | recreateParameters(); |
113 | 0 | } |
114 | | |
115 | | // Listen in on reload events so we can regenerate params |
116 | 0 | mProgram->addListener(this); |
117 | 0 | } |
118 | | //----------------------------------------------------------------------------- |
119 | | size_t GpuProgramUsage::calculateSize(void) const |
120 | 0 | { |
121 | 0 | size_t memSize = sizeof(*this); |
122 | | |
123 | | // Tally up passes |
124 | 0 | if(mProgram) |
125 | 0 | memSize += mProgram->calculateSize(); |
126 | 0 | if(mParameters) |
127 | 0 | memSize += mParameters->calculateSize(); |
128 | |
|
129 | 0 | return memSize; |
130 | 0 | } |
131 | | //----------------------------------------------------------------------------- |
132 | | void GpuProgramUsage::_load(void) |
133 | 0 | { |
134 | 0 | if (!mProgram->isLoaded()) |
135 | 0 | mProgram->load(); |
136 | | |
137 | | // check type |
138 | 0 | if (mProgram->isLoaded() && mProgram->getType() != mType) |
139 | 0 | { |
140 | 0 | String myType = GpuProgram::getProgramTypeName(mType); |
141 | 0 | String yourType = GpuProgram::getProgramTypeName(mProgram->getType()); |
142 | |
|
143 | 0 | OGRE_EXCEPT(Exception::ERR_INVALIDPARAMS, |
144 | 0 | mProgram->getName() + " is a " + yourType + " program, but you are assigning it to a " |
145 | 0 | + myType + " program slot. This is invalid"); |
146 | |
|
147 | 0 | } |
148 | 0 | } |
149 | | //----------------------------------------------------------------------------- |
150 | | void GpuProgramUsage::_unload(void) |
151 | 0 | { |
152 | | // TODO? |
153 | 0 | } |
154 | | //--------------------------------------------------------------------- |
155 | | void GpuProgramUsage::unloadingComplete(Resource* prog) |
156 | 0 | { |
157 | 0 | mRecreateParams = true; |
158 | |
|
159 | 0 | } |
160 | | //--------------------------------------------------------------------- |
161 | | void GpuProgramUsage::loadingComplete(Resource* prog) |
162 | 0 | { |
163 | | // Need to re-create parameters |
164 | 0 | if (mRecreateParams) |
165 | 0 | recreateParameters(); |
166 | |
|
167 | 0 | } |
168 | | //--------------------------------------------------------------------- |
169 | | void GpuProgramUsage::recreateParameters() |
170 | 0 | { |
171 | | // Keep a reference to old ones to copy |
172 | 0 | GpuProgramParametersSharedPtr savedParams = mParameters; |
173 | | |
174 | | // Create new params |
175 | 0 | mParameters = mProgram->createParameters(); |
176 | | |
177 | | // Copy old (matching) values across |
178 | | // Don't use copyConstantsFrom since program may be different |
179 | 0 | if (savedParams) |
180 | 0 | mParameters->copyMatchingNamedConstantsFrom(*savedParams.get()); |
181 | |
|
182 | 0 | mRecreateParams = false; |
183 | |
|
184 | 0 | } |
185 | | |
186 | | |
187 | | |
188 | | } |