Repository: google/filament
File: libs/filamat/tests/test_filamat.cpp (C++)

Code before:
#ifdef __EXCEPTIONS
    EXPECT_THROW({
        filamat::MaterialBuilder builder;
        builder.constant("myFloatConstant", ConstantType::FLOAT, 10);
    }, utils::PostconditionPanic);
#endif
}

TEST_F(MaterialCompiler, FeatureLevel0Sampler2D) {
  std::string shaderCode(R"(
        void material(inout MaterialInputs material) {
            prepareMaterial(material);
            material.baseColor = texture2D(materialParams_sampler, vec2(0.0, 0.0));
        }
    )");
  filamat::MaterialBuilder builder;
  builder.parameter("sampler", SamplerType::SAMPLER_2D);

  builder.featureLevel(FeatureLevel::FEATURE_LEVEL_0);
  builder.shading(filament::Shading::UNLIT);
  builder.material(shaderCode.c_str());
  filamat::Package result = builder.build(*jobSystem);
  EXPECT_TRUE(result.isValid());
}

TEST_F(MaterialCompiler, SamplerTransformName) {
  std::string shaderCode(R"(
        void material(inout MaterialInputs material) {
            prepareMaterial(material);
            vec3 uvw = materialParams.sampler_transform * vec3(0.0, 0.0, 0.0);
            material.baseColor = texture2D(materialParams_sampler, uvw.xy);
        }
    )");
  filamat::MaterialBuilder builder;
  builder.parameter("sampler", SamplerType::SAMPLER_2D, SamplerFormat::FLOAT,
          ParameterPrecision::DEFAULT, true, false, "sampler_transform");

  builder.featureLevel(FeatureLevel::FEATURE_LEVEL_0);
  builder.shading(filament::Shading::UNLIT);
  builder.material(shaderCode.c_str());
  filamat::Package result = builder.build(*jobSystem);
  EXPECT_TRUE(result.isValid());
}

TEST_F(MaterialCompiler, FeatureLevel0Ess3CallFails) {
  std::string shaderCode(R"(
        void material(inout MaterialInputs material) {
            prepareMaterial(material);
            material.baseColor = texture(materialParams_sampler, vec3(0.0, 0.0));
        }
    )");

Review comment (suggestion):
This test doesn't exercise the newly introduced method `getParameterTransformName`. You may want to add one.

Code after:
#ifdef __EXCEPTIONS
    EXPECT_THROW({
        filamat::MaterialBuilder builder;
        builder.constant("myFloatConstant", ConstantType::FLOAT, 10);
    }, utils::PostconditionPanic);
#endif
}

TEST_F(MaterialCompiler, FeatureLevel0Sampler2D) {
  std::string shaderCode(R"(
        void material(inout MaterialInputs material) {
            prepareMaterial(material);
            material.baseColor = texture2D(materialParams_sampler, vec2(0.0, 0.0));
        }
    )");
  filamat::MaterialBuilder builder;
  builder.parameter("sampler", SamplerType::SAMPLER_2D);

  builder.featureLevel(FeatureLevel::FEATURE_LEVEL_0);
  builder.shading(filament::Shading::UNLIT);
  builder.material(shaderCode.c_str());
  filamat::Package result = builder.build(*jobSystem);
  EXPECT_TRUE(result.isValid());
}

TEST_F(MaterialCompiler, SamplerTransformName) {
  std::string shaderCode(R"(
        void material(inout MaterialInputs material) {
            prepareMaterial(material);
            vec3 uvw = materialParams.sampler_transform * vec3(0.0, 0.0, 0.0);
            material.baseColor = texture2D(materialParams_sampler, uvw.xy);
        }
    )");
  filamat::MaterialBuilder builder;
  builder.parameter("sampler", SamplerType::SAMPLER_2D, SamplerFormat::FLOAT,
          ParameterPrecision::DEFAULT, true, false, "sampler_transform");

  builder.featureLevel(FeatureLevel::FEATURE_LEVEL_0);
  builder.shading(filament::Shading::UNLIT);
  builder.material(shaderCode.c_str());
  filamat::Package result = builder.build(*jobSystem);
  EXPECT_TRUE(result.isValid());
}

TEST_F(MaterialCompiler, SamplerMissingTransformName) {
  std::string shaderCode(R"(
        void material(inout MaterialInputs material) {
            prepareMaterial(material);
            vec3 uvw = materialParams.sampler_transform * vec3(0.0, 0.0, 0.0);
            material.baseColor = texture2D(materialParams_sampler, uvw.xy);
        }