Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mesa
Path: blob/21.2-virgl/src/gallium/drivers/i915/i915_state_sampler.c
4570 views
1
/**************************************************************************
2
*
3
* Copyright 2003 VMware, Inc.
4
* All Rights Reserved.
5
*
6
* Permission is hereby granted, free of charge, to any person obtaining a
7
* copy of this software and associated documentation files (the
8
* "Software"), to deal in the Software without restriction, including
9
* without limitation the rights to use, copy, modify, merge, publish,
10
* distribute, sub license, and/or sell copies of the Software, and to
11
* permit persons to whom the Software is furnished to do so, subject to
12
* the following conditions:
13
*
14
* The above copyright notice and this permission notice (including the
15
* next paragraph) shall be included in all copies or substantial portions
16
* of the Software.
17
*
18
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21
* IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
22
* ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25
*
26
**************************************************************************/
27
28
#include "pipe/p_context.h"
29
#include "pipe/p_state.h"
30
31
#include "i915_context.h"
32
#include "i915_reg.h"
33
#include "i915_resource.h"
34
#include "i915_state.h"
35
#include "i915_state_inlines.h"
36
37
/*
38
* A note about min_lod & max_lod.
39
*
40
* There is a circular dependancy between the sampler state
41
* and the map state to be submitted to hw.
42
*
43
* Two condition must be meet:
44
* min_lod =< max_lod == true
45
* max_lod =< last_level == true
46
*
47
*
48
* This is all fine and dandy if it were for the fact that max_lod
49
* is set on the map state instead of the sampler state. That is
50
* the max_lod we submit on map is:
51
* max_lod = MIN2(last_level, max_lod);
52
*
53
* So we need to update the map state when we change samplers and
54
* we need to change the sampler state when map state is changed.
55
* The first part is done by calling update_texture in update_samplers
56
* and the second part is done else where in code tracking the state
57
* changes.
58
*/
59
60
/***********************************************************************
61
* Samplers
62
*/
63
64
/**
65
* Compute i915 texture sampling state.
66
*
67
* Recalculate all state from scratch. Perhaps not the most
68
* efficient, but this has gotten complex enough that we need
69
* something which is understandable and reliable.
70
* \param state returns the 3 words of compute state
71
*/
72
static void
73
update_sampler(struct i915_context *i915, uint32_t unit,
74
const struct i915_sampler_state *sampler,
75
const struct i915_texture *tex, unsigned state[3])
76
{
77
const struct pipe_resource *pt = &tex->b;
78
unsigned minlod, lastlod;
79
80
state[0] = sampler->state[0];
81
state[1] = sampler->state[1];
82
state[2] = sampler->state[2];
83
84
if (pt->format == PIPE_FORMAT_UYVY || pt->format == PIPE_FORMAT_YUYV)
85
state[0] |= SS2_COLORSPACE_CONVERSION;
86
87
if (pt->format == PIPE_FORMAT_B8G8R8A8_SRGB ||
88
pt->format == PIPE_FORMAT_L8_SRGB) {
89
state[0] |= SS2_REVERSE_GAMMA_ENABLE;
90
}
91
92
/* There is no HW support for 1D textures, so we just make them 2D textures
93
* with h=1, but that means we need to make the Y coordinate not contribute
94
* to bringing any border color in. Clearing it sets it to WRAP.
95
*/
96
if (pt->target == PIPE_TEXTURE_1D) {
97
state[1] &= ~SS3_TCY_ADDR_MODE_MASK;
98
}
99
100
/* The GLES2 spec says textures are incomplete (return 0,0,0,1) if:
101
*
102
* "A cube map sampler is called, any of the corresponding texture images are
103
* non-power-of-two images, and either the texture wrap mode is not
104
* CLAMP_TO_EDGE, or the minification filter is neither NEAREST nor LINEAR."
105
*
106
* while the i915 spec says:
107
*
108
* "When using cube map texture coordinates, only TEXCOORDMODE_CLAMP and *
109
* TEXCOORDMODE_CUBE settings are valid, and each TC component must have the
110
* same Address Control mode. TEXCOORDMODE_CUBE is not valid unless the
111
* width and height of the cube map are power-of-2."
112
*
113
* We don't expose support for the seamless cube map extension, so always use
114
* edge clamping.
115
*/
116
if (pt->target == PIPE_TEXTURE_CUBE) {
117
state[1] &= ~(SS3_TCX_ADDR_MODE_MASK | SS3_TCY_ADDR_MODE_MASK |
118
SS3_TCZ_ADDR_MODE_MASK);
119
state[1] |= (TEXCOORDMODE_CLAMP_EDGE << SS3_TCX_ADDR_MODE_SHIFT);
120
state[1] |= (TEXCOORDMODE_CLAMP_EDGE << SS3_TCY_ADDR_MODE_SHIFT);
121
state[1] |= (TEXCOORDMODE_CLAMP_EDGE << SS3_TCZ_ADDR_MODE_SHIFT);
122
}
123
124
/* 3D textures don't seem to respect the border color.
125
* Fallback if there's ever a danger that they might refer to
126
* it.
127
*
128
* Effectively this means fallback on 3D clamp or
129
* clamp_to_border.
130
*
131
* XXX: Check if this is true on i945.
132
* XXX: Check if this bug got fixed in release silicon.
133
*/
134
#if 0
135
{
136
const unsigned ws = sampler->templ->wrap_s;
137
const unsigned wt = sampler->templ->wrap_t;
138
const unsigned wr = sampler->templ->wrap_r;
139
if (pt->target == PIPE_TEXTURE_3D &&
140
(sampler->templ->min_img_filter != PIPE_TEX_FILTER_NEAREST ||
141
sampler->templ->mag_img_filter != PIPE_TEX_FILTER_NEAREST) &&
142
(ws == PIPE_TEX_WRAP_CLAMP ||
143
wt == PIPE_TEX_WRAP_CLAMP ||
144
wr == PIPE_TEX_WRAP_CLAMP ||
145
ws == PIPE_TEX_WRAP_CLAMP_TO_BORDER ||
146
wt == PIPE_TEX_WRAP_CLAMP_TO_BORDER ||
147
wr == PIPE_TEX_WRAP_CLAMP_TO_BORDER)) {
148
if (i915->conformance_mode > 0) {
149
assert(0);
150
/* sampler->fallback = true; */
151
/* TODO */
152
}
153
}
154
}
155
#endif
156
157
/* See note at the top of file */
158
minlod = sampler->minlod;
159
lastlod = pt->last_level << 4;
160
161
if (lastlod < minlod) {
162
minlod = lastlod;
163
}
164
165
state[1] |= (sampler->minlod << SS3_MIN_LOD_SHIFT);
166
state[1] |= (unit << SS3_TEXTUREMAP_INDEX_SHIFT);
167
}
168
169
/***********************************************************************
170
* Sampler views
171
*/
172
173
static uint32_t
174
translate_texture_format(enum pipe_format pipeFormat,
175
const struct pipe_sampler_view *view)
176
{
177
if ((view->swizzle_r != PIPE_SWIZZLE_X ||
178
view->swizzle_g != PIPE_SWIZZLE_Y ||
179
view->swizzle_b != PIPE_SWIZZLE_Z ||
180
view->swizzle_a != PIPE_SWIZZLE_W) &&
181
pipeFormat != PIPE_FORMAT_Z24_UNORM_S8_UINT &&
182
pipeFormat != PIPE_FORMAT_Z24X8_UNORM)
183
debug_printf("i915: unsupported texture swizzle for format %d\n",
184
pipeFormat);
185
186
switch (pipeFormat) {
187
case PIPE_FORMAT_L8_UNORM:
188
return MAPSURF_8BIT | MT_8BIT_L8;
189
case PIPE_FORMAT_I8_UNORM:
190
return MAPSURF_8BIT | MT_8BIT_I8;
191
case PIPE_FORMAT_A8_UNORM:
192
return MAPSURF_8BIT | MT_8BIT_A8;
193
case PIPE_FORMAT_L8A8_UNORM:
194
return MAPSURF_16BIT | MT_16BIT_AY88;
195
case PIPE_FORMAT_B5G6R5_UNORM:
196
return MAPSURF_16BIT | MT_16BIT_RGB565;
197
case PIPE_FORMAT_B5G5R5A1_UNORM:
198
return MAPSURF_16BIT | MT_16BIT_ARGB1555;
199
case PIPE_FORMAT_B4G4R4A4_UNORM:
200
return MAPSURF_16BIT | MT_16BIT_ARGB4444;
201
case PIPE_FORMAT_B10G10R10A2_UNORM:
202
return MAPSURF_32BIT | MT_32BIT_ARGB2101010;
203
case PIPE_FORMAT_B8G8R8A8_UNORM:
204
case PIPE_FORMAT_B8G8R8A8_SRGB:
205
return MAPSURF_32BIT | MT_32BIT_ARGB8888;
206
case PIPE_FORMAT_B8G8R8X8_UNORM:
207
return MAPSURF_32BIT | MT_32BIT_XRGB8888;
208
case PIPE_FORMAT_R8G8B8A8_UNORM:
209
return MAPSURF_32BIT | MT_32BIT_ABGR8888;
210
case PIPE_FORMAT_R8G8B8X8_UNORM:
211
return MAPSURF_32BIT | MT_32BIT_XBGR8888;
212
case PIPE_FORMAT_YUYV:
213
return (MAPSURF_422 | MT_422_YCRCB_NORMAL);
214
case PIPE_FORMAT_UYVY:
215
return (MAPSURF_422 | MT_422_YCRCB_SWAPY);
216
#if 0
217
case PIPE_FORMAT_RGB_FXT1:
218
case PIPE_FORMAT_RGBA_FXT1:
219
return (MAPSURF_COMPRESSED | MT_COMPRESS_FXT1);
220
#endif
221
case PIPE_FORMAT_Z16_UNORM:
222
return (MAPSURF_16BIT | MT_16BIT_L16);
223
case PIPE_FORMAT_DXT1_RGBA:
224
case PIPE_FORMAT_DXT1_RGB:
225
return (MAPSURF_COMPRESSED | MT_COMPRESS_DXT1);
226
case PIPE_FORMAT_DXT3_RGBA:
227
return (MAPSURF_COMPRESSED | MT_COMPRESS_DXT2_3);
228
case PIPE_FORMAT_DXT5_RGBA:
229
return (MAPSURF_COMPRESSED | MT_COMPRESS_DXT4_5);
230
case PIPE_FORMAT_Z24_UNORM_S8_UINT:
231
case PIPE_FORMAT_Z24X8_UNORM: {
232
if (view->swizzle_r == PIPE_SWIZZLE_X &&
233
view->swizzle_g == PIPE_SWIZZLE_X &&
234
view->swizzle_b == PIPE_SWIZZLE_X &&
235
view->swizzle_a == PIPE_SWIZZLE_1)
236
return (MAPSURF_32BIT | MT_32BIT_xL824);
237
if (view->swizzle_r == PIPE_SWIZZLE_X &&
238
view->swizzle_g == PIPE_SWIZZLE_X &&
239
view->swizzle_b == PIPE_SWIZZLE_X &&
240
view->swizzle_a == PIPE_SWIZZLE_X)
241
return (MAPSURF_32BIT | MT_32BIT_xI824);
242
if (view->swizzle_r == PIPE_SWIZZLE_0 &&
243
view->swizzle_g == PIPE_SWIZZLE_0 &&
244
view->swizzle_b == PIPE_SWIZZLE_0 &&
245
view->swizzle_a == PIPE_SWIZZLE_X)
246
return (MAPSURF_32BIT | MT_32BIT_xA824);
247
debug_printf("i915: unsupported depth swizzle %d %d %d %d\n",
248
view->swizzle_r, view->swizzle_g, view->swizzle_b,
249
view->swizzle_a);
250
return (MAPSURF_32BIT | MT_32BIT_xL824);
251
}
252
default:
253
debug_printf("i915: translate_texture_format() bad image format %x\n",
254
pipeFormat);
255
assert(0);
256
return 0;
257
}
258
}
259
260
static inline uint32_t
261
ms3_tiling_bits(enum i915_winsys_buffer_tile tiling)
262
{
263
uint32_t tiling_bits = 0;
264
265
switch (tiling) {
266
case I915_TILE_Y:
267
tiling_bits |= MS3_TILE_WALK_Y;
268
FALLTHROUGH;
269
case I915_TILE_X:
270
tiling_bits |= MS3_TILED_SURFACE;
271
FALLTHROUGH;
272
case I915_TILE_NONE:
273
break;
274
}
275
276
return tiling_bits;
277
}
278
279
static void
280
update_map(struct i915_context *i915, uint32_t unit,
281
const struct i915_texture *tex,
282
const struct i915_sampler_state *sampler,
283
const struct pipe_sampler_view *view, uint32_t state[3])
284
{
285
const struct pipe_resource *pt = &tex->b;
286
uint32_t width = pt->width0, height = pt->height0, depth = pt->depth0;
287
int first_level = view->u.tex.first_level;
288
const uint32_t num_levels = pt->last_level - first_level;
289
unsigned max_lod = num_levels * 4;
290
bool is_npot = (!util_is_power_of_two_or_zero(pt->width0) ||
291
!util_is_power_of_two_or_zero(pt->height0));
292
uint32_t format, pitch;
293
294
/*
295
* This is a bit messy. i915 doesn't support NPOT with mipmaps, but we can
296
* still texture from a single level. This is useful to make u_blitter work.
297
*/
298
if (is_npot) {
299
width = u_minify(width, first_level);
300
height = u_minify(height, first_level);
301
max_lod = 1;
302
}
303
304
assert(tex);
305
assert(width);
306
assert(height);
307
assert(depth);
308
309
format = translate_texture_format(pt->format, view);
310
pitch = tex->stride;
311
312
assert(format);
313
assert(pitch);
314
315
/* MS3 state */
316
state[0] =
317
(((height - 1) << MS3_HEIGHT_SHIFT) | ((width - 1) << MS3_WIDTH_SHIFT) |
318
format | ms3_tiling_bits(tex->tiling));
319
320
/*
321
* XXX When min_filter != mag_filter and there's just one mipmap level,
322
* set max_lod = 1 to make sure i915 chooses between min/mag filtering.
323
*/
324
325
/* See note at the top of file */
326
if (max_lod > (sampler->maxlod >> 2))
327
max_lod = sampler->maxlod >> 2;
328
329
/* MS4 state */
330
state[1] = ((((pitch / 4) - 1) << MS4_PITCH_SHIFT) | MS4_CUBE_FACE_ENA_MASK |
331
((max_lod) << MS4_MAX_LOD_SHIFT) |
332
((depth - 1) << MS4_VOLUME_DEPTH_SHIFT));
333
334
if (is_npot)
335
state[2] = i915_texture_offset(tex, first_level, 0);
336
else
337
state[2] = 0;
338
}
339
340
static void
341
update_samplers(struct i915_context *i915)
342
{
343
uint32_t unit;
344
345
i915->current.sampler_enable_nr = 0;
346
i915->current.sampler_enable_flags = 0x0;
347
348
for (unit = 0;
349
unit < i915->num_fragment_sampler_views && unit < i915->num_samplers;
350
unit++) {
351
/* determine unit enable/disable by looking for a bound texture */
352
/* could also examine the fragment program? */
353
if (i915->fragment_sampler_views[unit]) {
354
struct i915_texture *texture =
355
i915_texture(i915->fragment_sampler_views[unit]->texture);
356
357
update_sampler(i915, unit,
358
i915->fragment_sampler[unit], /* sampler state */
359
texture, /* texture */
360
i915->current.sampler[unit]); /* the result */
361
update_map(i915, unit, texture, /* texture */
362
i915->fragment_sampler[unit], /* sampler state */
363
i915->fragment_sampler_views[unit], /* sampler view */
364
i915->current.texbuffer[unit]); /* the result */
365
366
i915->current.sampler_enable_nr++;
367
i915->current.sampler_enable_flags |= (1 << unit);
368
}
369
}
370
371
i915->hardware_dirty |= I915_HW_SAMPLER | I915_HW_MAP;
372
}
373
374
struct i915_tracked_state i915_hw_samplers = {
375
"samplers", update_samplers, I915_NEW_SAMPLER | I915_NEW_SAMPLER_VIEW};
376
377