Subversion Repository Public Repository

Divide-Framework

This repository has no backups
This repository's network speed is throttled to 100KB/sec

Diff Revisions 167 vs 168 for /trunk/Source Code/Hardware/Video/Shaders/ShaderProgram.cpp

Diff revisions: vs.
  @@ -8,7 +8,6 @@
8 8 #include "Geometry/Material/Headers/Material.h"
9 9
10 10 ShaderProgram::ShaderProgram(const bool optimise) : HardwareResource(),
11 - _invalidShaderProgramId(std::numeric_limits<U32>::max()),
12 11 _optimise(optimise),
13 12 _useTessellation(false),
14 13 _useGeometry(false),
  @@ -18,18 +17,19 @@
18 17 _bound(false),
19 18 _dirty(true),
20 19 _wasBound(false)
21 - {
22 - _shaderProgramId = _invalidShaderProgramId;
23 - _refreshVert = _refreshFrag = _refreshGeom = _refreshTess = false;
24 - //Enable all matrix uploads by default (note: projection and view matrices are ALWAYS uploaded)
25 - _matrixMask.b.b0 = 1;
26 - _matrixMask.b.b1 = 1;
27 - _matrixMask.b.b2 = 1;
28 - _matrixMask.b.b3 = 1;
29 - _maxCombinedTextureUnits = ParamHandler::getInstance().getParam<I32>("GFX_DEVICE.maxTextureCombinedUnits",16);
30 - }
20 + {
21 + _shaderProgramId = 0;//<Override in concrete implementations with appropriate invalid values
22 + _refreshVert = _refreshFrag = _refreshGeom = _refreshTess = false;
23 + //Enable all matrix uploads by default (note: projection and view matrices are ALWAYS uploaded)
24 + _matrixMask.b.b0 = 1;
25 + _matrixMask.b.b1 = 1;
26 + _matrixMask.b.b2 = 1;
27 + _matrixMask.b.b3 = 1;
28 + _maxCombinedTextureUnits = ParamHandler::getInstance().getParam<I32>("GFX_DEVICE.maxTextureCombinedUnits",16);
29 + }
31 30
32 - ShaderProgram::~ShaderProgram(){
31 + ShaderProgram::~ShaderProgram()
32 + {
33 33 D_PRINT_FN(Locale::get("SHADER_PROGRAM_REMOVE"), getName().c_str());
34 34 for_each(ShaderIdMap::value_type& it, _shaderIdMap){
35 35 ShaderManager::getInstance().removeShader(it.second);
  @@ -40,28 +40,36 @@
40 40
41 41 U8 ShaderProgram::tick(const U32 deltaTime){
42 42 ParamHandler& par = ParamHandler::getInstance();
43 - this->Uniform("enableFog",par.getParam<bool>("rendering.enableFog"));
43 + this->Uniform("dvd_enableFog",par.getParam<bool>("rendering.enableFog"));
44 44 this->Uniform("dvd_lightAmbient", LightManager::getInstance().getAmbientLight());
45 -
45 +
46 46 if(_dirty){
47 + this->Uniform("dvd_zPlanes", Frustum::getInstance().getZPlanes());
48 + this->Uniform("screenDimension", Application::getInstance().getResolution());
49 + U8 shadowMapSlot = Config::MAX_TEXTURE_STORAGE;
47 50 //Apply global shader values valid throughout application runtime:
48 - std::string shadowMaps("texDepthMapFromLight");
49 - std::stringstream ss;
50 - for(I32 i = 0; i < MAX_SHADOW_CASTING_LIGHTS_PER_NODE; i++){
51 - ss.str(std::string());
52 - ss << shadowMaps << i;
53 - this->UniformTexture(ss.str(),10+i); //Shadow Maps always bound from 8 and above
51 + char depthMapSampler[32];
52 + for(I32 i = 0; i < Config::MAX_SHADOW_CASTING_LIGHTS_PER_NODE; i++){
53 + sprintf_s(depthMapSampler, "texDepthMapFromLight%d", i);
54 + //Shadow Maps always bound from the last texture slot upwards
55 + this->UniformTexture(depthMapSampler,shadowMapSlot++);
56 + }
57 + shadowMapSlot = Config::MAX_TEXTURE_STORAGE + Config::MAX_SHADOW_CASTING_LIGHTS_PER_NODE;
58 + //Reserve first for directional shadows
59 + this->UniformTexture("texDepthMapFromLightArray", shadowMapSlot);
60 + //Reserve second for point shadows
61 + this->UniformTexture("texDepthMapFromLightCube", shadowMapSlot + 1);
62 +
63 + this->UniformTexture("texNormalMap", Material::TEXTURE_NORMALMAP);
64 + this->UniformTexture("texOpacityMap", Material::TEXTURE_OPACITY);
65 + this->UniformTexture("texSpecular", Material::TEXTURE_SPECULAR);
66 +
67 + for(U32 i = Material::TEXTURE_UNIT0, j = 0; i < Config::MAX_TEXTURE_STORAGE; ++i)
68 + {
69 + char uniformSlot[32];
70 + sprintf_s(uniformSlot, "texDiffuse%d", j++);
71 + this->UniformTexture(uniformSlot, i);
54 72 }
55 -
56 - this->Uniform("zPlanes", Frustum::getInstance().getZPlanes());
57 - this->Uniform("screenDimension", Application::getInstance().getResolution());
58 - this->UniformTexture("texDepthMapFromLightArray",_maxCombinedTextureUnits - 2); //Reserve first for directional shadows
59 - this->UniformTexture("texDepthMapFromLightCube", _maxCombinedTextureUnits - 1); //Reserve second for point shadows
60 - this->UniformTexture("texSpecular",Material::SPECULAR_TEXTURE_UNIT);
61 - this->UniformTexture("opacityMap",Material::OPACITY_TEXTURE_UNIT);
62 - this->UniformTexture("texBump",Material::BUMP_TEXTURE_UNIT);
63 - this->UniformTexture("texDiffuse1",Material::SECOND_TEXTURE_UNIT);
64 - this->UniformTexture("texDiffuse0", Material::FIRST_TEXTURE_UNIT);
65 73
66 74 this->Uniform("fogColor", vec3<F32>(par.getParam<F32>("rendering.sceneState.fogColor.r"),
67 75 par.getParam<F32>("rendering.sceneState.fogColor.g"),
  @@ -104,13 +112,13 @@
104 112 if(_shaderProgramId == 0) return;
105 113
106 114 //Apply global shader values valid throughout current render call:
107 - this->Uniform("time", static_cast<F32>(GETMSTIME()));
108 - this->Attribute("cameraPosition",Frustum::getInstance().getEyePos());
115 + this->Uniform("dvd_time", static_cast<F32>(GETMSTIME()));
116 + this->Attribute("dvd_cameraPosition",Frustum::getInstance().getEyePos());
109 117 }
110 118
111 119 void ShaderProgram::uploadModelMatrices(){
112 120 GFXDevice& GFX = GFX_DEVICE;
113 - /*Get matrix data*/
121 + /*Get and upload matrix data*/
114 122 if(_matrixMask.b.b0){
115 123 GFX.getMatrix(NORMAL_MATRIX,_cachedNormalMatrix);
116 124 this->Uniform("dvd_NormalMatrix",_cachedNormalMatrix);
  @@ -127,6 +135,28 @@
127 135 GFX.getMatrix(MVP_MATRIX,_cachedMatrix);
128 136 this->Uniform("dvd_ModelViewProjectionMatrix",_cachedMatrix);
129 137 }
138 +
139 + GFX.getMatrix(MV_INV_MATRIX,_cachedMatrix);
140 + this->Uniform("dvd_ModelViewMatrixInverse",_cachedMatrix);
141 +
142 + /*Get and upload clip plane data*/
143 + if(GFX_DEVICE.clippingPlanesDirty()){
144 + GFX_DEVICE.updateClipPlanes();
145 + U32 planeCount = GFX_DEVICE.getClippingPlanes().size();
146 + this->Uniform("dvd_clip_plane_count", (I32)planeCount);
147 + if(planeCount == 0) return;
148 +
149 + _clipPlanes.resize(planeCount);
150 + _clipPlanesStates.resize(planeCount, false);
151 + for(U32 i = 0; i < planeCount; i++){
152 + const Plane<F32>& currentPlane = GFX_DEVICE.getClippingPlanes()[i];
153 + _clipPlanes[i] = currentPlane.getEquation();
154 + _clipPlanesStates[i] = currentPlane.active() ? 1 : 0;
155 + }
156 +
157 + this->Uniform("dvd_clip_plane", _clipPlanes);
158 + this->Uniform("dvd_clip_plane_active",_clipPlanesStates);
159 + }
130 160 }
131 161
132 162
  @@ -150,6 +180,26 @@
150 180 else D_ERROR_FN(Locale::get("ERROR_INVALID_SHADER_DEFINE_DELETE"),define.c_str(),getName().c_str());
151 181 }
152 182
183 + void ShaderProgram::addShaderUniform(const std::string& uniform, const ShaderType& type) {
184 + if(type == FRAGMENT_SHADER)
185 + _fragUniforms.push_back(uniform);
186 + else
187 + _vertUniforms.push_back(uniform);
188 + }
189 +
190 + void ShaderProgram::removeUniform(const std::string& uniform, const ShaderType& type) {
191 + vectorImpl<std::string >::iterator it;
192 + if(type == FRAGMENT_SHADER){
193 + it = find(_fragUniforms.begin(), _fragUniforms.end(), uniform);
194 + if (it != _fragUniforms.end()) _fragUniforms.erase(it);
195 + else D_ERROR_FN(Locale::get("ERROR_INVALID_SHADER_UNIFORM_DELETE"),uniform.c_str(),"fragment", getName().c_str());
196 + }else{
197 + it = find(_vertUniforms.begin(), _vertUniforms.end(), uniform);
198 + if (it != _vertUniforms.end()) _vertUniforms.erase(it);
199 + else D_ERROR_FN(Locale::get("ERROR_INVALID_SHADER_DEFINE_DELETE"),uniform.c_str(), "vertex", getName().c_str());
200 + }
201 + }
202 +
153 203 void ShaderProgram::recompile(const bool vertex,
154 204 const bool fragment,
155 205 const bool geometry,