Skip to content

Commit

Permalink
Merge branch 'markh/aliasConstants' into 'main'
Browse files Browse the repository at this point in the history
Alias constants in Opaque material to save space

See merge request lightspeedrtx/dxvk-remix-nv!459
  • Loading branch information
MarkEHenderson committed Sep 8, 2023
2 parents 41235fe + 9129baa commit 03d2e19
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 43 deletions.
81 changes: 54 additions & 27 deletions src/dxvk/rtx_render/rtx_materials.h
Original file line number Diff line number Diff line change
Expand Up @@ -495,45 +495,72 @@ struct RtOpaqueSurfaceMaterial {

void writeGPUData(unsigned char* data, std::size_t& offset) const {
[[maybe_unused]] const std::size_t oldOffset = offset;
uint32_t flags = (0 << 30); // Note: Bit 30 and 31 of last word set to 0 for opaque material type

// 12 Bytes
writeGPUHelperExplicit<2>(data, offset, m_albedoOpacityTextureIndex);
writeGPUHelperExplicit<2>(data, offset, m_normalTextureIndex);
// Bytes 0-3
if (m_albedoOpacityTextureIndex != kSurfaceMaterialInvalidTextureIndex) {
writeGPUHelperExplicit<2>(data, offset, m_albedoOpacityTextureIndex);
writeGPUPadding<2>(data, offset); // Note: Padding for unused space
flags |= OPAQUE_SURFACE_MATERIAL_FLAG_HAS_ALBEDO_TEXTURE;
} else {
writeGPUHelper(data, offset, packUnorm<8, uint8_t>(m_albedoOpacityConstant.x));
writeGPUHelper(data, offset, packUnorm<8, uint8_t>(m_albedoOpacityConstant.y));
writeGPUHelper(data, offset, packUnorm<8, uint8_t>(m_albedoOpacityConstant.z));
writeGPUHelper(data, offset, packUnorm<8, uint8_t>(m_albedoOpacityConstant.w));
}

// Bytes 4-5
writeGPUHelperExplicit<2>(data, offset, m_tangentTextureIndex);
writeGPUHelperExplicit<2>(data, offset, m_roughnessTextureIndex);
writeGPUHelperExplicit<2>(data, offset, m_metallicTextureIndex);
writeGPUHelperExplicit<2>(data, offset, m_emissiveColorTextureIndex);

// 1 Byte
writeGPUHelper(data, offset, packSnorm<8, uint8_t>(m_anisotropy));

// 3 Bytes
// Note: Re-ordered to here to re-align singular anisotropy value shifting everything by 8 bits
writeGPUHelper(data, offset, packUnorm<8, uint8_t>(m_emissiveColorConstant.x));
writeGPUHelper(data, offset, packUnorm<8, uint8_t>(m_emissiveColorConstant.y));
writeGPUHelper(data, offset, packUnorm<8, uint8_t>(m_emissiveColorConstant.z));
// Bytes 6-7
if (m_roughnessTextureIndex != kSurfaceMaterialInvalidTextureIndex) {
writeGPUHelperExplicit<2>(data, offset, m_roughnessTextureIndex);
flags |= OPAQUE_SURFACE_MATERIAL_FLAG_HAS_ROUGHNESS_TEXTURE;
} else {
writeGPUHelper(data, offset, packUnorm<8, uint8_t>(m_roughnessConstant));
writeGPUPadding<1>(data, offset); // Note: Padding for unused space
}

assert(m_cachedEmissiveIntensity <= FLOAT16_MAX);
writeGPUHelper(data, offset, glm::packHalf1x16(m_cachedEmissiveIntensity));
// Bytes 8-9
if (m_metallicTextureIndex != kSurfaceMaterialInvalidTextureIndex) {
writeGPUHelperExplicit<2>(data, offset, m_metallicTextureIndex);
flags |= OPAQUE_SURFACE_MATERIAL_FLAG_HAS_METALLIC_TEXTURE;
} else {
writeGPUHelper(data, offset, packUnorm<8, uint8_t>(m_metallicConstant));
writeGPUPadding<1>(data, offset); // Note: Padding for unused space
}

writeGPUHelper(data, offset, packUnorm<8, uint8_t>(m_albedoOpacityConstant.x));
writeGPUHelper(data, offset, packUnorm<8, uint8_t>(m_albedoOpacityConstant.y));
// Bytes 10-12
if (m_emissiveColorTextureIndex != kSurfaceMaterialInvalidTextureIndex) {
writeGPUHelperExplicit<2>(data, offset, m_emissiveColorTextureIndex);
writeGPUPadding<1>(data, offset); // Note: Padding for unused space
flags |= OPAQUE_SURFACE_MATERIAL_FLAG_HAS_EMISSIVE_TEXTURE;
} else {
writeGPUHelper(data, offset, packUnorm<8, uint8_t>(m_emissiveColorConstant.x));
writeGPUHelper(data, offset, packUnorm<8, uint8_t>(m_emissiveColorConstant.y));
writeGPUHelper(data, offset, packUnorm<8, uint8_t>(m_emissiveColorConstant.z));
}

writeGPUHelper(data, offset, packUnorm<8, uint8_t>(m_albedoOpacityConstant.z));
writeGPUHelper(data, offset, packUnorm<8, uint8_t>(m_albedoOpacityConstant.w));
// Byte 13
writeGPUHelper(data, offset, packSnorm<8, uint8_t>(m_anisotropy));

writeGPUHelper(data, offset, packUnorm<8, uint8_t>(m_roughnessConstant));
writeGPUHelper(data, offset, packUnorm<8, uint8_t>(m_metallicConstant));

// 1 byte
writeGPUHelper(data, offset, packUnorm<8, uint8_t>(m_cachedThinFilmNormalizedThicknessConstant));
// Bytes 14-15
writeGPUHelperExplicit<2>(data, offset, m_normalTextureIndex);

writeGPUPadding<1>(data, offset); // Note: Padding for unused space
// Bytes 16-17
assert(m_cachedEmissiveIntensity <= FLOAT16_MAX);
writeGPUHelper(data, offset, glm::packHalf1x16(m_cachedEmissiveIntensity));

// Bytes 18-19
writeGPUHelperExplicit<2>(data, offset, m_samplerIndex);

uint32_t flags = (0 << 30); // Note: Bit 30 and 31 of last word set to 0 for opaque material type
// Byte 20
writeGPUHelper(data, offset, packUnorm<8, uint8_t>(m_cachedThinFilmNormalizedThicknessConstant));

// Bytes 21-27
writeGPUPadding<7>(data, offset); // Note: Padding for unused space

// Bytes 28-31
if (m_enableThinFilm) {
flags |= OPAQUE_SURFACE_MATERIAL_FLAG_USE_THIN_FILM_LAYER;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,28 +39,46 @@ OpaqueSurfaceMaterial opaqueSurfaceMaterialCreate(uint16_t surfaceMaterialIndex,
const u16vec2 data12 = unpack16(memoryPolymorphicSurfaceMaterial.data1.z);
// Note: Last two bits of data1.w (or data13 in this case) are reserved for common polymorphic type
const u16vec2 data13 = unpack16(memoryPolymorphicSurfaceMaterial.data1.w);
opaqueSurfaceMaterial.flags = (data13.x & 0xFF);
if (opaqueSurfaceMaterial.flags & OPAQUE_SURFACE_MATERIAL_FLAG_HAS_ALBEDO_TEXTURE) {
opaqueSurfaceMaterial.albedoOpacityTextureIndex = data00.x;
opaqueSurfaceMaterial.albedoOpacityConstant = f16vec4(0.0);
} else {
opaqueSurfaceMaterial.albedoOpacityTextureIndex = BINDING_INDEX_INVALID;
opaqueSurfaceMaterial.albedoOpacityConstant = f16vec4(unorm8ToF16(uint8_t(data00.x)), unorm8ToF16(uint8_t(data00.x >> 8u)), unorm8ToF16(uint8_t(data00.y)), unorm8ToF16(uint8_t(data00.y >> 8u)));
}

opaqueSurfaceMaterial.albedoOpacityTextureIndex = data00.x;
opaqueSurfaceMaterial.normalTextureIndex = data00.y;
opaqueSurfaceMaterial.tangentTextureIndex = data01.x;
opaqueSurfaceMaterial.roughnessTextureIndex = data01.y;
opaqueSurfaceMaterial.metallicTextureIndex = data02.x;
opaqueSurfaceMaterial.emissiveColorTextureIndex = data02.y;

opaqueSurfaceMaterial.anisotropy = snorm8ToF16(uint8_t(data03.x));
opaqueSurfaceMaterial.emissiveColorConstant = f16vec3(unorm8ToF16(uint8_t(data03.x >> 8u)), unorm8ToF16(uint8_t(data03.y)), unorm8ToF16(uint8_t(data03.y >> 8u)));

opaqueSurfaceMaterial.emissiveIntensity = uint16BitsToHalf(data10.x);

opaqueSurfaceMaterial.albedoOpacityConstant = f16vec4(unorm8ToF16(uint8_t(data10.y)), unorm8ToF16(uint8_t(data10.y >> 8u)), unorm8ToF16(uint8_t(data11.x)), unorm8ToF16(uint8_t(data11.x >> 8u)));
opaqueSurfaceMaterial.roughnessConstant = unorm8ToF16(uint8_t(data11.y));
opaqueSurfaceMaterial.metallicConstant = unorm8ToF16(uint8_t(data11.y >> 8u));
if (opaqueSurfaceMaterial.flags & OPAQUE_SURFACE_MATERIAL_FLAG_HAS_ROUGHNESS_TEXTURE) {
opaqueSurfaceMaterial.roughnessTextureIndex = data01.y;
opaqueSurfaceMaterial.roughnessConstant = 0.0;
} else {
opaqueSurfaceMaterial.roughnessTextureIndex = BINDING_INDEX_INVALID;
opaqueSurfaceMaterial.roughnessConstant = unorm8ToF16(uint8_t(data01.y));
}

opaqueSurfaceMaterial.thinFilmThicknessConstant = unorm8ToF16(uint8_t(data12.x));
if (opaqueSurfaceMaterial.flags & OPAQUE_SURFACE_MATERIAL_FLAG_HAS_METALLIC_TEXTURE) {
opaqueSurfaceMaterial.metallicTextureIndex = data02.x;
opaqueSurfaceMaterial.metallicConstant = 0.0;
} else {
opaqueSurfaceMaterial.metallicTextureIndex = BINDING_INDEX_INVALID;
opaqueSurfaceMaterial.metallicConstant = unorm8ToF16(uint8_t(data02.x));
}

opaqueSurfaceMaterial.samplerIndex = data12.y;
if (opaqueSurfaceMaterial.flags & OPAQUE_SURFACE_MATERIAL_FLAG_HAS_EMISSIVE_TEXTURE) {
opaqueSurfaceMaterial.emissiveColorTextureIndex = data02.y;
opaqueSurfaceMaterial.emissiveColorConstant = f16vec3(0.0);
} else {
opaqueSurfaceMaterial.emissiveColorTextureIndex = BINDING_INDEX_INVALID;
opaqueSurfaceMaterial.emissiveColorConstant = f16vec3(unorm8ToF16(uint8_t(data02.y)), unorm8ToF16(uint8_t(data02.y >> 8u)), unorm8ToF16(uint8_t(data03.x)));
}

opaqueSurfaceMaterial.flags = (data13.x & 0xFF);
opaqueSurfaceMaterial.anisotropy = snorm8ToF16(uint8_t(data03.x >> 8u));
opaqueSurfaceMaterial.normalTextureIndex = data03.y;
opaqueSurfaceMaterial.emissiveIntensity = uint16BitsToHalf(data10.x);
opaqueSurfaceMaterial.samplerIndex = data10.y;
opaqueSurfaceMaterial.thinFilmThicknessConstant = unorm8ToF16(uint8_t(data11.x));

return opaqueSurfaceMaterial;
}
4 changes: 4 additions & 0 deletions src/dxvk/shaders/rtx/utility/shared_constants.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@
// bits for flags field in OpaqueSurfaceMaterial
#define OPAQUE_SURFACE_MATERIAL_FLAG_USE_THIN_FILM_LAYER (1 << 0)
#define OPAQUE_SURFACE_MATERIAL_FLAG_ALPHA_IS_THIN_FILM_THICKNESS (1 << 1)
#define OPAQUE_SURFACE_MATERIAL_FLAG_HAS_ALBEDO_TEXTURE (1 << 2)
#define OPAQUE_SURFACE_MATERIAL_FLAG_HAS_ROUGHNESS_TEXTURE (1 << 3)
#define OPAQUE_SURFACE_MATERIAL_FLAG_HAS_METALLIC_TEXTURE (1 << 4)
#define OPAQUE_SURFACE_MATERIAL_FLAG_HAS_EMISSIVE_TEXTURE (1 << 5)

// Note: Bits for flags field in TranslucentSurfaceMaterial and TranslucentSurfaceMaterialInteraction
// If set, then the texture bound to transmittanceOrDiffuseTextureIndex is an albedo map for the diffuse layer
Expand Down

0 comments on commit 03d2e19

Please sign in to comment.