Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mesa
Path: blob/21.2-virgl/src/gallium/drivers/softpipe/sp_state_sampler.c
4570 views
1
/**************************************************************************
2
*
3
* Copyright 2007 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
/* Authors:
29
* Brian Paul
30
*/
31
32
#include "util/u_memory.h"
33
#include "util/u_inlines.h"
34
#include "util/format/u_format.h"
35
36
#include "draw/draw_context.h"
37
38
#include "sp_context.h"
39
#include "sp_state.h"
40
#include "sp_texture.h"
41
#include "sp_tex_sample.h"
42
#include "sp_tex_tile_cache.h"
43
#include "sp_screen.h"
44
#include "frontend/sw_winsys.h"
45
46
47
/**
48
* Bind a range [start, start+num-1] of samplers for a shader stage.
49
*/
50
static void
51
softpipe_bind_sampler_states(struct pipe_context *pipe,
52
enum pipe_shader_type shader,
53
unsigned start,
54
unsigned num,
55
void **samplers)
56
{
57
struct softpipe_context *softpipe = softpipe_context(pipe);
58
unsigned i;
59
60
assert(shader < PIPE_SHADER_TYPES);
61
assert(start + num <= ARRAY_SIZE(softpipe->samplers[shader]));
62
63
draw_flush(softpipe->draw);
64
65
/* set the new samplers */
66
for (i = 0; i < num; i++) {
67
softpipe->samplers[shader][start + i] = samplers[i];
68
}
69
70
/* find highest non-null samplers[] entry */
71
{
72
unsigned j = MAX2(softpipe->num_samplers[shader], start + num);
73
while (j > 0 && softpipe->samplers[shader][j - 1] == NULL)
74
j--;
75
softpipe->num_samplers[shader] = j;
76
}
77
78
if (shader == PIPE_SHADER_VERTEX || shader == PIPE_SHADER_GEOMETRY) {
79
draw_set_samplers(softpipe->draw,
80
shader,
81
softpipe->samplers[shader],
82
softpipe->num_samplers[shader]);
83
}
84
85
softpipe->dirty |= SP_NEW_SAMPLER;
86
}
87
88
89
static void
90
softpipe_sampler_view_destroy(struct pipe_context *pipe,
91
struct pipe_sampler_view *view)
92
{
93
pipe_resource_reference(&view->texture, NULL);
94
FREE(view);
95
}
96
97
98
void
99
softpipe_set_sampler_views(struct pipe_context *pipe,
100
enum pipe_shader_type shader,
101
unsigned start,
102
unsigned num,
103
unsigned unbind_num_trailing_slots,
104
struct pipe_sampler_view **views)
105
{
106
struct softpipe_context *softpipe = softpipe_context(pipe);
107
uint i;
108
109
assert(shader < PIPE_SHADER_TYPES);
110
assert(start + num <= ARRAY_SIZE(softpipe->sampler_views[shader]));
111
112
draw_flush(softpipe->draw);
113
114
/* set the new sampler views */
115
for (i = 0; i < num; i++) {
116
struct sp_sampler_view *sp_sviewsrc;
117
struct sp_sampler_view *sp_sviewdst =
118
&softpipe->tgsi.sampler[shader]->sp_sview[start + i];
119
struct pipe_sampler_view **pview = &softpipe->sampler_views[shader][start + i];
120
pipe_sampler_view_reference(pview, views[i]);
121
sp_tex_tile_cache_set_sampler_view(softpipe->tex_cache[shader][start + i],
122
views[i]);
123
/*
124
* We don't really have variants, however some bits are different per shader,
125
* so just copy?
126
*/
127
sp_sviewsrc = (struct sp_sampler_view *)*pview;
128
if (sp_sviewsrc) {
129
memcpy(sp_sviewdst, sp_sviewsrc, sizeof(*sp_sviewsrc));
130
sp_sviewdst->compute_lambda = softpipe_get_lambda_func(&sp_sviewdst->base, shader);
131
sp_sviewdst->compute_lambda_from_grad = softpipe_get_lambda_from_grad_func(&sp_sviewdst->base, shader);
132
sp_sviewdst->cache = softpipe->tex_cache[shader][start + i];
133
}
134
else {
135
memset(sp_sviewdst, 0, sizeof(*sp_sviewsrc));
136
}
137
}
138
for (; i < num + unbind_num_trailing_slots; i++) {
139
struct pipe_sampler_view **pview = &softpipe->sampler_views[shader][start + i];
140
pipe_sampler_view_reference(pview, NULL);
141
sp_tex_tile_cache_set_sampler_view(softpipe->tex_cache[shader][start + i],
142
NULL);
143
}
144
145
146
/* find highest non-null sampler_views[] entry */
147
{
148
unsigned j = MAX2(softpipe->num_sampler_views[shader], start + num);
149
while (j > 0 && softpipe->sampler_views[shader][j - 1] == NULL)
150
j--;
151
softpipe->num_sampler_views[shader] = j;
152
}
153
154
if (shader == PIPE_SHADER_VERTEX || shader == PIPE_SHADER_GEOMETRY) {
155
draw_set_sampler_views(softpipe->draw,
156
shader,
157
softpipe->sampler_views[shader],
158
softpipe->num_sampler_views[shader]);
159
}
160
161
softpipe->dirty |= SP_NEW_TEXTURE;
162
}
163
164
165
static void
166
softpipe_delete_sampler_state(struct pipe_context *pipe,
167
void *sampler)
168
{
169
FREE( sampler );
170
}
171
172
173
static void
174
prepare_shader_sampling(
175
struct softpipe_context *sp,
176
unsigned num,
177
struct pipe_sampler_view **views,
178
enum pipe_shader_type shader_type,
179
struct pipe_resource *mapped_tex[PIPE_MAX_SHADER_SAMPLER_VIEWS])
180
{
181
182
unsigned i;
183
uint32_t row_stride[PIPE_MAX_TEXTURE_LEVELS];
184
uint32_t img_stride[PIPE_MAX_TEXTURE_LEVELS];
185
uint32_t mip_offsets[PIPE_MAX_TEXTURE_LEVELS];
186
const void *addr;
187
188
assert(num <= PIPE_MAX_SHADER_SAMPLER_VIEWS);
189
if (!num)
190
return;
191
192
for (i = 0; i < num; i++) {
193
struct pipe_sampler_view *view = views[i];
194
195
if (view) {
196
struct pipe_resource *tex = view->texture;
197
struct softpipe_resource *sp_tex = softpipe_resource(tex);
198
unsigned width0 = tex->width0;
199
unsigned num_layers = tex->depth0;
200
unsigned first_level = 0;
201
unsigned last_level = 0;
202
203
/* We're referencing the texture's internal data, so save a
204
* reference to it.
205
*/
206
pipe_resource_reference(&mapped_tex[i], tex);
207
208
if (!sp_tex->dt) {
209
/* regular texture - setup array of mipmap level offsets */
210
ASSERTED struct pipe_resource *res = view->texture;
211
int j;
212
213
if (view->target != PIPE_BUFFER) {
214
first_level = view->u.tex.first_level;
215
last_level = view->u.tex.last_level;
216
assert(first_level <= last_level);
217
assert(last_level <= res->last_level);
218
addr = sp_tex->data;
219
220
for (j = first_level; j <= last_level; j++) {
221
mip_offsets[j] = sp_tex->level_offset[j];
222
row_stride[j] = sp_tex->stride[j];
223
img_stride[j] = sp_tex->img_stride[j];
224
}
225
if (tex->target == PIPE_TEXTURE_1D_ARRAY ||
226
tex->target == PIPE_TEXTURE_2D_ARRAY ||
227
tex->target == PIPE_TEXTURE_CUBE ||
228
tex->target == PIPE_TEXTURE_CUBE_ARRAY) {
229
num_layers = view->u.tex.last_layer - view->u.tex.first_layer + 1;
230
for (j = first_level; j <= last_level; j++) {
231
mip_offsets[j] += view->u.tex.first_layer *
232
sp_tex->img_stride[j];
233
}
234
if (view->target == PIPE_TEXTURE_CUBE ||
235
view->target == PIPE_TEXTURE_CUBE_ARRAY) {
236
assert(num_layers % 6 == 0);
237
}
238
assert(view->u.tex.first_layer <= view->u.tex.last_layer);
239
assert(view->u.tex.last_layer < res->array_size);
240
}
241
}
242
else {
243
unsigned view_blocksize = util_format_get_blocksize(view->format);
244
addr = sp_tex->data;
245
/* probably don't really need to fill that out */
246
mip_offsets[0] = 0;
247
row_stride[0] = 0;
248
img_stride[0] = 0;
249
250
/* everything specified in number of elements here. */
251
width0 = view->u.buf.size / view_blocksize;
252
addr = (uint8_t *)addr + view->u.buf.offset;
253
assert(view->u.buf.offset + view->u.buf.size <= res->width0);
254
}
255
}
256
else {
257
/* display target texture/surface */
258
struct softpipe_screen *screen = softpipe_screen(tex->screen);
259
struct sw_winsys *winsys = screen->winsys;
260
addr = winsys->displaytarget_map(winsys, sp_tex->dt,
261
PIPE_MAP_READ);
262
row_stride[0] = sp_tex->stride[0];
263
img_stride[0] = sp_tex->img_stride[0];
264
mip_offsets[0] = 0;
265
assert(addr);
266
}
267
draw_set_mapped_texture(sp->draw,
268
shader_type,
269
i,
270
width0, tex->height0, num_layers,
271
first_level, last_level, 0, 0,
272
addr,
273
row_stride, img_stride, mip_offsets);
274
}
275
}
276
}
277
278
static void
279
sp_sampler_view_display_target_unmap(struct softpipe_context *sp,
280
struct pipe_sampler_view *view)
281
{
282
if (view) {
283
struct pipe_resource *tex = view->texture;
284
struct softpipe_resource *sp_tex = softpipe_resource(tex);
285
if (sp_tex->dt) {
286
struct softpipe_screen *screen = softpipe_screen(tex->screen);
287
struct sw_winsys *winsys = screen->winsys;
288
winsys->displaytarget_unmap(winsys, sp_tex->dt);
289
}
290
}
291
}
292
293
/**
294
* Called during state validation when SP_NEW_TEXTURE is set.
295
*/
296
void
297
softpipe_prepare_vertex_sampling(struct softpipe_context *sp,
298
unsigned num,
299
struct pipe_sampler_view **views)
300
{
301
prepare_shader_sampling(sp, num, views, PIPE_SHADER_VERTEX,
302
sp->mapped_vs_tex);
303
}
304
305
void
306
softpipe_cleanup_vertex_sampling(struct softpipe_context *ctx)
307
{
308
unsigned i;
309
for (i = 0; i < ARRAY_SIZE(ctx->mapped_vs_tex); i++) {
310
sp_sampler_view_display_target_unmap(
311
ctx, ctx->sampler_views[PIPE_SHADER_VERTEX][i]);
312
pipe_resource_reference(&ctx->mapped_vs_tex[i], NULL);
313
}
314
}
315
316
317
/**
318
* Called during state validation when SP_NEW_TEXTURE is set.
319
*/
320
void
321
softpipe_prepare_geometry_sampling(struct softpipe_context *sp,
322
unsigned num,
323
struct pipe_sampler_view **views)
324
{
325
prepare_shader_sampling(sp, num, views, PIPE_SHADER_GEOMETRY,
326
sp->mapped_gs_tex);
327
}
328
329
void
330
softpipe_cleanup_geometry_sampling(struct softpipe_context *ctx)
331
{
332
unsigned i;
333
for (i = 0; i < ARRAY_SIZE(ctx->mapped_gs_tex); i++) {
334
sp_sampler_view_display_target_unmap(
335
ctx, ctx->sampler_views[PIPE_SHADER_GEOMETRY][i]);
336
pipe_resource_reference(&ctx->mapped_gs_tex[i], NULL);
337
}
338
}
339
340
341
void
342
softpipe_init_sampler_funcs(struct pipe_context *pipe)
343
{
344
pipe->create_sampler_state = softpipe_create_sampler_state;
345
pipe->bind_sampler_states = softpipe_bind_sampler_states;
346
pipe->delete_sampler_state = softpipe_delete_sampler_state;
347
348
pipe->create_sampler_view = softpipe_create_sampler_view;
349
pipe->set_sampler_views = softpipe_set_sampler_views;
350
pipe->sampler_view_destroy = softpipe_sampler_view_destroy;
351
}
352
353
354