From baa62823509bfc1fa0daa85243e989150ab04414 Mon Sep 17 00:00:00 2001
From: jwendt <wendt@vr.rwth-aachen.de>
Date: Fri, 29 Jun 2018 13:53:43 +0200
Subject: [PATCH] upload textures within render_pass

#484
---
 .../rendering/render_passes/geometry_pass.cpp |  4 +++
 library/phx/resources/types/material.cpp      | 31 +++++++++++++++----
 library/phx/resources/types/material.hpp      |  4 +++
 3 files changed, 33 insertions(+), 6 deletions(-)

diff --git a/library/phx/rendering/render_passes/geometry_pass.cpp b/library/phx/rendering/render_passes/geometry_pass.cpp
index 434b0b0a..aeaf4a16 100644
--- a/library/phx/rendering/render_passes/geometry_pass.cpp
+++ b/library/phx/rendering/render_passes/geometry_pass.cpp
@@ -67,6 +67,10 @@ void GeometryPass::UploadMeshData(
   mesh_cache_.clear();
   RenderOffset offset{0, 0};
   for (const auto& instance : rendering_instances) {
+    if (instance.material != nullptr) {
+      instance.material->UploadTextures();
+    }
+
     Mesh* mesh = instance.mesh;
     if (mesh_cache_.find(mesh) != mesh_cache_.end()) {
       continue;
diff --git a/library/phx/resources/types/material.cpp b/library/phx/resources/types/material.cpp
index cf0b925a..49bd5519 100644
--- a/library/phx/resources/types/material.cpp
+++ b/library/phx/resources/types/material.cpp
@@ -42,7 +42,7 @@ ResourcePointer<Image> Material::GetDiffuseImage() const {
 }
 void Material::SetDiffuseImage(ResourcePointer<Image> image) {
   diffuse_image_ = image;
-  SetTexture(diffuse_image_, &diffuse_texture_);
+  ResetTexture(&diffuse_texture_);
 }
 
 gl::texture_2d* Material::GetDiffuseTexture() const {
@@ -57,7 +57,7 @@ ResourcePointer<Image> Material::GetAmbientImage() const {
 }
 void Material::SetAmbientImage(ResourcePointer<Image> image) {
   ambient_image_ = image;
-  SetTexture(ambient_image_, &ambient_texture_);
+  ResetTexture(&ambient_texture_);
 }
 
 gl::texture_2d* Material::GetAmbientTexture() const {
@@ -72,7 +72,7 @@ ResourcePointer<Image> Material::GetSpecularImage() const {
 }
 void Material::SetSpecularImage(ResourcePointer<Image> image) {
   specular_image_ = image;
-  SetTexture(specular_image_, &specular_texture_);
+  ResetTexture(&specular_texture_);
 }
 
 gl::texture_2d* Material::GetSpecularTexture() const {
@@ -93,12 +93,23 @@ void Material::SetShininess(float shininess) {
 
 const Material* Material::GetDefault() { return &default_material_; }
 
+void Material::UploadTextures() {
+  if (!diffuse_texture_) {
+    SetTexture(diffuse_image_, &diffuse_texture_);
+  }
+  if (!specular_texture_) {
+    SetTexture(specular_image_, &specular_texture_);
+  }
+  if (!ambient_texture_) {
+    SetTexture(ambient_image_, &ambient_texture_);
+  }
+}
+
 void Material::SetTexture(ResourcePointer<Image> image,
                           std::shared_ptr<gl::texture_2d>* texture) {
   if (image == nullptr) return;
-  if (*texture && (*texture)->is_valid()) {
-    gl::texture_handle handle(*texture->get());
-    handle.set_resident(false);
+  if (*texture) {
+    ResetTexture(texture);
   }
 
   auto dimensions = image->GetDimensions();
@@ -123,4 +134,12 @@ void Material::SetTexture(ResourcePointer<Image> image,
   gl::print_error("[Material::SetTexture] OpenGl Error code");
 }
 
+void Material::ResetTexture(std::shared_ptr<gl::texture_2d>* texture) {
+  if (*texture && (*texture)->is_valid()) {
+    gl::texture_handle handle(*texture->get());
+    handle.set_resident(false);
+  }
+  texture->reset();
+}
+
 }  // namespace phx
diff --git a/library/phx/resources/types/material.hpp b/library/phx/resources/types/material.hpp
index ae74ee21..fc839341 100644
--- a/library/phx/resources/types/material.hpp
+++ b/library/phx/resources/types/material.hpp
@@ -83,9 +83,13 @@ class PHOENIX_EXPORT Material : public Resource, public Nameable {
 
   static const Material* GetDefault();
 
+  // unless this was called Get..Texture return nullptr
+  void UploadTextures();
+
  private:
   void SetTexture(ResourcePointer<Image> image,
                   std::shared_ptr<gl::texture_2d>* texture);
+  void ResetTexture(std::shared_ptr<gl::texture_2d>* texture);
 
   ResourcePointer<Image> ambient_image_{nullptr};
   ResourcePointer<Image> diffuse_image_{nullptr};
-- 
GitLab