| 1 |
/* |
| 2 |
* Copyright © 2025 Imagination Technologies Ltd. |
| 3 |
* SPDX-License-Identifier: MIT |
| 4 |
*/ |
| 5 |
|
| 6 |
#ifndef __pvr_address_type |
| 7 |
#define __pvr_address_type uint64_t |
| 8 |
#define __pvr_get_address(addr) addr |
| 9 |
#define __pvr_make_address(addr) addr |
| 10 |
#endif /* __pvr_address_type */ |
| 11 |
|
| 12 |
#include "csbgen/rogue/texstate.h" |
| 13 |
#include "libcl.h" |
| 14 |
#include "util/u_math.h" |
| 15 |
|
| 16 |
|
| 17 |
uint |
| 18 |
usclib_tex_state_levels(uint4 tex_state) |
| 19 |
{ |
| 20 |
uint32_t texstate_image_word1[] = {tex_state.z, tex_state.w}; |
| 21 |
struct ROGUE_TEXSTATE_IMAGE_WORD1 texstate_image_struct; |
| 22 |
|
| 23 |
ROGUE_TEXSTATE_IMAGE_WORD1_unpack(texstate_image_word1, &texstate_image_struct); |
| 24 |
|
| 25 |
return texstate_image_struct.num_mip_levels; |
| 26 |
} |
| 27 |
|
| 28 |
uint |
| 29 |
usclib_tex_state_samples(uint4 tex_state) |
| 30 |
{ |
| 31 |
uint32_t texstate_image_word0[] = {tex_state.x, tex_state.y}; |
| 32 |
struct ROGUE_TEXSTATE_IMAGE_WORD0 texstate_image_struct; |
| 33 |
|
| 34 |
ROGUE_TEXSTATE_IMAGE_WORD0_unpack(texstate_image_word0, &texstate_image_struct); |
| 35 |
|
| 36 |
return 1 << texstate_image_struct.smpcnt; |
| 37 |
} |
| 38 |
|
| 39 |
uint2 |
| 40 |
usclib_tex_state_address(uint4 tex_state) |
| 41 |
{ |
| 42 |
uint32_t texstate_image_word1[] = {tex_state.z, tex_state.w}; |
| 43 |
struct ROGUE_TEXSTATE_IMAGE_WORD1 texstate_image_struct; |
| 44 |
|
| 45 |
ROGUE_TEXSTATE_IMAGE_WORD1_unpack(texstate_image_word1, &texstate_image_struct); |
| 46 |
|
| 47 |
uint64_t addr = texstate_image_struct.texaddr; |
| 48 |
|
| 49 |
return (uint2)(addr & 0xffffffff, addr >> 32); |
| 50 |
} |
| 51 |
|
| 52 |
uint |
| 53 |
usclib_tex_state_array_max(uint4 tex_state) |
| 54 |
{ |
| 55 |
uint32_t texstate_image_word1[] = {tex_state.z, tex_state.w}; |
| 56 |
struct ROGUE_TEXSTATE_IMAGE_WORD1 texstate_image_struct; |
| 57 |
|
| 58 |
ROGUE_TEXSTATE_IMAGE_WORD1_unpack(texstate_image_word1, &texstate_image_struct); |
| 59 |
|
| 60 |
return texstate_image_struct.depth; |
| 61 |
} |
| 62 |
|
| 63 |
uint3 |
| 64 |
usclib_tex_state_size(uint4 tex_state, uint num_comps, enum glsl_sampler_dim dim, bool is_array, bool is_image, uint lod) |
| 65 |
{ |
| 66 |
uint32_t texstate_image_word0[] = {tex_state.x, tex_state.y}; |
| 67 |
uint32_t texstate_image_word1[] = {tex_state.z, tex_state.w}; |
| 68 |
struct ROGUE_TEXSTATE_IMAGE_WORD0 texstate_image_struct0; |
| 69 |
struct ROGUE_TEXSTATE_IMAGE_WORD1 texstate_image_struct1; |
| 70 |
|
| 71 |
ROGUE_TEXSTATE_IMAGE_WORD0_unpack(texstate_image_word0, &texstate_image_struct0); |
| 72 |
ROGUE_TEXSTATE_IMAGE_WORD1_unpack(texstate_image_word1, &texstate_image_struct1); |
| 73 |
|
| 74 |
lod += texstate_image_struct1.baselevel; |
| 75 |
|
| 76 |
uint3 size_comps = (uint3)(texstate_image_struct0.width + 1, |
| 77 |
texstate_image_struct0.height + 1, |
| 78 |
texstate_image_struct1.depth + 1); |
| 79 |
|
| 80 |
if (is_array) |
| 81 |
--num_comps; |
| 82 |
|
| 83 |
for (unsigned u = 0; u < num_comps; ++u) |
| 84 |
size_comps[u] = MAX2(size_comps[u] >> lod, 1); |
| 85 |
|
| 86 |
if (is_array) { |
| 87 |
if (dim == GLSL_SAMPLER_DIM_1D) |
| 88 |
size_comps.y = size_comps.z; |
| 89 |
else if (dim == GLSL_SAMPLER_DIM_CUBE && is_image) |
| 90 |
size_comps.z /= 6; |
| 91 |
} |
| 92 |
|
| 93 |
return size_comps; |
| 94 |
} |
| 95 |
|
| 96 |
uint |
| 97 |
usclib_tex_state_base_level(uint4 tex_state) |
| 98 |
{ |
| 99 |
uint32_t texstate_image_word1[] = {tex_state.z, tex_state.w}; |
| 100 |
struct ROGUE_TEXSTATE_IMAGE_WORD1 texstate_image_struct; |
| 101 |
|
| 102 |
ROGUE_TEXSTATE_IMAGE_WORD1_unpack(texstate_image_word1, &texstate_image_struct); |
| 103 |
|
| 104 |
return texstate_image_struct.baselevel; |
| 105 |
} |
| 106 |
|
| 107 |
bool |
| 108 |
usclib_smp_state_mipfilter(uint4 smp_state) |
| 109 |
{ |
| 110 |
uint32_t texstate_sampler_word0[] = {smp_state.x, smp_state.y}; |
| 111 |
struct ROGUE_TEXSTATE_SAMPLER_WORD0 texstate_sampler_struct; |
| 112 |
|
| 113 |
ROGUE_TEXSTATE_SAMPLER_WORD0_unpack(texstate_sampler_word0, &texstate_sampler_struct); |
| 114 |
|
| 115 |
return texstate_sampler_struct.mipfilter; |
| 116 |
} |
| 117 |
|
| 118 |
float |
| 119 |
usclib_tex_lod_dval_post_clamp_resource_to_view_space(uint4 tex_state, uint4 smp_state, float lod_dval_post_clamp) |
| 120 |
{ |
| 121 |
lod_dval_post_clamp -= (float)usclib_tex_state_base_level(tex_state); |
| 122 |
if (!usclib_smp_state_mipfilter(smp_state)) |
| 123 |
lod_dval_post_clamp = floor(lod_dval_post_clamp + 0.5f); |
| 124 |
|
| 125 |
return MAX2(lod_dval_post_clamp, 0.0f); |
| 126 |
} |
| 127 |
|
| 128 |
/* TODO: this can probably be optimized with nir_interleave. */ |
| 129 |
uint32_t |
| 130 |
usclib_twiddle3d(uint3 coords, uint3 size) |
| 131 |
{ |
| 132 |
uint32_t width = nir_umax(size.x, 4); |
| 133 |
width = util_next_power_of_two(width); |
| 134 |
|
| 135 |
uint32_t height = nir_umax(size.y, 4); |
| 136 |
height = util_next_power_of_two(height); |
| 137 |
|
| 138 |
uint32_t depth = nir_umax(size.z, 4); |
| 139 |
depth = util_next_power_of_two(depth); |
| 140 |
|
| 141 |
/* Get to the inner 4x4 cube. */ |
| 142 |
width /= 4; |
| 143 |
height /= 4; |
| 144 |
depth /= 4; |
| 145 |
|
| 146 |
uint32_t cx = coords.x / 4; |
| 147 |
uint32_t cy = coords.y / 4; |
| 148 |
uint32_t cz = coords.z / 4; |
| 149 |
uint32_t shift = 0; |
| 150 |
uint32_t cubeoffset = 0; |
| 151 |
uint32_t i = 0; |
| 152 |
|
| 153 |
while (width > 1 || height > 1 || depth > 1) { |
| 154 |
uint32_t b1, b2, b3; |
| 155 |
|
| 156 |
if (height > 1) { |
| 157 |
b2 = ((cy & (1 << i)) >> i); |
| 158 |
cubeoffset |= (b2 << shift); |
| 159 |
shift++; |
| 160 |
height >>= 1; |
| 161 |
} |
| 162 |
|
| 163 |
if (width > 1) { |
| 164 |
b1 = ((cx & (1 << i)) >> i); |
| 165 |
cubeoffset |= (b1 << shift); |
| 166 |
shift++; |
| 167 |
width >>= 1; |
| 168 |
} |
| 169 |
|
| 170 |
if (depth > 1) { |
| 171 |
b3 = ((cz & (1 << i)) >> i); |
| 172 |
cubeoffset |= (b3 << shift); |
| 173 |
shift++; |
| 174 |
depth >>= 1; |
| 175 |
} |
| 176 |
|
| 177 |
++i; |
| 178 |
} |
| 179 |
|
| 180 |
cubeoffset *= 4 * 4 * 4; |
| 181 |
|
| 182 |
/* Get to slice. */ |
| 183 |
cubeoffset += 4 * 4 * (coords.z % 4); |
| 184 |
|
| 185 |
/* Twiddle within slice. */ |
| 186 |
uint32_t r = (coords.y & 1) | ((coords.x & 1) << 1) | (((coords.y & 2) >> 1) << 2) | (((coords.x & 2) >> 1) << 3); |
| 187 |
|
| 188 |
return cubeoffset + r; |
| 189 |
} |