From d858d3452336a2fbba9612a6b8cd7cb594be0c3d Mon Sep 17 00:00:00 2001 From: Brad Davis Date: Wed, 7 Mar 2018 17:27:47 -0800 Subject: [PATCH] Update material binding --- libraries/graphics/src/graphics/Material.h | 11 +++ .../render-utils/src/MaterialTextures.slh | 25 +++-- .../render-utils/src/RenderPipelines.cpp | 96 +++++++++++++++++++ 3 files changed, 119 insertions(+), 13 deletions(-) diff --git a/libraries/graphics/src/graphics/Material.h b/libraries/graphics/src/graphics/Material.h index 632cf99391..d31a0cbd31 100755 --- a/libraries/graphics/src/graphics/Material.h +++ b/libraries/graphics/src/graphics/Material.h @@ -20,9 +20,12 @@ #include #include +#include class Transform; +#define USE_BINDLESS_TEXTURES 1 + namespace graphics { class TextureMap; @@ -360,6 +363,11 @@ public: void setModel(const std::string& model) { _model = model; } +#if USE_BINDLESS_TEXTURES + const gpu::TextureTablePointer& getTextureTable() const { return _textureTable; } + void setTextureTable(const gpu::TextureTablePointer& textureTable) { _textureTable = textureTable; } +#endif + protected: std::string _name { "" }; @@ -367,6 +375,9 @@ private: mutable MaterialKey _key; mutable UniformBufferView _schemaBuffer; mutable UniformBufferView _texMapArrayBuffer; +#if USE_BINDLESS_TEXTURES + mutable gpu::TextureTablePointer _textureTable; +#endif TextureMaps _textureMaps; diff --git a/libraries/render-utils/src/MaterialTextures.slh b/libraries/render-utils/src/MaterialTextures.slh index 594fb5c380..6c335353fd 100644 --- a/libraries/render-utils/src/MaterialTextures.slh +++ b/libraries/render-utils/src/MaterialTextures.slh @@ -54,32 +54,31 @@ TexMapArray getTexMapArray() { TextureTable(0, matTex); <@if withAlbedo@> -#define albedoMap 1 +#define albedoMap 0 vec4 fetchAlbedoMap(vec2 uv) { return tableTexValue(matTex, albedoMap, uv); } <@endif@> <@if withRoughness@> -#define roughnessMap 3 +#define roughnessMap 4 float fetchRoughnessMap(vec2 uv) { return tableTexValue(matTex, roughnessMap, uv).r; } <@endif@> <@if withNormal@> -#define normalMap 4 +#define normalMap 1 vec3 fetchNormalMap(vec2 uv) { return tableTexValue(matTex, normalMap, uv).xyz; } @@ -93,7 +92,7 @@ float fetchMetallicMap(vec2 uv) { <@endif@> <@if withEmissive@> -#define emissiveMap 0 +#define emissiveMap 3 vec3 fetchEmissiveMap(vec2 uv) { return tableTexValue(matTex, emissiveMap, uv).rgb; } @@ -107,7 +106,7 @@ float fetchOcclusionMap(vec2 uv) { <@endif@> <@if withScattering@> -#define scatteringMap 7 +#define scatteringMap 6 float fetchScatteringMap(vec2 uv) { float scattering = texture(tableTex(matTex, scatteringMap), uv).r; // boolean scattering for now return max(((scattering - 0.1) / 0.9), 0.0); diff --git a/libraries/render-utils/src/RenderPipelines.cpp b/libraries/render-utils/src/RenderPipelines.cpp index aca8439547..8165e09754 100644 --- a/libraries/render-utils/src/RenderPipelines.cpp +++ b/libraries/render-utils/src/RenderPipelines.cpp @@ -612,6 +612,21 @@ void initZPassPipelines(ShapePlumber& shapePlumber, gpu::StatePointer state) { #include "RenderPipelines.h" #include +#if USE_BINDLESS_TEXTURES +gpu::TextureTablePointer makeTextureTable() { + auto textureCache = DependencyManager::get(); + auto textureTable = std::make_shared(); + textureTable->setTexture(ShapePipeline::Slot::ALBEDO, textureCache->getWhiteTexture()); + textureTable->setTexture(ShapePipeline::Slot::ROUGHNESS, textureCache->getWhiteTexture()); + textureTable->setTexture(ShapePipeline::Slot::NORMAL, textureCache->getBlueTexture()); + textureTable->setTexture(ShapePipeline::Slot::METALLIC, textureCache->getBlackTexture()); + textureTable->setTexture(ShapePipeline::Slot::OCCLUSION, textureCache->getWhiteTexture()); + textureTable->setTexture(ShapePipeline::Slot::SCATTERING, textureCache->getWhiteTexture()); + textureTable->setTexture(ShapePipeline::Slot::EMISSIVE_LIGHTMAP, textureCache->getBlackTexture()); + return textureTable; +} +#endif + void RenderPipelines::bindMaterial(graphics::MaterialPointer material, gpu::Batch& batch, bool enableTextures) { if (!material) { return; @@ -630,6 +645,86 @@ void RenderPipelines::bindMaterial(graphics::MaterialPointer material, gpu::Batc numUnlit++; } +#if USE_BINDLESS_TEXTURES + if (!material->getTextureTable()) { + material->setTextureTable(makeTextureTable()); + } + + batch.setResourceTextureTable(material->getTextureTable()); + if (!enableTextures) { + return; + } + + const auto& drawMaterialTextures = material->getTextureTable(); + + // Albedo + if (materialKey.isAlbedoMap()) { + auto itr = textureMaps.find(graphics::MaterialKey::ALBEDO_MAP); + if (itr != textureMaps.end() && itr->second->isDefined()) { + drawMaterialTextures->setTexture(ShapePipeline::Slot::ALBEDO, itr->second->getTextureView()); + } + } + + // Roughness map + if (materialKey.isRoughnessMap()) { + auto itr = textureMaps.find(graphics::MaterialKey::ROUGHNESS_MAP); + if (itr != textureMaps.end() && itr->second->isDefined()) { + drawMaterialTextures->setTexture(ShapePipeline::Slot::ROUGHNESS, itr->second->getTextureView()); + } + } + + // Normal map + if (materialKey.isNormalMap()) { + auto itr = textureMaps.find(graphics::MaterialKey::NORMAL_MAP); + if (itr != textureMaps.end() && itr->second->isDefined()) { + drawMaterialTextures->setTexture(ShapePipeline::Slot::NORMAL, itr->second->getTextureView()); + } + } + + // Metallic map + if (materialKey.isMetallicMap()) { + auto itr = textureMaps.find(graphics::MaterialKey::METALLIC_MAP); + if (itr != textureMaps.end() && itr->second->isDefined()) { + drawMaterialTextures->setTexture(ShapePipeline::Slot::METALLIC, itr->second->getTextureView()); + } + } + + // Occlusion map + if (materialKey.isOcclusionMap()) { + auto itr = textureMaps.find(graphics::MaterialKey::OCCLUSION_MAP); + if (itr != textureMaps.end() && itr->second->isDefined()) { + drawMaterialTextures->setTexture(ShapePipeline::Slot::OCCLUSION, itr->second->getTextureView()); + } + } + + // Scattering map + if (materialKey.isScatteringMap()) { + auto itr = textureMaps.find(graphics::MaterialKey::SCATTERING_MAP); + if (itr != textureMaps.end() && itr->second->isDefined()) { + drawMaterialTextures->setTexture(ShapePipeline::Slot::SCATTERING, itr->second->getTextureView()); + } + } + + // Emissive / Lightmap + if (materialKey.isLightmapMap()) { + auto itr = textureMaps.find(graphics::MaterialKey::LIGHTMAP_MAP); + + if (itr != textureMaps.end() && itr->second->isDefined()) { + drawMaterialTextures->setTexture(ShapePipeline::Slot::EMISSIVE_LIGHTMAP, itr->second->getTextureView()); + } else { + drawMaterialTextures->setTexture(ShapePipeline::Slot::EMISSIVE_LIGHTMAP, textureCache->getGrayTexture()); + } + } else if (materialKey.isEmissiveMap()) { + auto itr = textureMaps.find(graphics::MaterialKey::EMISSIVE_MAP); + if (itr != textureMaps.end() && itr->second->isDefined()) { + drawMaterialTextures->setTexture(ShapePipeline::Slot::EMISSIVE_LIGHTMAP, itr->second->getTextureView()); + } else { + drawMaterialTextures->setTexture(ShapePipeline::Slot::EMISSIVE_LIGHTMAP, textureCache->getBlackTexture()); + } + } + + +#else if (!enableTextures) { batch.setResourceTexture(ShapePipeline::Slot::ALBEDO, textureCache->getWhiteTexture()); batch.setResourceTexture(ShapePipeline::Slot::MAP::ROUGHNESS, textureCache->getWhiteTexture()); @@ -729,4 +824,5 @@ void RenderPipelines::bindMaterial(graphics::MaterialPointer material, gpu::Batc batch.setResourceTexture(ShapePipeline::Slot::MAP::EMISSIVE_LIGHTMAP, textureCache->getBlackTexture()); } } +#endif }