Skip to content

Commit

Permalink
Output to an rgba16ui image for the BC1 and BC4 shaders
Browse files Browse the repository at this point in the history
Increases compatibility with GLSL ES version 310.

The ARB_shader_image_load_store extension says,

	For textures allocated by the GL, an image unit format is
	compatible with a texture internal format if they match by size.

The textures were allocated by the GL implementation, so this format
substitution is safe to do.
  • Loading branch information
nchery-intel committed Oct 4, 2022
1 parent 4d32a7a commit dfc01a9
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 13 deletions.
12 changes: 7 additions & 5 deletions bin/Data/bc1.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ uniform uint p_numRefinements;

uniform sampler2D srcTex;

layout( rg32ui ) uniform restrict writeonly highp uimage2D dstTexture;
layout( rgba16ui ) uniform restrict writeonly mediump uimage2D dstTexture;

layout( std430, binding = 1 ) readonly restrict buffer globalBuffer
{
Expand Down Expand Up @@ -513,10 +513,12 @@ void main()
mask ^= 0x55555555u;
}

uint2 outputBytes;
outputBytes.x = uint( maxEndp16 ) | ( uint( minEndp16 ) << 16u );
outputBytes.y = mask;
uint4 outputBytes;
outputBytes.x = uint( maxEndp16 );
outputBytes.y = uint( minEndp16 );
outputBytes.z = mask & 0xFFFFu;
outputBytes.w = mask >> 16u;

uint2 dstUV = gl_GlobalInvocationID.xy;
imageStore( dstTexture, int2( dstUV ), uint4( outputBytes.xy, 0u, 0u ) );
imageStore( dstTexture, int2( dstUV ), outputBytes );
}
11 changes: 6 additions & 5 deletions bin/Data/bc4.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ layout( location = 0 ) uniform float2 params;

uniform sampler2D srcTex;

layout( rg32ui ) uniform restrict writeonly highp uimage2D dstTexture;
layout( rgba16ui ) uniform restrict writeonly mediump uimage2D dstTexture;

layout( local_size_x = 4, //
local_size_y = 4, //
Expand Down Expand Up @@ -142,7 +142,7 @@ void main()
if( blockThreadId == 0u )
{
// Save data
uint2 outputBytes;
uint4 outputBytes;

if( p_useSNorm != 0.0f )
{
Expand All @@ -155,10 +155,11 @@ void main()
outputBytes.x = packUnorm4x8(
float4( maxVal * ( 1.0f / 255.0f ), minVal * ( 1.0f / 255.0f ), 0.0f, 0.0f ) );
}
outputBytes.x |= g_mask[maskIdxBase].x;
outputBytes.y = g_mask[maskIdxBase].y;
outputBytes.y = g_mask[maskIdxBase].x >> 16u;
outputBytes.z = g_mask[maskIdxBase].y & 0xFFFFu;
outputBytes.w = g_mask[maskIdxBase].y >> 16u;

uint2 dstUV = gl_GlobalInvocationID.yz;
imageStore( dstTexture, int2( dstUV ), uint4( outputBytes.xy, 0u, 0u ) );
imageStore( dstTexture, int2( dstUV ), outputBytes );
}
}
4 changes: 2 additions & 2 deletions src/betsy/EncoderBC1.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ namespace betsy
{
bindTexture( 0u, m_srcTexture );
bindComputePso( m_bc1Pso );
bindUav( 0u, m_bc1TargetRes, PFG_RG32_UINT, ResourceAccess::Write );
bindUav( 0u, m_bc1TargetRes, PFG_RGBA16_UINT, ResourceAccess::Write );
bindUavBuffer( 1u, m_bc1TablesSsbo, 0u, sizeof( Bc1Tables ) );

glUniform1ui( 0, 2u );
Expand All @@ -128,7 +128,7 @@ namespace betsy
{
// Compress Alpha too (using BC4)
bindComputePso( m_bc4Pso );
bindUav( 0u, m_bc4TargetRes, PFG_RG32_UINT, ResourceAccess::Write );
bindUav( 0u, m_bc4TargetRes, PFG_RGBA16_UINT, ResourceAccess::Write );

// p_channelIdx, p_useSNorm
glUniform2f( 0, 3.0f, 0.0f );
Expand Down
2 changes: 1 addition & 1 deletion src/betsy/EncoderBC4.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ namespace betsy
const size_t numChannels = m_bc4TargetRes[1] ? 2u : 1u;
for( size_t i = 0u; i < numChannels; ++i )
{
bindUav( 0u, m_bc4TargetRes[i], PFG_RG32_UINT, ResourceAccess::Write );
bindUav( 0u, m_bc4TargetRes[i], PFG_RGBA16_UINT, ResourceAccess::Write );

// p_channelIdx, p_useSNorm
glUniform2f( 0, i == 0u ? 0.0f : 1.0f, m_encodeSNorm ? 1.0f : 0.0f );
Expand Down

0 comments on commit dfc01a9

Please sign in to comment.