Browse Source

Removed View friend class from Renderer. Made more Renderer functions public, as they can be usable also outside of View.
As they are potentially dangerous, made the execution nesting level related functions private in Script, and added ScriptFile as a friend class.

Lasse Öörni 14 years ago
parent
commit
37621a0c4b
5 changed files with 147 additions and 145 deletions
  1. 111 109
      Engine/Graphics/Renderer.cpp
  2. 17 19
      Engine/Graphics/Renderer.h
  3. 4 4
      Engine/Graphics/View.cpp
  4. 5 5
      Engine/Script/Script.cpp
  5. 10 8
      Engine/Script/Script.h

+ 111 - 109
Engine/Graphics/Renderer.cpp

@@ -673,62 +673,6 @@ void Renderer::DrawDebugGeometry(bool depthTest)
     }
     }
 }
 }
 
 
-void Renderer::Initialize()
-{
-    Graphics* graphics = GetSubsystem<Graphics>();
-    ResourceCache* cache = GetSubsystem<ResourceCache>();
-    
-    if (!graphics || !graphics->IsInitialized() || !cache)
-        return;
-    
-    PROFILE(InitRenderer);
-    
-    graphics_ = graphics;
-    cache_ = cache;
-    
-    // Check shader model support
-    #ifndef USE_OPENGL
-    if (graphics_->GetSM3Support())
-    {
-        shaderPath_ = "Shaders/SM3/";
-        vsFormat_ = ".vs3";
-        psFormat_ = ".ps3";
-    }
-    else
-    {
-        shaderPath_ = "Shaders/SM2/";
-        vsFormat_ = ".vs2";
-        psFormat_ = ".ps2";
-    }
-    
-    #else
-    {
-        shaderPath_ = "Shaders/GLSL/";
-        vsFormat_ = ".vert";
-        psFormat_ = ".frag";
-    }
-    #endif
-    
-    defaultLightRamp_ = cache->GetResource<Texture2D>("Textures/Ramp.png");
-    defaultLightSpot_ = cache->GetResource<Texture2D>("Textures/Spot.png");
-    defaultMaterial_ = cache->GetResource<Material>("Materials/Default.xml");
-    
-    CreateGeometries();
-    CreateInstancingBuffer();
-    
-    viewports_.Resize(1);
-    ResetViews();
-    
-    LOGINFO("Initialized renderer");
-    initialized_ = true;
-}
-
-void Renderer::ResetViews()
-{
-    views_.Clear();
-    numViews_ = 0;
-}
-
 bool Renderer::AddView(RenderSurface* renderTarget, const Viewport& viewport)
 bool Renderer::AddView(RenderSurface* renderTarget, const Viewport& viewport)
 {
 {
     // If using a render target texture, make sure it will not be rendered to multiple times
     // If using a render target texture, make sure it will not be rendered to multiple times
@@ -797,6 +741,7 @@ Geometry* Renderer::GetLightGeometry(Light* light)
         return 0;
         return 0;
 }
 }
 
 
+
 Texture2D* Renderer::GetShadowMap(Light* light, Camera* camera, unsigned viewWidth, unsigned viewHeight)
 Texture2D* Renderer::GetShadowMap(Light* light, Camera* camera, unsigned viewWidth, unsigned viewHeight)
 {
 {
     LightType type = light->GetLightType();
     LightType type = light->GetLightType();
@@ -1087,6 +1032,116 @@ void Renderer::SetBatchShaders(Batch& batch, Technique* technique, Pass* pass, b
     }
     }
 }
 }
 
 
+
+Camera* Renderer::CreateShadowCamera()
+{
+    if (numShadowCameras_ >= shadowCameraStore_.Size())
+        shadowCameraStore_.Push(SharedPtr<Camera>(new Camera(context_)));
+    Camera* camera = shadowCameraStore_[numShadowCameras_];
+    camera->SetNode(CreateTempNode());
+    camera->SetOrthographic(false);
+    camera->SetZoom(1.0f);
+    ++numShadowCameras_;
+    return camera;
+}
+
+Node* Renderer::CreateTempNode()
+{
+    if (numTempNodes_ >= tempNodeStore_.Size())
+        tempNodeStore_.Push(SharedPtr<Node>(new Node(context_)));
+    Node* node = tempNodeStore_[numTempNodes_];
+    
+    ++numTempNodes_;
+    return node;
+}
+
+bool Renderer::ResizeInstancingBuffer(unsigned numInstances)
+{
+    if (!instancingBuffer_ || !dynamicInstancing_)
+        return false;
+    
+    unsigned oldSize = instancingBuffer_->GetVertexCount();
+    if (numInstances <= oldSize)
+        return true;
+    
+    unsigned newSize = INSTANCING_BUFFER_DEFAULT_SIZE;
+    while (newSize < numInstances)
+        newSize <<= 1;
+    
+    if (!instancingBuffer_->SetSize(newSize, INSTANCING_BUFFER_MASK, true))
+    {
+        LOGERROR("Failed to resize instancing buffer to " + String(newSize));
+        // If failed, try to restore the old size
+        instancingBuffer_->SetSize(oldSize, INSTANCING_BUFFER_MASK, true);
+        return false;
+    }
+    
+    LOGDEBUG("Resized instancing buffer to " + String(newSize));
+    return true;
+}
+
+void Renderer::ResetShadowMapAllocations()
+{
+    for (HashMap<int, PODVector<Light*> >::Iterator i = shadowMapAllocations_.Begin(); i != shadowMapAllocations_.End(); ++i)
+        i->second_.Clear();
+}
+
+void Renderer::Initialize()
+{
+    Graphics* graphics = GetSubsystem<Graphics>();
+    ResourceCache* cache = GetSubsystem<ResourceCache>();
+    
+    if (!graphics || !graphics->IsInitialized() || !cache)
+        return;
+    
+    PROFILE(InitRenderer);
+    
+    graphics_ = graphics;
+    cache_ = cache;
+    
+    // Check shader model support
+    #ifndef USE_OPENGL
+    if (graphics_->GetSM3Support())
+    {
+        shaderPath_ = "Shaders/SM3/";
+        vsFormat_ = ".vs3";
+        psFormat_ = ".ps3";
+    }
+    else
+    {
+        shaderPath_ = "Shaders/SM2/";
+        vsFormat_ = ".vs2";
+        psFormat_ = ".ps2";
+    }
+    
+    #else
+    {
+        shaderPath_ = "Shaders/GLSL/";
+        vsFormat_ = ".vert";
+        psFormat_ = ".frag";
+    }
+    #endif
+    
+    defaultLightRamp_ = cache->GetResource<Texture2D>("Textures/Ramp.png");
+    defaultLightSpot_ = cache->GetResource<Texture2D>("Textures/Spot.png");
+    defaultMaterial_ = cache->GetResource<Material>("Materials/Default.xml");
+    
+    CreateGeometries();
+    CreateInstancingBuffer();
+    
+    viewports_.Resize(1);
+    ResetViews();
+    
+    LOGINFO("Initialized renderer");
+    initialized_ = true;
+}
+
+void Renderer::ResetViews()
+{
+    views_.Clear();
+    numViews_ = 0;
+}
+
 void Renderer::LoadShaders()
 void Renderer::LoadShaders()
 {
 {
     LOGINFO("Reloading shaders");
     LOGINFO("Reloading shaders");
@@ -1318,31 +1373,6 @@ void Renderer::CreateInstancingBuffer()
     }
     }
 }
 }
 
 
-bool Renderer::ResizeInstancingBuffer(unsigned numInstances)
-{
-    if (!instancingBuffer_ || !dynamicInstancing_)
-        return false;
-    
-    unsigned oldSize = instancingBuffer_->GetVertexCount();
-    if (numInstances <= oldSize)
-        return true;
-    
-    unsigned newSize = INSTANCING_BUFFER_DEFAULT_SIZE;
-    while (newSize < numInstances)
-        newSize <<= 1;
-    
-    if (!instancingBuffer_->SetSize(newSize, INSTANCING_BUFFER_MASK, true))
-    {
-        LOGERROR("Failed to resize instancing buffer to " + String(newSize));
-        // If failed, try to restore the old size
-        instancingBuffer_->SetSize(oldSize, INSTANCING_BUFFER_MASK, true);
-        return false;
-    }
-    
-    LOGDEBUG("Resized instancing buffer to " + String(newSize));
-    return true;
-}
-
 void Renderer::ResetShadowMaps()
 void Renderer::ResetShadowMaps()
 {
 {
     shadowMaps_.Clear();
     shadowMaps_.Clear();
@@ -1350,34 +1380,6 @@ void Renderer::ResetShadowMaps()
     shadowDepthStencil_.Reset();
     shadowDepthStencil_.Reset();
 }
 }
 
 
-void Renderer::ResetShadowMapAllocations()
-{
-    for (HashMap<int, PODVector<Light*> >::Iterator i = shadowMapAllocations_.Begin(); i != shadowMapAllocations_.End(); ++i)
-        i->second_.Clear();
-}
-
-Camera* Renderer::CreateShadowCamera()
-{
-    if (numShadowCameras_ >= shadowCameraStore_.Size())
-        shadowCameraStore_.Push(SharedPtr<Camera>(new Camera(context_)));
-    Camera* camera = shadowCameraStore_[numShadowCameras_];
-    camera->SetNode(CreateTempNode());
-    camera->SetOrthographic(false);
-    camera->SetZoom(1.0f);
-    ++numShadowCameras_;
-    return camera;
-}
-
-Node* Renderer::CreateTempNode()
-{
-    if (numTempNodes_ >= tempNodeStore_.Size())
-        tempNodeStore_.Push(SharedPtr<Node>(new Node(context_)));
-    Node* node = tempNodeStore_[numTempNodes_];
-    
-    ++numTempNodes_;
-    return node;
-}
-
 void Renderer::HandleScreenMode(StringHash eventType, VariantMap& eventData)
 void Renderer::HandleScreenMode(StringHash eventType, VariantMap& eventData)
 {
 {
     if (!initialized_)
     if (!initialized_)

+ 17 - 19
Engine/Graphics/Renderer.h

@@ -101,8 +101,6 @@ class Renderer : public Object
 {
 {
     OBJECT(Object);
     OBJECT(Object);
     
     
-    friend class View;
-    
 public:
 public:
     /// Construct.
     /// Construct.
     Renderer(Context* context);
     Renderer(Context* context);
@@ -225,6 +223,10 @@ public:
     ShaderVariation* GetVertexShader(const String& name, bool checkExists = false) const;
     ShaderVariation* GetVertexShader(const String& name, bool checkExists = false) const;
     /// Return a pixel shader by name.
     /// Return a pixel shader by name.
     ShaderVariation* GetPixelShader(const String& name, bool checkExists = false) const;
     ShaderVariation* GetPixelShader(const String& name, bool checkExists = false) const;
+    /// Return the stencil vertex shader.
+    ShaderVariation* GetStencilVS() const { return stencilVS_; }
+    /// Return the stencil pixel shader.
+    ShaderVariation* GetStencilPS() const { return stencilPS_; }
     /// Return the frame update parameters.
     /// Return the frame update parameters.
     const FrameInfo& GetFrameInfo() { return frame_; }
     const FrameInfo& GetFrameInfo() { return frame_; }
     
     
@@ -234,6 +236,10 @@ public:
     void Render();
     void Render();
     /// Add debug geometry to the debug graphics(s).
     /// Add debug geometry to the debug graphics(s).
     void DrawDebugGeometry(bool depthTest);
     void DrawDebugGeometry(bool depthTest);
+    /// Add a view. Return true if successful.
+    bool AddView(RenderSurface* renderTarget, const Viewport& viewport);
+    /// Return an occlusion buffer for use.
+    OcclusionBuffer* GetOrCreateOcclusionBuffer(Camera* camera, int maxOccluderTriangles, bool halfResolution = false);
     /// Return volume geometry for a light.
     /// Return volume geometry for a light.
     Geometry* GetLightGeometry(Light* light);
     Geometry* GetLightGeometry(Light* light);
     /// Return shadow map for a light. If shadow map reuse is disabled, a different map is returned each time.
     /// Return shadow map for a light. If shadow map reuse is disabled, a different map is returned each time.
@@ -242,16 +248,20 @@ public:
     ShaderVariation* GetShader(const String& name, const String& extension, bool checkExists) const;
     ShaderVariation* GetShader(const String& name, const String& extension, bool checkExists) const;
     /// Choose shaders for a batch.
     /// Choose shaders for a batch.
     void SetBatchShaders(Batch& batch, Technique* technique, Pass* pass, bool allowShadows = true);
     void SetBatchShaders(Batch& batch, Technique* technique, Pass* pass, bool allowShadows = true);
-
+    /// Allocate a shadow camera and a scene node for it.
+    Camera* CreateShadowCamera();
+    /// Allocate a temporary scene node for attaching a split light or a shadow camera.
+    Node* CreateTempNode();
+    /// Ensure sufficient size of the instancing vertex buffer. Return true if successful.
+    bool ResizeInstancingBuffer(unsigned numInstances);
+    /// Reset shadow map allocation counts.
+    void ResetShadowMapAllocations();
+    
 private:
 private:
     /// Initialize when screen mode initially set.
     /// Initialize when screen mode initially set.
     void Initialize();
     void Initialize();
     /// Clear views from previous frame.
     /// Clear views from previous frame.
     void ResetViews();
     void ResetViews();
-    /// Add a view. Return true if successful.
-    bool AddView(RenderSurface* renderTarget, const Viewport& viewport);
-    /// Return an occlusion buffer for use.
-    OcclusionBuffer* GetOrCreateOcclusionBuffer(Camera* camera, int maxOccluderTriangles, bool halfResolution = false);
     /// Reload shaders.
     /// Reload shaders.
     void LoadShaders();
     void LoadShaders();
     /// Reload shaders for a material technique.
     /// Reload shaders for a material technique.
@@ -266,20 +276,8 @@ private:
     void CreateGeometries();
     void CreateGeometries();
     /// Create instancing vertex buffer.
     /// Create instancing vertex buffer.
     void CreateInstancingBuffer();
     void CreateInstancingBuffer();
-    /// Ensure sufficient size of the instancing vertex buffer. Return true if successful.
-    bool ResizeInstancingBuffer(unsigned numInstances);
     /// Remove all shadow maps. Called when global shadow map resolution or format is changed.
     /// Remove all shadow maps. Called when global shadow map resolution or format is changed.
     void ResetShadowMaps();
     void ResetShadowMaps();
-    /// Reset shadow map allocation counts.
-    void ResetShadowMapAllocations();
-    /// Split a light into several for shadow mapping.
-    unsigned SplitLight(Light* light);
-    /// Allocate a shadow camera and a scene node for it.
-    Camera* CreateShadowCamera();
-    /// Allocate (if necessary) and clone a light. Attach it to a temporary scene node.
-    Light* CreateSplitLight(Light* original);
-    /// Allocate a temporary scene node for attaching a split light or a shadow camera.
-    Node* CreateTempNode();
     /// Handle screen mode event.
     /// Handle screen mode event.
     void HandleScreenMode(StringHash eventType, VariantMap& eventData);
     void HandleScreenMode(StringHash eventType, VariantMap& eventData);
     /// Handle render update event.
     /// Handle render update event.

+ 4 - 4
Engine/Graphics/View.cpp

@@ -542,7 +542,7 @@ void View::GetLitBatches(Drawable* drawable, LightBatchQueue& lightQueue)
     Light* firstLight = drawable->GetFirstLight();
     Light* firstLight = drawable->GetFirstLight();
     
     
     // Shadows on transparencies can only be rendered if shadow maps are not reused
     // Shadows on transparencies can only be rendered if shadow maps are not reused
-    bool allowTransparentShadows = !renderer_->reuseShadowMaps_;
+    bool allowTransparentShadows = !renderer_->GetReuseShadowMaps();
     unsigned numBatches = drawable->GetNumBatches();
     unsigned numBatches = drawable->GetNumBatches();
     
     
     for (unsigned i = 0; i < numBatches; ++i)
     for (unsigned i = 0; i < numBatches; ++i)
@@ -597,7 +597,7 @@ void View::GetLitBatches(Drawable* drawable, LightBatchQueue& lightQueue)
 void View::RenderBatches()
 void View::RenderBatches()
 {
 {
     // If not reusing shadowmaps, render all of them first
     // If not reusing shadowmaps, render all of them first
-    if (!renderer_->reuseShadowMaps_)
+    if (!renderer_->GetReuseShadowMaps())
     {
     {
         PROFILE(RenderShadowMaps);
         PROFILE(RenderShadowMaps);
         
         
@@ -630,7 +630,7 @@ void View::RenderBatches()
             LightBatchQueue& queue = lightQueues_[i];
             LightBatchQueue& queue = lightQueues_[i];
             
             
             // If reusing shadowmaps, render each of them before the lit batches
             // If reusing shadowmaps, render each of them before the lit batches
-            if (renderer_->reuseShadowMaps_ && queue.shadowMap_)
+            if (renderer_->GetReuseShadowMaps() && queue.shadowMap_)
                 RenderShadowMap(queue);
                 RenderShadowMap(queue);
             
             
             graphics_->SetRenderTarget(0, renderTarget_);
             graphics_->SetRenderTarget(0, renderTarget_);
@@ -1079,7 +1079,7 @@ void View::OptimizeLightByStencil(Light* light)
         graphics_->SetColorWrite(false);
         graphics_->SetColorWrite(false);
         graphics_->SetDepthWrite(false);
         graphics_->SetDepthWrite(false);
         graphics_->SetStencilTest(true, CMP_ALWAYS, OP_REF, OP_KEEP, OP_KEEP, lightStencilValue_);
         graphics_->SetStencilTest(true, CMP_ALWAYS, OP_REF, OP_KEEP, OP_KEEP, lightStencilValue_);
-        graphics_->SetShaders(renderer_->stencilVS_, renderer_->stencilPS_);
+        graphics_->SetShaders(renderer_->GetStencilVS(), renderer_->GetStencilPS());
         graphics_->SetShaderParameter(VSP_VIEWPROJ, projection * view);
         graphics_->SetShaderParameter(VSP_VIEWPROJ, projection * view);
         graphics_->SetShaderParameter(VSP_MODEL, light->GetVolumeTransform());
         graphics_->SetShaderParameter(VSP_MODEL, light->GetVolumeTransform());
         
         

+ 5 - 5
Engine/Script/Script.cpp

@@ -390,11 +390,6 @@ void Script::ExceptionCallback(asIScriptContext* context)
     MessageCallback(&msg);
     MessageCallback(&msg);
 }
 }
 
 
-asIScriptContext* Script::GetScriptFileContext() const
-{
-    return scriptNestingLevel_ < scriptFileContexts_.Size() ? scriptFileContexts_[scriptNestingLevel_] : 0;
-}
-
 ScriptFile* Script::GetDefaultScriptFile() const
 ScriptFile* Script::GetDefaultScriptFile() const
 {
 {
     return defaultScriptFile_;
     return defaultScriptFile_;
@@ -416,6 +411,11 @@ asIObjectType* Script::GetObjectType(const char* declaration)
     return type;
     return type;
 }
 }
 
 
+asIScriptContext* Script::GetScriptFileContext() const
+{
+    return scriptNestingLevel_ < scriptFileContexts_.Size() ? scriptFileContexts_[scriptNestingLevel_] : 0;
+}
+
 void Script::OutputAPIRow(const String& row, bool removeReference)
 void Script::OutputAPIRow(const String& row, bool removeReference)
 {
 {
     String out = row;
     String out = row;

+ 10 - 8
Engine/Script/Script.h

@@ -50,6 +50,8 @@ class Script : public Object
 {
 {
     OBJECT(Script);
     OBJECT(Script);
     
     
+    friend class ScriptFile;
+    
 public:
 public:
     /// Construct.
     /// Construct.
     Script(Context* context);
     Script(Context* context);
@@ -77,8 +79,6 @@ public:
     asIScriptEngine* GetScriptEngine() const { return scriptEngine_; }
     asIScriptEngine* GetScriptEngine() const { return scriptEngine_; }
     /// Return immediate execution script context.
     /// Return immediate execution script context.
     asIScriptContext* GetImmediateContext() const { return immediateContext_; }
     asIScriptContext* GetImmediateContext() const { return immediateContext_; }
-    /// Return a script function/method execution context for the current execution nesting level.
-    asIScriptContext* GetScriptFileContext() const;
     /// Return immediate mode script file.
     /// Return immediate mode script file.
     ScriptFile* GetDefaultScriptFile() const;
     ScriptFile* GetDefaultScriptFile() const;
     /// Return immediate mode scene.
     /// Return immediate mode scene.
@@ -90,18 +90,20 @@ public:
     /// Return retained mode log messages.
     /// Return retained mode log messages.
     const String& GetLogMessages() const { return logMessages_; }
     const String& GetLogMessages() const { return logMessages_; }
     
     
-    /// Increase script nesting level.
-    void IncScriptNestingLevel() { ++scriptNestingLevel_; }
-    /// Decrease script nesting level.
-    void DecScriptNestingLevel() { --scriptNestingLevel_; }
-    /// Return current script nesting level.
-    unsigned GetScriptNestingLevel() { return scriptNestingLevel_; }
     /// Return script module to script file map.
     /// Return script module to script file map.
     Map<asIScriptModule*, ScriptFile*>& GetModuleMap() { return moduleMap_; }
     Map<asIScriptModule*, ScriptFile*>& GetModuleMap() { return moduleMap_; }
     /// Return script object to script instance map.
     /// Return script object to script instance map.
     Map<void*, ScriptInstance*>& GetObjectMap() { return objectMap_; }
     Map<void*, ScriptInstance*>& GetObjectMap() { return objectMap_; }
     
     
 private:
 private:
+    /// Increase script nesting level.
+    void IncScriptNestingLevel() { ++scriptNestingLevel_; }
+    /// Decrease script nesting level.
+    void DecScriptNestingLevel() { --scriptNestingLevel_; }
+    /// Return current script nesting level.
+    unsigned GetScriptNestingLevel() { return scriptNestingLevel_; }
+    /// Return a script function/method execution context for the current execution nesting level.
+    asIScriptContext* GetScriptFileContext() const;
     /// Output a sanitated row of script API.
     /// Output a sanitated row of script API.
     void OutputAPIRow(const String& row, bool removeReference = false);
     void OutputAPIRow(const String& row, bool removeReference = false);