فهرست منبع

Write test(s) for uniform materials. Fix but where we were asking for the path while doing a uniform-lookup.

Sam Jaffe 6 سال پیش
والد
کامیت
8b33487632
3فایلهای تغییر یافته به همراه103 افزوده شده و 1 حذف شده
  1. 4 0
      graphics/graphics.xcodeproj/project.pbxproj
  2. 1 1
      graphics/src/manager.cxx
  3. 98 0
      graphics/test/manager_test.cxx

+ 4 - 0
graphics/graphics.xcodeproj/project.pbxproj

@@ -28,6 +28,7 @@
 		CDED9C4622A2FCA100AE5CE5 /* GoogleMock.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = CD62FCDF22904AD100376440 /* GoogleMock.framework */; };
 		CDED9C4722A308AE00AE5CE5 /* libmath.dylib in Frameworks */ = {isa = PBXBuildFile; fileRef = CDA34D9922517A3D008036A7 /* libmath.dylib */; };
 		CDED9C5422A465DB00AE5CE5 /* opengl_renderer.h in Headers */ = {isa = PBXBuildFile; fileRef = CDED9C5322A465DB00AE5CE5 /* opengl_renderer.h */; settings = {ATTRIBUTES = (Private, ); }; };
+		CDED9C6322A961CE00AE5CE5 /* manager_test.cxx in Sources */ = {isa = PBXBuildFile; fileRef = CDED9C5E22A961CA00AE5CE5 /* manager_test.cxx */; };
 /* End PBXBuildFile section */
 
 /* Begin PBXContainerItemProxy section */
@@ -100,6 +101,7 @@
 		CDED9C1822A2D6CE00AE5CE5 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
 		CDED9C4222A2FACB00AE5CE5 /* renderer_test.cxx */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = renderer_test.cxx; sourceTree = "<group>"; };
 		CDED9C5322A465DB00AE5CE5 /* opengl_renderer.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = opengl_renderer.h; sourceTree = "<group>"; };
+		CDED9C5E22A961CA00AE5CE5 /* manager_test.cxx */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = manager_test.cxx; sourceTree = "<group>"; };
 /* End PBXFileReference section */
 
 /* Begin PBXFrameworksBuildPhase section */
@@ -227,6 +229,7 @@
 			isa = PBXGroup;
 			children = (
 				CDED9C4222A2FACB00AE5CE5 /* renderer_test.cxx */,
+				CDED9C5E22A961CA00AE5CE5 /* manager_test.cxx */,
 			);
 			path = test;
 			sourceTree = "<group>";
@@ -411,6 +414,7 @@
 			buildActionMask = 2147483647;
 			files = (
 				CDED9C4322A2FACB00AE5CE5 /* renderer_test.cxx in Sources */,
+				CDED9C6322A961CE00AE5CE5 /* manager_test.cxx in Sources */,
 			);
 			runOnlyForDeploymentPostprocessing = 0;
 		};

+ 1 - 1
graphics/src/manager.cxx

@@ -154,7 +154,7 @@ manager::texture_or_uniform(std::string const & path,
   prepare_uniforms();
   // The uniform is primed into the cache already.
   auto & cache = pcache_->textures;
-  auto found = cache.values.find(path);
+  auto found = cache.values.find(uniform);
   if (found != cache.values.end()) { return found->second; }
   throw;
 }

+ 98 - 0
graphics/test/manager_test.cxx

@@ -0,0 +1,98 @@
+//
+//  manager_test.cxx
+//  graphics
+//
+//  Created by Sam Jaffe on 6/6/19.
+//  Copyright © 2019 Sam Jaffe. All rights reserved.
+//
+
+#include <gmock/gmock.h>
+
+#include "../src/helper.hpp"
+#include "game/graphics/manager.hpp"
+#include "game/graphics/material.hpp"
+#include "game/graphics/shader.hpp"
+#include "game/graphics/shader_program.hpp"
+#include "game/graphics/texture.hpp"
+
+using testing::AllOf;
+using testing::AnyNumber;
+using testing::Eq;
+using testing::Ge;
+using testing::Le;
+using testing::SizeIs;
+using testing::_;
+
+struct mock_manager_impl : graphics::manager {
+  using shader = graphics::shader;
+  using shader_program = graphics::shader_program;
+  using texture = graphics::texture;
+
+  MOCK_CONST_METHOD2(compile_shader,
+                     shader(graphics::shaders::type, std::string const &));
+  MOCK_CONST_METHOD2(compile_program,
+                     shader_program(identity<shader>, identity<shader>));
+  shader compile(graphics::shaders::type t, std::string const & s) const {
+    return compile_shader(t, s);
+  }
+  shader_program compile(identity<shader> f, identity<shader> v) const {
+    return compile_program(f, v);
+  }
+  MOCK_CONST_METHOD3(compile, texture(graphics::textures::format, math::vec2i,
+                                      void const *));
+};
+
+template <typename T> identity<T> cast(unsigned int id) {
+  return *reinterpret_cast<identity<T> *>(&id);
+}
+
+auto cast_p(unsigned int id) { return cast<graphics::shader_program>(id); }
+
+struct ManagerTest : testing::Test {
+  void SetUp() override;
+  mock_manager_impl mock;
+};
+
+void ManagerTest::SetUp() {
+  using testing::Invoke;
+  using testing::WithArg;
+  // Start with a new set of IDs
+  auto next_tex = [](math::vec2i const & sz) mutable {
+    static unsigned int i = 0;
+    return graphics::texture{++i, sz};
+  };
+  ON_CALL(mock, compile(_, _, _)).WillByDefault(WithArg<1>(Invoke(next_tex)));
+}
+
+TEST_F(ManagerTest, DoesNotGreedilyCompile) {
+  EXPECT_CALL(mock, compile(_, _, _)).Times(0);
+  EXPECT_CALL(mock, compile_shader(_, _)).Times(0);
+  EXPECT_CALL(mock, compile_program(_, _)).Times(0);
+}
+
+using MaterialTest = ManagerTest;
+
+// TEST_F(MaterialTest, ThrowsExceptionIfNoTexOrUniform) {
+//  EXPECT_CALL(mock, compile(_, _, _)).Times(AnyNumber());
+//  // TODO (sjaffe): Throw a specific exception here, since throw; kills us
+//  EXPECT_ANY_THROW(mock.get(cast_p(1), "", ""));
+//}
+
+TEST_F(MaterialTest, GeneratesUniformTexturesIfNoTexFile) {
+  using graphics::materials::uniform;
+  EXPECT_CALL(mock, compile(_, make_vector(1, 1), _)).Times(3);
+  auto material_id = mock.get(cast_p(1), "", "u_normalMap");
+  auto material = mock.get(material_id);
+  // Ensure that the material is the 'same one' i.e.
+  // mock.get(mock.get(id)) == mock.get(id)
+  EXPECT_THAT(material, Eq(material_id));
+  // Uniforms are always sized 1x1
+  EXPECT_THAT(material.size, Eq(make_vector(1, 1)));
+
+  EXPECT_THAT(material.uniforms, SizeIs(1));
+  // Because we never initialize any textures, we are within the first three
+  // texture ID units.
+  EXPECT_THAT(material.uniforms[0].texture.id, AllOf(Ge(1), Le(3)));
+  // Test the mapping from "u_normalMap" to uniform::NORMAL
+  EXPECT_THAT(material.uniforms[0].uniform_id, Eq(uniform::NORMAL));
+}