|
| 1 | +#ifndef GXM_UTIL_H |
| 2 | +#define GXM_UTIL_H |
| 3 | + |
| 4 | +#include <psp2/gxm.h> |
| 5 | +#include <switch.h> |
| 6 | + |
| 7 | +#define SCE_GXM_NOTIFICATION_COUNT 512 |
| 8 | + |
| 9 | +#define SCE_GXM_COLOR_BASE_FORMAT_MASK 0xF1800000U |
| 10 | +#define SCE_GXM_TEXTURE_BASE_FORMAT_MASK 0x9f000000U |
| 11 | + |
| 12 | +#define SCE_GXM_DEPTH_STENCIL_ZLS_CTRL_DISABLE_BIT 1 |
| 13 | +#define SCE_GXM_DEPTH_STENCIL_ZLS_CTRL_STRIDE_OFFSET 3 |
| 14 | +#define SCE_GXM_DEPTH_STENCIL_ZLS_CTRL_STRIDE_MASK 0xF |
| 15 | +#define SCE_GXM_DEPTH_STENCIL_ZLS_CTRL_TYPE_OFFSET 12 |
| 16 | +#define SCE_GXM_DEPTH_STENCIL_ZLS_CTRL_TYPE_MASK 0xFF |
| 17 | +#define SCE_GXM_DEPTH_STENCIL_ZLS_CTRL_FORMAT_OFFSET 21 |
| 18 | +#define SCE_GXM_DEPTH_STENCIL_ZLS_CTRL_FORMAT_MASK 0x7FF |
| 19 | + |
| 20 | +#define SCE_GXM_DEPTH_STENCIL_BG_CTRL_STENCIL_MASK 0xFF |
| 21 | +#define SCE_GXM_DEPTH_STENCIL_BG_CTRL_MASK_BIT 0x100 |
| 22 | + |
| 23 | +typedef struct { |
| 24 | + // Control Word 0 |
| 25 | + uint32_t unk0 : 3; |
| 26 | + uint32_t vaddr_mode : 3; |
| 27 | + uint32_t uaddr_mode : 3; |
| 28 | + uint32_t mip_filter : 1; |
| 29 | + uint32_t min_filter : 2; |
| 30 | + uint32_t mag_filter : 2; |
| 31 | + uint32_t unk1 : 3; |
| 32 | + uint32_t mip_count : 4; |
| 33 | + uint32_t lod_bias : 6; |
| 34 | + uint32_t gamma_mode : 2; |
| 35 | + uint32_t unk2 : 2; |
| 36 | + uint32_t format0 : 1; |
| 37 | + // Control Word 1 |
| 38 | + union { |
| 39 | + struct { |
| 40 | + uint32_t height : 12; |
| 41 | + uint32_t width : 12; |
| 42 | + }; |
| 43 | + |
| 44 | + struct { |
| 45 | + uint32_t height_base2 : 4; |
| 46 | + uint32_t unknown1 : 12; |
| 47 | + uint32_t width_base2 : 4; |
| 48 | + uint32_t unknown2 : 4; |
| 49 | + }; |
| 50 | + |
| 51 | + struct { |
| 52 | + uint32_t whblock : 24; |
| 53 | + uint32_t base_format : 5; |
| 54 | + uint32_t type : 3; |
| 55 | + }; |
| 56 | + }; |
| 57 | + // Control Word 2 |
| 58 | + uint32_t lod_min0 : 2; |
| 59 | + uint32_t data_addr : 30; |
| 60 | + // Control Word 3 |
| 61 | + uint32_t palette_addr : 26; |
| 62 | + uint32_t lod_min1 : 2; |
| 63 | + uint32_t swizzle_format : 3; |
| 64 | + uint32_t normalize_mode : 1; |
| 65 | +} SceGxmTextureInner; |
| 66 | +static_assert(sizeof(SceGxmTextureInner) == sizeof(SceGxmTexture), "Incorrect size"); |
| 67 | + |
| 68 | +typedef struct { |
| 69 | + // opaque start |
| 70 | + uint32_t disabled : 1; |
| 71 | + uint32_t downscale : 1; |
| 72 | + uint32_t pad : 30; |
| 73 | + uint32_t width; |
| 74 | + uint32_t height; |
| 75 | + uint32_t strideInPixels; |
| 76 | + void *data; |
| 77 | + SceGxmColorFormat colorFormat; |
| 78 | + SceGxmColorSurfaceType surfaceType; |
| 79 | + // opaque end |
| 80 | + uint32_t outputRegisterSize; |
| 81 | + SceGxmTexture backgroundTex; |
| 82 | +} SceGxmColorSurfaceInner; |
| 83 | +static_assert(sizeof(SceGxmColorSurfaceInner) == sizeof(SceGxmColorSurface), "Incorrect size"); |
| 84 | + |
| 85 | +typedef struct SceGxmProgram { |
| 86 | + uint32_t magic; // should be "GXP\0" |
| 87 | + |
| 88 | + uint8_t major_version; // min 1 |
| 89 | + uint8_t minor_version; // min 4 |
| 90 | + uint16_t sdk_version; // 0x350 - 3.50 |
| 91 | + |
| 92 | + uint32_t |
| 93 | + size; // size of file - ignoring padding bytes at the end after SceGxmProgramParameter table |
| 94 | + |
| 95 | + uint32_t binary_guid; |
| 96 | + uint32_t source_guid; |
| 97 | + |
| 98 | + uint32_t program_flags; |
| 99 | + |
| 100 | + uint32_t buffer_flags; // Buffer flags. 2 bits per buffer. 0x1 - loaded into registers. 0x2 - |
| 101 | + // read from memory |
| 102 | + |
| 103 | + uint32_t texunit_flags[2]; // Tex unit flags. 4 bits per tex unit. 0x1 is non dependent read, |
| 104 | + // 0x2 is dependent. |
| 105 | + |
| 106 | + uint32_t parameter_count; |
| 107 | + uint32_t |
| 108 | + parameters_offset; // Number of bytes from the start of this field to the first parameter. |
| 109 | + uint32_t varyings_offset; // offset to vertex outputs / fragment inputs, relative to this field |
| 110 | + |
| 111 | + uint16_t primary_reg_count; // (PAs) |
| 112 | + uint16_t secondary_reg_count; // (SAs) |
| 113 | + uint32_t temp_reg_count1; |
| 114 | + uint16_t temp_reg_count2; // Temp reg count in selective rate(programmable blending) phase |
| 115 | + |
| 116 | + uint16_t primary_program_phase_count; |
| 117 | + uint32_t primary_program_instr_count; |
| 118 | + uint32_t primary_program_offset; |
| 119 | + |
| 120 | + uint32_t secondary_program_instr_count; |
| 121 | + uint32_t secondary_program_offset; // relative to the beginning of this field |
| 122 | + uint32_t secondary_program_offset_end; // relative to the beginning of this field |
| 123 | + |
| 124 | + uint32_t scratch_buffer_count; |
| 125 | + uint32_t thread_buffer_count; |
| 126 | + uint32_t literal_buffer_count; |
| 127 | + |
| 128 | + uint32_t data_buffer_count; |
| 129 | + uint32_t texture_buffer_count; |
| 130 | + uint32_t default_uniform_buffer_count; |
| 131 | + |
| 132 | + uint32_t literal_buffer_data_offset; |
| 133 | + |
| 134 | + uint32_t compiler_version; // The version is shifted 4 bits to the left. |
| 135 | + |
| 136 | + uint32_t literals_count; |
| 137 | + uint32_t literals_offset; |
| 138 | + uint32_t uniform_buffer_count; |
| 139 | + uint32_t uniform_buffer_offset; |
| 140 | + |
| 141 | + uint32_t dependent_sampler_count; |
| 142 | + uint32_t dependent_sampler_offset; |
| 143 | + uint32_t texture_buffer_dependent_sampler_count; |
| 144 | + uint32_t texture_buffer_dependent_sampler_offset; |
| 145 | + uint32_t container_count; |
| 146 | + uint32_t container_offset; |
| 147 | + uint32_t sampler_query_info_offset; // Offset to array of uint16_t |
| 148 | +} SceGxmProgram; |
| 149 | + |
| 150 | +typedef struct SceGxmProgramParameter { |
| 151 | + int32_t name_offset; // Number of bytes from the start of this structure to the name string. |
| 152 | + struct { |
| 153 | + uint16_t category : 4; // SceGxmParameterCategory |
| 154 | + uint16_t type : 4; // SceGxmParameterType - applicable for constants, not applicable for |
| 155 | + // samplers (select type like float, half, fixed ...) |
| 156 | + uint16_t component_count : 4; // applicable for constants, not applicable for samplers |
| 157 | + // (select size like float2, float3, float3 ...) |
| 158 | + uint16_t container_index : 4; // applicable for constants, not applicable for samplers |
| 159 | + // (buffer, default, texture) |
| 160 | + }; |
| 161 | + uint8_t semantic; // applicable only for for vertex attributes, for everything else it's 0 |
| 162 | + uint8_t semantic_index; |
| 163 | + uint32_t array_size; |
| 164 | + int32_t resource_index; |
| 165 | +} SceGxmProgramParameter; |
| 166 | + |
| 167 | +static inline uint32_t gxm_parameter_type_size(SceGxmParameterType type) |
| 168 | +{ |
| 169 | + switch (type) { |
| 170 | + case SCE_GXM_PARAMETER_TYPE_U8: |
| 171 | + case SCE_GXM_PARAMETER_TYPE_S8: |
| 172 | + return 1; |
| 173 | + case SCE_GXM_PARAMETER_TYPE_F16: |
| 174 | + case SCE_GXM_PARAMETER_TYPE_U16: |
| 175 | + case SCE_GXM_PARAMETER_TYPE_S16: |
| 176 | + return 2; |
| 177 | + case SCE_GXM_PARAMETER_TYPE_F32: |
| 178 | + case SCE_GXM_PARAMETER_TYPE_U32: |
| 179 | + case SCE_GXM_PARAMETER_TYPE_S32: |
| 180 | + default: |
| 181 | + return 4; |
| 182 | + } |
| 183 | +} |
| 184 | + |
| 185 | +static inline uint32_t gxm_texture_get_type(const SceGxmTextureInner *texture) |
| 186 | +{ |
| 187 | + return texture->type << 29; |
| 188 | +} |
| 189 | + |
| 190 | +static inline size_t gxm_texture_get_width(const SceGxmTextureInner *texture) |
| 191 | +{ |
| 192 | + if (gxm_texture_get_type(texture) != SCE_GXM_TEXTURE_SWIZZLED && |
| 193 | + gxm_texture_get_type(texture) != SCE_GXM_TEXTURE_CUBE) |
| 194 | + return texture->width + 1; |
| 195 | + return 1ull << (texture->width_base2 & 0xF); |
| 196 | +} |
| 197 | + |
| 198 | +static inline size_t gxm_texture_get_height(const SceGxmTextureInner *texture) |
| 199 | +{ |
| 200 | + if (gxm_texture_get_type(texture) != SCE_GXM_TEXTURE_SWIZZLED && |
| 201 | + gxm_texture_get_type(texture) != SCE_GXM_TEXTURE_CUBE) |
| 202 | + return texture->height + 1; |
| 203 | + return 1ull << (texture->height_base2 & 0xF); |
| 204 | +} |
| 205 | + |
| 206 | +static inline SceGxmTextureFormat gxm_texture_get_format(const SceGxmTextureInner *texture) |
| 207 | +{ |
| 208 | + return (SceGxmTextureFormat)(texture->base_format << 24 | texture->format0 << 31 | |
| 209 | + texture->swizzle_format << 12); |
| 210 | +} |
| 211 | + |
| 212 | +static inline SceGxmTextureBaseFormat gxm_texture_get_base_format(SceGxmTextureFormat src) |
| 213 | +{ |
| 214 | + return (SceGxmTextureBaseFormat)(src & SCE_GXM_TEXTURE_BASE_FORMAT_MASK); |
| 215 | +} |
| 216 | + |
| 217 | +static inline size_t gxm_texture_get_stride_in_bytes(const SceGxmTextureInner *texture) |
| 218 | +{ |
| 219 | + return ((texture->mip_filter | (texture->min_filter << 1) | (texture->mip_count << 3) | |
| 220 | + (texture->lod_bias << 7)) + |
| 221 | + 1) * |
| 222 | + 4; |
| 223 | +} |
| 224 | + |
| 225 | +static inline bool gxm_base_format_is_paletted_format(SceGxmTextureBaseFormat base_format) |
| 226 | +{ |
| 227 | + return base_format == SCE_GXM_TEXTURE_BASE_FORMAT_P8 || |
| 228 | + base_format == SCE_GXM_TEXTURE_BASE_FORMAT_P4; |
| 229 | +} |
| 230 | + |
| 231 | +#endif |
0 commit comments