Przeglądaj źródła

Texture uniform binding in new renderer, made textures and cubemaps regular shader parameters, removed old fixed function texture stuff from SceneMesh

Ivan Safrin 10 lat temu
rodzic
commit
e69fbb7b0c

+ 2 - 0
Core/Contents/Include/PolyOpenGLGraphicsInterface.h

@@ -65,6 +65,7 @@ namespace Polycode {
         void useShader(Shader *shader);
         void createProgram(ShaderProgram *program);
         void createShader(Shader *shader);
+        void beginDrawCall();
         
         void drawIndices(int type, IndexDataArray *indexArray);
         void drawArrays(int type, unsigned int vertexCount);
@@ -74,6 +75,7 @@ namespace Polycode {
         
 	protected:
 		
+        int textureIndex;
         static GLenum getGLDrawMode(int polycodeMode);
         static int getPolycodeParamType(int glType);
         static int getAttributeSize(int glType);

+ 2 - 0
Core/Contents/Include/PolyRenderer.h

@@ -56,6 +56,8 @@ namespace Polycode {
             virtual void enableDepthTest(bool val) = 0;
             virtual void enableDepthWrite(bool val) = 0;
         
+            virtual void beginDrawCall() = 0;
+        
     };
     
     class _PolyExport RendererThreadJob {

+ 0 - 14
Core/Contents/Include/PolySceneMesh.h

@@ -83,26 +83,12 @@ namespace Polycode {
 			* Returns the Mesh instance of the actual mesh.
 			*/
 			Mesh *getMesh();
-		
-			/**
-			* Returns the texture applied.
-			*/				
-			Texture *getTexture() const;
 			
 			/**
 			* Returns the material applied.
 			*/							
 			Material *getMaterial();
 			
-			/**
-			* Loads a simple texture from a file name and applies it to the mesh.
-			* @param fileName Filename to load the mesh from.
-			* @param clamp If true, clamps the texture to edges. See Texture for details on that.
-			*/
-			void loadTexture(const String& fileName);
-
-			void loadTextureFromImage(Image *image);
-			
 			/**
 			* Loads a skeleton from a file and applies it to the scene mesh.
 			* @param fileName Filename to load the skeleton from.

+ 7 - 11
Core/Contents/Include/PolyShader.h

@@ -51,6 +51,8 @@ namespace Polycode {
         static const int PARAM_VECTOR3 = 3;
         static const int PARAM_COLOR = 4;
         static const int PARAM_MATRIX = 5;
+        static const int PARAM_TEXTURE = 6;
+        static const int PARAM_CUBEMAP = 7;
 	};
 	
     class _PolyExport ProgramAttribute {
@@ -155,6 +157,9 @@ namespace Polycode {
             void setMatrix4(Matrix4 x);
             void setColor(Color x);
         
+            void setTexture(Texture *texture);
+            Texture *getTexture();
+        
             void setParamValueFromString(int type, String pvalue);
 	};
     
@@ -182,13 +187,6 @@ namespace Polycode {
 			virtual ~ShaderBinding();
         
             void copyTo(ShaderBinding *targetBinding);
-
-            Texture *getTexture(const String& name);
-            Cubemap *getCubemap(const String& name);
-			void clearTexture(const String& name);
-			void clearCubemap(const String& name);
-            void addTexture(const String& name, Texture *texture);
-            void addCubemap(const String& name, Cubemap *cubemap);
         
 			LocalShaderParam *addParam(int type, const String& name);
 			LocalShaderParam *addParamPointer(int type, const String& name, void *ptr);        
@@ -216,12 +214,10 @@ namespace Polycode {
 
 			unsigned int getNumOutTargetBindings();
 			RenderTargetBinding *getOutTargetBinding(unsigned int index);
-			            
-            std::vector<TextureBinding> textures;
-            std::vector<CubemapBinding> cubemaps;
-	
+        
 			std::vector<LocalShaderParam*> localParams;
 			std::vector<AttributeBinding*> attributes;
+        
 			std::vector<RenderTargetBinding*> renderTargetBindings;
 			std::vector<RenderTargetBinding*> inTargetBindings;
 			std::vector<RenderTargetBinding*> outTargetBindings;

+ 6 - 3
Core/Contents/Source/PolyMaterialManager.cpp

@@ -568,7 +568,8 @@ Material *MaterialManager::materialFromXMLNode(ResourcePool *resourcePool, TiXml
 								}
 								
 								if(newBinding->mode == RenderTargetBinding::MODE_IN) {
-									newShaderBinding->addTexture(newBinding->name, newBinding->texture);
+                                    // RENDERER_TODO
+									//newShaderBinding->addTexture(newBinding->name, newBinding->texture);
 								}
 							}						
 						}
@@ -584,7 +585,8 @@ Material *MaterialManager::materialFromXMLNode(ResourcePool *resourcePool, TiXml
 									tname =  pChild2Element->Attribute("name");
 								}
 								Texture *texture = CoreServices::getInstance()->getMaterialManager()->createTextureFromFile(pChild2Element->GetText());
-								newShaderBinding->addTexture(tname,texture);
+                                    // RENDERER_TODO
+								//newShaderBinding->addTexture(tname,texture);
 //								newShaderBinding->addTexture(tname, (Texture*)CoreServices::getInstance()->getResourceManager()->getResource(Resource::RESOURCE_TEXTURE, pChild2Element->GetText()));
 							}
 							
@@ -593,7 +595,8 @@ Material *MaterialManager::materialFromXMLNode(ResourcePool *resourcePool, TiXml
 								if(pChild2Element->Attribute("name")) {
 									tname =  pChild2Element->Attribute("name");
 								}
-								newShaderBinding->addCubemap(tname, (Cubemap*)resourcePool->getResource(Resource::RESOURCE_CUBEMAP, pChild2Element->GetText()));
+                                // RENDERER_TODO
+								//newShaderBinding->addCubemap(tname, (Cubemap*)resourcePool->getResource(Resource::RESOURCE_CUBEMAP, pChild2Element->GetText()));
 							}
 							
 						}

+ 34 - 17
Core/Contents/Source/PolyOpenGLGraphicsInterface.cpp

@@ -108,10 +108,31 @@ void OpenGLGraphicsInterface::setParamInShader(Shader *shader, const ProgramPara
                 Matrix4 defaultMatrix;
                 setUniformMatrix(paramLocation, defaultMatrix);
             }
-            break;
+        break;
+        case ProgramParam::PARAM_TEXTURE:
+            
+            glUniform1i(paramLocation, textureIndex);
+            glActiveTexture(GL_TEXTURE0 + textureIndex);
+            
+            if(localParam) {
+                Texture* texture = localParam->getTexture();
+                glBindTexture(GL_TEXTURE_2D, *((GLuint*) texture->platformData));
+            } else {
+                glBindTexture(GL_TEXTURE_2D, 0);
+            }
+            
+            textureIndex++;
+        break;
+        case ProgramParam::PARAM_CUBEMAP:
+            // RENDERER_TODO
+        break;
     }
 }
 
+void OpenGLGraphicsInterface::beginDrawCall() {
+    textureIndex = 0;
+}
+
 void OpenGLGraphicsInterface::useShader(Shader *shader) {
     GLuint shaderID = *((GLuint*) shader->platformData);
     glUseProgram(shaderID);
@@ -341,21 +362,11 @@ void OpenGLGraphicsInterface::createShader(Shader *shader) {
         char name[128];
         glGetActiveUniform(shaderID, GLuint(i), sizeof(name)-1, &name_len, &num, &type, name );
         name[name_len] = 0;
-        
-        switch(type) {
-            case GL_SAMPLER_2D:
-                shader->expectedTextures.push_back(String(name));
-                break;
-            case GL_SAMPLER_CUBE:
-                shader->expectedCubemaps.push_back(String(name));
-                break;
-            default:
-                ProgramParam param;
-                param.name = String(name);
-                param.type = getPolycodeParamType(type);
-                shader->expectedParams.push_back(param);
-            break;
-        }
+   
+        ProgramParam param;
+        param.name = String(name);
+        param.type = getPolycodeParamType(type);
+        shader->expectedParams.push_back(param);
     }
     
     total = -1;
@@ -492,8 +503,14 @@ int OpenGLGraphicsInterface::getPolycodeParamType(int glType) {
         case GL_FLOAT_MAT4:
             return ProgramParam::PARAM_MATRIX;		
             break;
+        case GL_SAMPLER_2D:
+            return ProgramParam::PARAM_TEXTURE;
+        break;
+        case GL_SAMPLER_CUBE:
+            return ProgramParam::PARAM_CUBEMAP;
+        break;
         default:
             return ProgramParam::PARAM_UNKNOWN;
-            break;
+        break;
     }
 }

+ 1 - 2
Core/Contents/Source/PolyRenderer.cpp

@@ -77,7 +77,7 @@ void RenderThread::processDrawBuffer(GPUDrawBuffer *buffer) {
     
     for(int i=0; i < buffer->drawCalls.size(); i++) {
         
-        
+        interface->beginDrawCall();
         interface->enableDepthTest(buffer->drawCalls[i].options.depthTest);
         interface->enableDepthWrite(buffer->drawCalls[i].options.depthWrite);
         
@@ -100,7 +100,6 @@ void RenderThread::processDrawBuffer(GPUDrawBuffer *buffer) {
                 // !!!!!!!!!!!!!!!!!!!!!!!!
                 
                 // set shader uniforms
-                
    
                 for(int p=0; p < shader->expectedParams.size(); p++) {
                     ProgramParam param = shader->expectedParams[p];

+ 12 - 2
Core/Contents/Source/PolySceneEntityInstance.cpp

@@ -204,12 +204,18 @@ void SceneEntityInstance::applySceneMesh(ObjectEntry *entry, SceneMesh *sceneMes
                                             Cubemap *cubemap;
                                             
                                             cubemap = (Cubemap*)topLevelResourcePool->getResource(Resource::RESOURCE_CUBEMAP, textureEntry->stringVal);
-                                                
-                                                                                      if(cubemap) {
+                                            
+                                            // RENDERER_TODO
+                                            /*
+                                            if(cubemap) {
                                                 sceneMesh->getLocalShaderOptions()->addCubemap(nameEntry->stringVal, cubemap);
                                             }
+                                             */
                                         } else {
+                                             // RENDERER_TODO
+                                            /*
                                             sceneMesh->getLocalShaderOptions()->addTexture(nameEntry->stringVal, CoreServices::getInstance()->getMaterialManager()->createTextureFromFile(textureEntry->stringVal));
+                                             */
                                         }
                                     }
                                 }
@@ -356,10 +362,14 @@ Entity *SceneEntityInstance::loadObjectEntryIntoEntity(ObjectEntry *entry, Entit
             label->snapToPixels = false;
             label->positionAtBaseline = false;
             applySceneMesh((*entry)["SceneMesh"], label);
+            
+            // RENDERER_TODO
+            /*
             if(label->getLocalShaderOptions()) {
                 label->getLocalShaderOptions()->clearTexture("diffuse");
                 label->getLocalShaderOptions()->addTexture("diffuse", label->getTexture());
             }
+            */
             
 			entity = label;
         } else if(entityType->stringVal == "SceneParticleEmitter") {

+ 10 - 5
Core/Contents/Source/PolySceneImage.cpp

@@ -35,7 +35,9 @@ SceneImage* SceneImage::SceneImageWithTexture(Texture *texture) {
 }
 
 SceneImage::SceneImage(const String& fileName) : ScenePrimitive(ScenePrimitive::TYPE_VPLANE, 1, 1) {
-	loadTexture(fileName);
+    
+    // RENDERER_TODO
+	//loadTexture(fileName);
 
 	imageWidth = texture->getWidth();
 	imageHeight = texture->getHeight();
@@ -46,7 +48,8 @@ SceneImage::SceneImage(const String& fileName) : ScenePrimitive(ScenePrimitive::
 }
 
 SceneImage::SceneImage(Image *image) : ScenePrimitive(ScenePrimitive::TYPE_VPLANE, 1, 1) {
-	loadTextureFromImage(image);
+    // RENDERER_TODO
+	//loadTextureFromImage(image);
 
 	imageWidth = texture->getWidth();
 	imageHeight = texture->getHeight();
@@ -72,9 +75,11 @@ SceneImage::~SceneImage() {
 }
 
 Entity *SceneImage::Clone(bool deepClone, bool ignoreEditorOnly) const {
-	SceneImage *newImage = new SceneImage(getTexture()->getResourcePath());
-	applyClone(newImage, deepClone, ignoreEditorOnly);
-	return newImage;
+    // RENDERER_TODO
+	//SceneImage *newImage = new SceneImage(getTexture()->getResourcePath());
+//	applyClone(newImage, deepClone, ignoreEditorOnly);
+//	return newImage;
+    return NULL;
 }
 
 void SceneImage::applyClone(Entity *clone, bool deepClone, bool ignoreEditorOnly) const {

+ 3 - 1
Core/Contents/Source/PolySceneLabel.cpp

@@ -109,11 +109,13 @@ void SceneLabel::updateFromLabel() {
 		texture = materialManager->createTextureFromImage(label, materialManager->clampDefault, false);		
 	}
 
+    // RENDERER_TODO
+    /*
 	if(material) {
 		localShaderOptions->clearTexture("diffuse");
 		localShaderOptions->addTexture("diffuse", texture);	
 	}
-
+     */
 
 	setPrimitiveOptions(type, label->getWidth()*labelScale/CoreServices::getInstance()->getRenderer()->getBackingResolutionScaleX(),label->getHeight()*labelScale/CoreServices::getInstance()->getRenderer()->getBackingResolutionScaleX());
 	

+ 1 - 21
Core/Contents/Source/PolySceneMesh.cpp

@@ -188,12 +188,7 @@ void SceneMesh::setMaterial(Material *material) {
     localShaderOptions = new ShaderBinding();
     
     localShaderOptions->addAttributeBinding("texCoord", &mesh->vertexTexCoordArray);
-    localShaderOptions->addAttributeBinding("position", &mesh->vertexPositionArray);
-    
-	if(texture) {
-		localShaderOptions->clearTexture("diffuse");
-		localShaderOptions->addTexture("diffuse", texture);
-	}
+    localShaderOptions->addAttributeBinding("position", &mesh->vertexPositionArray);    
 	
 }
 
@@ -209,21 +204,6 @@ void SceneMesh::setMaterialByName(const String& materialName, ResourcePool *reso
     setMaterial(material);
 }
 
-Texture *SceneMesh::getTexture() const {
-	return texture;
-}
-
-
-void SceneMesh::loadTexture(const String& fileName) {
-	MaterialManager *materialManager = CoreServices::getInstance()->getMaterialManager();
-	texture = materialManager->createTextureFromFile(fileName, materialManager->clampDefault, materialManager->mipmapsDefault);
-}
-
-void SceneMesh::loadTextureFromImage(Image *image) {
-	MaterialManager *materialManager = CoreServices::getInstance()->getMaterialManager();
-	texture = materialManager->createTextureFromImage(image, materialManager->clampDefault, materialManager->mipmapsDefault);	
-}
-
 ShaderBinding *SceneMesh::getLocalShaderOptions() {
 	return localShaderOptions;
 }

+ 3 - 1
Core/Contents/Source/PolySceneSprite.cpp

@@ -132,11 +132,13 @@ void SceneSprite::setSpriteSet(SpriteSet *spriteSet) {
     
     setTexture(spriteSet->getTexture());
     
+    // RENDERER_TODO
+    /*
     if(getLocalShaderOptions()) {
         getLocalShaderOptions()->clearTexture("diffuse");
         getLocalShaderOptions()->addTexture("diffuse", getTexture());
     }
-    
+    */
     currentSprite = NULL;
     currentSpriteState = NULL;
 }

+ 8 - 59
Core/Contents/Source/PolyShader.cpp

@@ -196,19 +196,10 @@ void ShaderBinding::removeRenderTargetBinding(RenderTargetBinding *binding) {
 }
 
 void ShaderBinding::copyTo(ShaderBinding *targetBinding) {
-    for(int i=0; i < textures.size(); i++) {
-        targetBinding->textures.push_back(textures[i]);
-    }
-    
-    for(int i=0; i < cubemaps.size(); i++) {
-        targetBinding->cubemaps.push_back(cubemaps[i]);
-    }
-
     for(int i=0; i < localParams.size(); i++) {
         LocalShaderParam *copyParam = localParams[i]->Copy();
         targetBinding->localParams.push_back(copyParam);
     }
-    
 }
 
 unsigned int ShaderBinding::getNumRenderTargetBindings() {
@@ -360,6 +351,14 @@ LocalShaderParam::LocalShaderParam() {
     ownsPointer = true;
 }
 
+void LocalShaderParam::setTexture(Texture *texture) {
+    data = (void*) texture;
+}
+
+Texture *LocalShaderParam::getTexture() {
+    return (Texture*) data;
+}
+
 LocalShaderParam::~LocalShaderParam() {
     if(ownsPointer) {
         switch(type) {
@@ -466,53 +465,3 @@ void LocalShaderParam::setParamValueFromString(int type, String pvalue) {
                 break;
         }
 }
-
-Cubemap *ShaderBinding::getCubemap(const String& name) {
-	for(int i=0; i < cubemaps.size(); i++) {
-		if(cubemaps[i].name == name) {
-			return cubemaps[i].cubemap;
-		}
-	}
-	return NULL;
-}
-
-Texture *ShaderBinding::getTexture(const String& name) {
-	for(int i=0; i < textures.size(); i++) {
-		if(textures[i].name == name) {
-			return textures[i].texture;
-		}
-	}
-	return NULL;
-}
-
-void ShaderBinding::addTexture(const String& name, Texture *texture) {
-	TextureBinding binding;
-	binding.name = name;
-	binding.texture = texture;
-	textures.push_back(binding);
-}
-
-void ShaderBinding::addCubemap(const String& name, Cubemap *cubemap) {
-	CubemapBinding binding;
-	binding.cubemap = cubemap;
-	binding.name = name;
-	cubemaps.push_back(binding);
-}
-
-void ShaderBinding::clearCubemap(const String& name) {
-	for(int i=0; i < cubemaps.size(); i++) {
-		if(cubemaps[i].name == name) {
-			cubemaps.erase(cubemaps.begin()+i);
-			return;
-		}
-	}
-}
-
-void ShaderBinding::clearTexture(const String& name) {
-	for(int i=0; i < textures.size(); i++) {
-		if(textures[i].name == name) {
-			textures.erase(textures.begin()+i);
-			return;
-		}
-	}
-}