Update material binding

This commit is contained in:
Brad Davis 2018-03-07 17:27:47 -08:00
parent ddd60ded03
commit d858d34523
3 changed files with 119 additions and 13 deletions

View file

@ -20,9 +20,12 @@
#include <ColorUtils.h>
#include <gpu/Resource.h>
#include <gpu/TextureTable.h>
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;

View file

@ -54,32 +54,31 @@ TexMapArray getTexMapArray() {
TextureTable(0, matTex);
<!
EMISSIVE_MAP = 0,
ALBEDO_MAP,
METALLIC_MAP,
ROUGHNESS_MAP,
NORMAL_MAP,
OCCLUSION_MAP,
LIGHTMAP_MAP,
SCATTERING_MAP,
ALBEDO = 0,
NORMAL, 1
METALLIC, 2
EMISSIVE_LIGHTMAP, 3
ROUGHNESS, 4
OCCLUSION, 5
SCATTERING, 6
!>
<@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);

View file

@ -612,6 +612,21 @@ void initZPassPipelines(ShapePlumber& shapePlumber, gpu::StatePointer state) {
#include "RenderPipelines.h"
#include <model-networking/TextureCache.h>
#if USE_BINDLESS_TEXTURES
gpu::TextureTablePointer makeTextureTable() {
auto textureCache = DependencyManager::get<TextureCache>();
auto textureTable = std::make_shared<gpu::TextureTable>();
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
}