xref: /aosp_15_r20/external/mesa3d/src/gallium/frontends/nine/pixelshader9.h (revision 6104692788411f58d303aa86923a9ff6ecaded22)
1 /*
2  * Copyright 2011 Joakim Sindholt <[email protected]>
3  * SPDX-License-Identifier: MIT
4  */
5 
6 #ifndef _NINE_PIXELSHADER9_H_
7 #define _NINE_PIXELSHADER9_H_
8 
9 #include "iunknown.h"
10 #include "nine_shader.h"
11 #include "nine_state.h"
12 #include "basetexture9.h"
13 #include "nine_ff.h"
14 #include "surface9.h"
15 
16 struct nine_lconstf;
17 
18 struct NinePixelShader9
19 {
20     struct NineUnknown base;
21     struct nine_shader_variant variant;
22 
23     struct {
24         const DWORD *tokens;
25         DWORD size;
26         uint8_t version; /* (major << 4) | minor */
27     } byte_code;
28 
29     uint8_t bumpenvmat_needed;
30     uint16_t sampler_mask;
31     uint8_t rt_mask;
32 
33     bool int_slots_used[NINE_MAX_CONST_I];
34     bool bool_slots_used[NINE_MAX_CONST_B];
35 
36     unsigned const_int_slots;
37     unsigned const_bool_slots;
38 
39     struct nine_shader_constant_combination *c_combinations;
40 
41     uint64_t ff_key[6];
42     void *ff_cso;
43 
44     uint64_t last_key;
45     void *last_cso;
46     unsigned *last_const_ranges;
47     unsigned last_const_used_size; /* in bytes */
48 
49     uint64_t next_key;
50 };
51 static inline struct NinePixelShader9 *
NinePixelShader9(void * data)52 NinePixelShader9( void *data )
53 {
54     return (struct NinePixelShader9 *)data;
55 }
56 
57 static inline BOOL
NinePixelShader9_UpdateKey(struct NinePixelShader9 * ps,struct nine_context * context)58 NinePixelShader9_UpdateKey( struct NinePixelShader9 *ps,
59                             struct nine_context *context )
60 {
61     uint16_t samplers_shadow;
62     uint16_t samplers_fetch4;
63     uint16_t samplers_ps1_types;
64     uint8_t projected;
65     uint64_t key;
66     BOOL res;
67 
68     samplers_shadow = (uint16_t)((context->samplers_shadow & NINE_PS_SAMPLERS_MASK) >> NINE_SAMPLER_PS(0));
69     samplers_fetch4 = (uint16_t)((context->samplers_fetch4 & NINE_PS_SAMPLERS_MASK) >> NINE_SAMPLER_PS(0));
70     key = samplers_shadow & ps->sampler_mask;
71     samplers_fetch4 &= ps->sampler_mask;
72 
73     if (unlikely(ps->byte_code.version < 0x20)) {
74         /* variable targets */
75         uint32_t m = ps->sampler_mask;
76         samplers_ps1_types = 0;
77         while (m) {
78             int s = ffs(m) - 1;
79             m &= ~(1 << s);
80             samplers_ps1_types |= (context->texture[s].enabled ? context->texture[s].pstype : 1) << (s * 2);
81         }
82         /* Note: For ps 1.X, only samplers 0 1 2 and 3 are available (except 1.4 where 4 and 5 are available).
83          * ps < 1.4: samplers_shadow 4b, samplers_ps1_types 8b, projected 8b
84          * ps 1.4: samplers_shadow 6b, samplers_ps1_types 12b
85          * Tot ps X.X samplers_shadow + extra: 20b */
86         assert((ps->byte_code.version < 0x14 && !(ps->sampler_mask & 0xFFF0)) || !(ps->sampler_mask & 0xFFC0));
87 
88         if (unlikely(ps->byte_code.version < 0x14)) {
89             key |= samplers_ps1_types << 4;
90             projected = nine_ff_get_projected_key_programmable(context);
91             key |= ((uint64_t) projected) << 12;
92         } else {
93             key |= samplers_ps1_types << 6;
94         }
95     }
96 
97     if (ps->byte_code.version < 0x30 && context->rs[D3DRS_FOGENABLE]) {
98         key |= 1 << 20;
99         key |= ((uint64_t)context->rs[D3DRS_FOGTABLEMODE]) << 21; /* 2 bits */
100         key |= ((uint64_t)context->zfog) << 23;
101     }
102 
103     if ((ps->const_int_slots > 0 || ps->const_bool_slots > 0) && context->inline_constants)
104         key |= ((uint64_t)nine_shader_constant_combination_key(&ps->c_combinations,
105                                                                ps->int_slots_used,
106                                                                ps->bool_slots_used,
107                                                                (void *)context->ps_const_i,
108                                                                context->ps_const_b)) << 24;
109 
110     key |= ((uint64_t)(context->rs[NINED3DRS_FETCH4] & samplers_fetch4)) << 32; /* 16 bits */
111 
112     /* centroid interpolation automatically used for color ps inputs */
113     if (context->rt[0]->base.info.nr_samples)
114         key |= ((uint64_t)1) << 48;
115     key |= ((uint64_t)(context->rs[NINED3DRS_EMULATED_ALPHATEST] & 0x7)) << 49; /* 3 bits */
116     if (context->rs[D3DRS_SHADEMODE] == D3DSHADE_FLAT)
117         key |= ((uint64_t)1) << 52;
118 
119     res = ps->last_key != key;
120     if (res)
121         ps->next_key = key;
122     return res;
123 }
124 
125 void *
126 NinePixelShader9_GetVariant( struct NinePixelShader9 *ps,
127                              unsigned **const_ranges,
128                              unsigned *const_used_size );
129 
130 /*** public ***/
131 
132 HRESULT
133 NinePixelShader9_new( struct NineDevice9 *pDevice,
134                       struct NinePixelShader9 **ppOut,
135                       const DWORD *pFunction, void *cso );
136 
137 HRESULT
138 NinePixelShader9_ctor( struct NinePixelShader9 *,
139                        struct NineUnknownParams *pParams,
140                        const DWORD *pFunction, void *cso );
141 
142 void
143 NinePixelShader9_dtor( struct NinePixelShader9 * );
144 
145 HRESULT NINE_WINAPI
146 NinePixelShader9_GetFunction( struct NinePixelShader9 *This,
147                               void *pData,
148                               UINT *pSizeOfData );
149 
150 #endif /* _NINE_PIXELSHADER9_H_ */
151