Simplified material binding in shadow mode. Added opacity mask to shadow fade. Adjusted default bias.

This commit is contained in:
Olivier Prat 2019-04-19 11:03:43 +02:00
parent 39d03ce87a
commit 7d412f9109
9 changed files with 50 additions and 75 deletions

View file

@ -313,11 +313,9 @@ void MaterialEntityRenderer::doRender(RenderArgs* args) {
batch.setModelTransform(renderTransform);
if (args->_renderMode != render::Args::RenderMode::SHADOW_RENDER_MODE) {
drawMaterial->setTextureTransforms(textureTransform, MaterialMappingMode::UV, true);
// bind the material
RenderPipelines::bindMaterial(drawMaterial, batch, args->_enableTexturing);
drawMaterial->setTextureTransforms(textureTransform, MaterialMappingMode::UV, true);
// bind the material
if (RenderPipelines::bindMaterial(drawMaterial, batch, args->_renderMode, args->_enableTexturing)) {
args->_details._materialSwitches++;
}

View file

@ -291,8 +291,7 @@ void ShapeEntityRenderer::doRender(RenderArgs* args) {
geometryCache->renderSolidShapeInstance(args, batch, geometryShape, outColor, pipeline);
}
} else {
if (args->_renderMode != render::Args::RenderMode::SHADOW_RENDER_MODE) {
RenderPipelines::bindMaterials(materials, batch, args->_enableTexturing);
if (RenderPipelines::bindMaterials(materials, batch, args->_renderMode, args->_enableTexturing)) {
args->_details._materialSwitches++;
}

View file

@ -149,13 +149,6 @@ float fetchScatteringMap(vec2 uv) {
<@endfunc@>
<@func fetchMaterialAlbedoTextureCoord0(matKey, texcoord0, albedo)@>
if (getTexMapArray()._materialParams.y != 1.0 && clamp(<$texcoord0$>, vec2(0.0), vec2(1.0)) != <$texcoord0$>) {
discard;
}
vec4 <$albedo$> = fetchAlbedoMap(<$texcoord0$>);
<@endfunc@>
<@func fetchMaterialTexturesCoord0(matKey, texcoord0, albedo, roughness, normal, metallic, emissive, scattering)@>
if (getTexMapArray()._materialParams.y != 1.0 && clamp(<$texcoord0$>, vec2(0.0), vec2(1.0)) != <$texcoord0$>) {
discard;

View file

@ -154,8 +154,7 @@ void MeshPartPayload::render(RenderArgs* args) {
bindMesh(batch);
// apply material properties
if (args->_renderMode != render::Args::RenderMode::SHADOW_RENDER_MODE) {
RenderPipelines::bindMaterials(_drawMaterials, batch, args->_enableTexturing);
if (RenderPipelines::bindMaterials(_drawMaterials, batch, args->_renderMode, args->_enableTexturing)) {
args->_details._materialSwitches++;
}
@ -434,13 +433,7 @@ void ModelMeshPartPayload::render(RenderArgs* args) {
}
// apply material properties
if (args->_renderMode != render::Args::RenderMode::SHADOW_RENDER_MODE) {
RenderPipelines::bindMaterials(_drawMaterials, batch, args->_enableTexturing);
args->_details._materialSwitches++;
} else {
// We might have an opacity mask so we need to bind the albedo texture and material which might hold
// the alpha mask channel
RenderPipelines::bindMaterialsOpacityMask(_drawMaterials, batch, args->_enableTexturing);
if (RenderPipelines::bindMaterials(_drawMaterials, batch, args->_renderMode, args->_enableTexturing)) {
args->_details._materialSwitches++;
}

View file

@ -373,10 +373,10 @@ void initZPassPipelines(ShapePlumber& shapePlumber, gpu::StatePointer state, con
gpu::Shader::createProgram(deformed_model_shadow_fade_dq), state, extraBatchSetter, itemSetter);
}
void RenderPipelines::bindMaterial(graphics::MaterialPointer& material, gpu::Batch& batch, bool enableTextures) {
bool RenderPipelines::bindMaterial(graphics::MaterialPointer& material, gpu::Batch& batch, render::Args::RenderMode renderMode, bool enableTextures) {
graphics::MultiMaterial multiMaterial;
multiMaterial.push(graphics::MaterialLayer(material, 0));
bindMaterials(multiMaterial, batch, enableTextures);
return bindMaterials(multiMaterial, batch, renderMode, enableTextures);
}
void RenderPipelines::updateMultiMaterial(graphics::MultiMaterial& multiMaterial) {
@ -730,17 +730,21 @@ void RenderPipelines::updateMultiMaterial(graphics::MultiMaterial& multiMaterial
multiMaterial.setInitialized();
}
static gpu::TextureTablePointer defaultMaterialTextures = std::make_shared<gpu::TextureTable>();
void RenderPipelines::bindMaterials(graphics::MultiMaterial& multiMaterial, gpu::Batch& batch, bool enableTextures) {
bool RenderPipelines::bindMaterials(graphics::MultiMaterial& multiMaterial, gpu::Batch& batch, render::Args::RenderMode renderMode, bool enableTextures) {
if (multiMaterial.shouldUpdate()) {
updateMultiMaterial(multiMaterial);
}
auto textureCache = DependencyManager::get<TextureCache>();
static gpu::TextureTablePointer defaultMaterialTextures = std::make_shared<gpu::TextureTable>();
static gpu::BufferView defaultMaterialSchema;
static std::once_flag once;
std::call_once(once, [textureCache] {
graphics::MultiMaterial::Schema schema;
defaultMaterialSchema = gpu::BufferView(std::make_shared<gpu::Buffer>(sizeof(schema), (const gpu::Byte*) &schema, sizeof(schema)));
defaultMaterialTextures->setTexture(gr::Texture::MaterialAlbedo, textureCache->getWhiteTexture());
defaultMaterialTextures->setTexture(gr::Texture::MaterialMetallic, textureCache->getBlackTexture());
defaultMaterialTextures->setTexture(gr::Texture::MaterialRoughness, textureCache->getWhiteTexture());
@ -750,49 +754,26 @@ void RenderPipelines::bindMaterials(graphics::MultiMaterial& multiMaterial, gpu:
// MaterialEmissiveLightmap has to be set later
});
auto& schemaBuffer = multiMaterial.getSchemaBuffer();
batch.setUniformBuffer(gr::Buffer::Material, schemaBuffer);
if (enableTextures) {
batch.setResourceTextureTable(multiMaterial.getTextureTable());
} else {
auto key = multiMaterial.getMaterialKey();
if (key.isLightmapMap()) {
defaultMaterialTextures->setTexture(gr::Texture::MaterialEmissiveLightmap, textureCache->getBlackTexture());
} else if (key.isEmissiveMap()) {
defaultMaterialTextures->setTexture(gr::Texture::MaterialEmissiveLightmap, textureCache->getGrayTexture());
}
batch.setResourceTextureTable(defaultMaterialTextures);
}
}
static gpu::BufferView defaultMaterialSchema;
void RenderPipelines::bindMaterialsOpacityMask(graphics::MultiMaterial& multiMaterial, gpu::Batch& batch, bool enableTextures) {
if (multiMaterial.shouldUpdate()) {
updateMultiMaterial(multiMaterial);
}
if (multiMaterial.getMaterialKey().isOpacityMaskMap()) {
auto textureCache = DependencyManager::get<TextureCache>();
static std::once_flag once;
std::call_once(once, [textureCache] {
defaultMaterialTextures->setTexture(gr::Texture::MaterialAlbedo, textureCache->getWhiteTexture());
});
// For shadows, we only need opacity mask information
if (renderMode != render::Args::RenderMode::SHADOW_RENDER_MODE || multiMaterial.getMaterialKey().isOpacityMaskMap()) {
auto& schemaBuffer = multiMaterial.getSchemaBuffer();
batch.setUniformBuffer(gr::Buffer::Material, schemaBuffer);
if (enableTextures) {
batch.setResourceTextureTable(multiMaterial.getTextureTable());
} else {
auto key = multiMaterial.getMaterialKey();
if (key.isLightmapMap()) {
defaultMaterialTextures->setTexture(gr::Texture::MaterialEmissiveLightmap, textureCache->getBlackTexture());
} else if (key.isEmissiveMap()) {
defaultMaterialTextures->setTexture(gr::Texture::MaterialEmissiveLightmap, textureCache->getGrayTexture());
}
batch.setResourceTextureTable(defaultMaterialTextures);
}
return true;
} else {
if (defaultMaterialSchema._buffer == nullptr) {
graphics::MultiMaterial::Schema schema;
defaultMaterialSchema = gpu::BufferView(std::make_shared<gpu::Buffer>(sizeof(schema), (const gpu::Byte*) &schema, sizeof(schema)));
}
batch.setResourceTextureTable(defaultMaterialTextures);
batch.setUniformBuffer(gr::Buffer::Material, defaultMaterialSchema);
return false;
}
}

View file

@ -12,13 +12,13 @@
#define hifi_RenderPipelines_h
#include <graphics/Material.h>
#include <render/Args.h>
class RenderPipelines {
public:
static void bindMaterial(graphics::MaterialPointer& material, gpu::Batch& batch, bool enableTextures);
static void updateMultiMaterial(graphics::MultiMaterial& multiMaterial);
static void bindMaterials(graphics::MultiMaterial& multiMaterial, gpu::Batch& batch, bool enableTextures);
static void bindMaterialsOpacityMask(graphics::MultiMaterial& multiMaterial, gpu::Batch& batch, bool enableTextures);
static bool bindMaterial(graphics::MaterialPointer& material, gpu::Batch& batch, render::Args::RenderMode renderMode, bool enableTextures);
static bool bindMaterials(graphics::MultiMaterial& multiMaterial, gpu::Batch& batch, render::Args::RenderMode renderMode, bool enableTextures);
};

View file

@ -93,8 +93,8 @@ public:
float constantBias1{ 0.15f };
float constantBias2{ 0.175f };
float constantBias3{ 0.2f };
float slopeBias0{ 0.6f };
float slopeBias1{ 0.6f };
float slopeBias0{ 0.4f };
float slopeBias1{ 0.45f };
float slopeBias2{ 0.65f };
float slopeBias3{ 0.7f };

View file

@ -23,13 +23,11 @@ layout(location=0) out vec4 _fragColor;
void main(void) {
Material mat = getMaterial();
BITFIELD matKey = getMaterialKey(mat);
<$fetchMaterialTexturesCoord0(matKey, _texCoord0, albedoTex, _SCRIBE_NULL, _SCRIBE_NULL, _SCRIBE_NULL, _SCRIBE_NULL, _SCRIBE_NULL)$>
if ((matKey & OPACITY_MASK_MAP_BIT) != 0) {
float opacity = 1.0;
<$fetchMaterialAlbedoTextureCoord0(matKey, _texCoord0, albedoTex)$>
<$evalMaterialOpacity(albedoTex.a, opacity, matKey, opacity)$>;
<$discardTransparent(opacity)$>;
}
float opacity = 1.0;
<$evalMaterialOpacity(albedoTex.a, opacity, matKey, opacity)$>;
<$discardTransparent(opacity)$>;
// pass-through to set z-buffer
_fragColor = vec4(1.0, 1.0, 1.0, 0.0);

View file

@ -9,13 +9,18 @@
// Distributed under the Apache License, Version 2.0.
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
//
<@include graphics/Material.slh@>
<@include graphics/MaterialTextures.slh@>
<@include render-utils/ShaderConstants.h@>
<$declareMaterialTextures(ALBEDO, _SCRIBE_NULL, _SCRIBE_NULL, _SCRIBE_NULL, _SCRIBE_NULL, _SCRIBE_NULL, _SCRIBE_NULL)$>
<@include Fade.slh@>
<$declareFadeFragment()$>
layout(location=RENDER_UTILS_ATTR_POSITION_WS) in vec4 _positionWS;
layout(location=RENDER_UTILS_ATTR_TEXCOORD01) in vec4 _texCoord01;
#define _texCoord0 _texCoord01.xy
layout(location=0) out vec4 _fragColor;
@ -24,6 +29,14 @@ void main(void) {
<$fetchFadeObjectParams(fadeParams)$>
applyFadeClip(fadeParams, _positionWS.xyz);
Material mat = getMaterial();
BITFIELD matKey = getMaterialKey(mat);
<$fetchMaterialTexturesCoord0(matKey, _texCoord0, albedoTex, _SCRIBE_NULL, _SCRIBE_NULL, _SCRIBE_NULL, _SCRIBE_NULL, _SCRIBE_NULL)$>
float opacity = 1.0;
<$evalMaterialOpacity(albedoTex.a, opacity, matKey, opacity)$>;
<$discardTransparent(opacity)$>;
// pass-through to set z-buffer
_fragColor = vec4(1.0, 1.0, 1.0, 0.0);
}