Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mesa
Path: blob/21.2-virgl/src/gallium/drivers/lima/lima_state.c
4565 views
1
/*
2
* Copyright (c) 2011-2013 Luc Verhaegen <[email protected]>
3
* Copyright (c) 2017-2019 Lima Project
4
*
5
* Permission is hereby granted, free of charge, to any person obtaining a
6
* copy of this software and associated documentation files (the "Software"),
7
* to deal in the Software without restriction, including without limitation
8
* the rights to use, copy, modify, merge, publish, distribute, sub license,
9
* and/or sell copies of the Software, and to permit persons to whom the
10
* Software is furnished to do so, subject to the following conditions:
11
*
12
* The above copyright notice and this permission notice (including the
13
* next paragraph) shall be included in all copies or substantial portions
14
* of the Software.
15
*
16
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
19
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22
* DEALINGS IN THE SOFTWARE.
23
*
24
*/
25
26
#include "util/format/u_format.h"
27
#include "util/u_memory.h"
28
#include "util/u_inlines.h"
29
#include "util/u_helpers.h"
30
#include "util/u_debug.h"
31
#include "util/u_framebuffer.h"
32
33
#include "pipe/p_state.h"
34
35
#include "lima_screen.h"
36
#include "lima_context.h"
37
#include "lima_format.h"
38
#include "lima_resource.h"
39
40
static void
41
lima_set_framebuffer_state(struct pipe_context *pctx,
42
const struct pipe_framebuffer_state *framebuffer)
43
{
44
struct lima_context *ctx = lima_context(pctx);
45
46
/* make sure there are always single job in this context */
47
if (lima_debug & LIMA_DEBUG_SINGLE_JOB)
48
lima_flush(ctx);
49
50
struct lima_context_framebuffer *fb = &ctx->framebuffer;
51
52
util_copy_framebuffer_state(&fb->base, framebuffer);
53
54
ctx->job = NULL;
55
ctx->dirty |= LIMA_CONTEXT_DIRTY_FRAMEBUFFER;
56
}
57
58
static void
59
lima_set_polygon_stipple(struct pipe_context *pctx,
60
const struct pipe_poly_stipple *stipple)
61
{
62
63
}
64
65
static void *
66
lima_create_depth_stencil_alpha_state(struct pipe_context *pctx,
67
const struct pipe_depth_stencil_alpha_state *cso)
68
{
69
struct lima_depth_stencil_alpha_state *so;
70
71
so = CALLOC_STRUCT(lima_depth_stencil_alpha_state);
72
if (!so)
73
return NULL;
74
75
so->base = *cso;
76
77
return so;
78
}
79
80
static void
81
lima_bind_depth_stencil_alpha_state(struct pipe_context *pctx, void *hwcso)
82
{
83
struct lima_context *ctx = lima_context(pctx);
84
85
ctx->zsa = hwcso;
86
ctx->dirty |= LIMA_CONTEXT_DIRTY_ZSA;
87
}
88
89
static void
90
lima_delete_depth_stencil_alpha_state(struct pipe_context *pctx, void *hwcso)
91
{
92
FREE(hwcso);
93
}
94
95
static void *
96
lima_create_rasterizer_state(struct pipe_context *pctx,
97
const struct pipe_rasterizer_state *cso)
98
{
99
struct lima_rasterizer_state *so;
100
101
so = CALLOC_STRUCT(lima_rasterizer_state);
102
if (!so)
103
return NULL;
104
105
so->base = *cso;
106
107
return so;
108
}
109
110
static void
111
lima_bind_rasterizer_state(struct pipe_context *pctx, void *hwcso)
112
{
113
struct lima_context *ctx = lima_context(pctx);
114
115
ctx->rasterizer = hwcso;
116
ctx->dirty |= LIMA_CONTEXT_DIRTY_RASTERIZER;
117
}
118
119
static void
120
lima_delete_rasterizer_state(struct pipe_context *pctx, void *hwcso)
121
{
122
FREE(hwcso);
123
}
124
125
static void *
126
lima_create_blend_state(struct pipe_context *pctx,
127
const struct pipe_blend_state *cso)
128
{
129
struct lima_blend_state *so;
130
131
so = CALLOC_STRUCT(lima_blend_state);
132
if (!so)
133
return NULL;
134
135
so->base = *cso;
136
137
return so;
138
}
139
140
static void
141
lima_bind_blend_state(struct pipe_context *pctx, void *hwcso)
142
{
143
struct lima_context *ctx = lima_context(pctx);
144
145
ctx->blend = hwcso;
146
ctx->dirty |= LIMA_CONTEXT_DIRTY_BLEND;
147
}
148
149
static void
150
lima_delete_blend_state(struct pipe_context *pctx, void *hwcso)
151
{
152
FREE(hwcso);
153
}
154
155
static void *
156
lima_create_vertex_elements_state(struct pipe_context *pctx, unsigned num_elements,
157
const struct pipe_vertex_element *elements)
158
{
159
struct lima_vertex_element_state *so;
160
161
so = CALLOC_STRUCT(lima_vertex_element_state);
162
if (!so)
163
return NULL;
164
165
memcpy(so->pipe, elements, sizeof(*elements) * num_elements);
166
so->num_elements = num_elements;
167
168
return so;
169
}
170
171
static void
172
lima_bind_vertex_elements_state(struct pipe_context *pctx, void *hwcso)
173
{
174
struct lima_context *ctx = lima_context(pctx);
175
176
ctx->vertex_elements = hwcso;
177
ctx->dirty |= LIMA_CONTEXT_DIRTY_VERTEX_ELEM;
178
}
179
180
static void
181
lima_delete_vertex_elements_state(struct pipe_context *pctx, void *hwcso)
182
{
183
FREE(hwcso);
184
}
185
186
static void
187
lima_set_vertex_buffers(struct pipe_context *pctx,
188
unsigned start_slot, unsigned count,
189
unsigned unbind_num_trailing_slots,
190
bool take_ownership,
191
const struct pipe_vertex_buffer *vb)
192
{
193
struct lima_context *ctx = lima_context(pctx);
194
struct lima_context_vertex_buffer *so = &ctx->vertex_buffers;
195
196
util_set_vertex_buffers_mask(so->vb, &so->enabled_mask,
197
vb, start_slot, count,
198
unbind_num_trailing_slots,
199
take_ownership);
200
so->count = util_last_bit(so->enabled_mask);
201
202
ctx->dirty |= LIMA_CONTEXT_DIRTY_VERTEX_BUFF;
203
}
204
205
static void
206
lima_set_viewport_states(struct pipe_context *pctx,
207
unsigned start_slot,
208
unsigned num_viewports,
209
const struct pipe_viewport_state *viewport)
210
{
211
struct lima_context *ctx = lima_context(pctx);
212
213
/* reverse calculate the parameter of glViewport */
214
ctx->viewport.left = viewport->translate[0] - fabsf(viewport->scale[0]);
215
ctx->viewport.right = viewport->translate[0] + fabsf(viewport->scale[0]);
216
ctx->viewport.bottom = viewport->translate[1] - fabsf(viewport->scale[1]);
217
ctx->viewport.top = viewport->translate[1] + fabsf(viewport->scale[1]);
218
219
/* reverse calculate the parameter of glDepthRange */
220
float near, far;
221
near = viewport->translate[2] - viewport->scale[2];
222
far = viewport->translate[2] + viewport->scale[2];
223
224
ctx->viewport.near = MIN2(near, far);
225
ctx->viewport.far = MAX2(near, far);
226
227
ctx->viewport.transform = *viewport;
228
ctx->dirty |= LIMA_CONTEXT_DIRTY_VIEWPORT;
229
}
230
231
static void
232
lima_set_scissor_states(struct pipe_context *pctx,
233
unsigned start_slot,
234
unsigned num_scissors,
235
const struct pipe_scissor_state *scissor)
236
{
237
struct lima_context *ctx = lima_context(pctx);
238
239
ctx->scissor = *scissor;
240
ctx->dirty |= LIMA_CONTEXT_DIRTY_SCISSOR;
241
}
242
243
static void
244
lima_set_blend_color(struct pipe_context *pctx,
245
const struct pipe_blend_color *blend_color)
246
{
247
struct lima_context *ctx = lima_context(pctx);
248
249
ctx->blend_color = *blend_color;
250
ctx->dirty |= LIMA_CONTEXT_DIRTY_BLEND_COLOR;
251
}
252
253
static void
254
lima_set_stencil_ref(struct pipe_context *pctx,
255
const struct pipe_stencil_ref stencil_ref)
256
{
257
struct lima_context *ctx = lima_context(pctx);
258
259
ctx->stencil_ref = stencil_ref;
260
ctx->dirty |= LIMA_CONTEXT_DIRTY_STENCIL_REF;
261
}
262
263
static void
264
lima_set_clip_state(struct pipe_context *pctx,
265
const struct pipe_clip_state *clip)
266
{
267
struct lima_context *ctx = lima_context(pctx);
268
ctx->clip = *clip;
269
270
ctx->dirty |= LIMA_CONTEXT_DIRTY_CLIP;
271
}
272
273
static void
274
lima_set_constant_buffer(struct pipe_context *pctx,
275
enum pipe_shader_type shader, uint index,
276
bool pass_reference,
277
const struct pipe_constant_buffer *cb)
278
{
279
struct lima_context *ctx = lima_context(pctx);
280
struct lima_context_constant_buffer *so = ctx->const_buffer + shader;
281
282
assert(index == 0);
283
284
if (unlikely(!cb)) {
285
so->buffer = NULL;
286
so->size = 0;
287
} else {
288
assert(!cb->buffer);
289
290
so->buffer = cb->user_buffer + cb->buffer_offset;
291
so->size = cb->buffer_size;
292
}
293
294
so->dirty = true;
295
ctx->dirty |= LIMA_CONTEXT_DIRTY_CONST_BUFF;
296
297
}
298
299
static void *
300
lima_create_sampler_state(struct pipe_context *pctx,
301
const struct pipe_sampler_state *cso)
302
{
303
struct lima_sampler_state *so = CALLOC_STRUCT(lima_sampler_state);
304
if (!so)
305
return NULL;
306
307
memcpy(so, cso, sizeof(*cso));
308
309
return so;
310
}
311
312
static void
313
lima_sampler_state_delete(struct pipe_context *pctx, void *sstate)
314
{
315
free(sstate);
316
}
317
318
static void
319
lima_sampler_states_bind(struct pipe_context *pctx,
320
enum pipe_shader_type shader, unsigned start,
321
unsigned nr, void **hwcso)
322
{
323
struct lima_context *ctx = lima_context(pctx);
324
struct lima_texture_stateobj *lima_tex = &ctx->tex_stateobj;
325
unsigned i;
326
unsigned new_nr = 0;
327
328
assert(start == 0);
329
330
for (i = 0; i < nr; i++) {
331
if (hwcso[i])
332
new_nr = i + 1;
333
lima_tex->samplers[i] = hwcso[i];
334
}
335
336
for (; i < lima_tex->num_samplers; i++) {
337
lima_tex->samplers[i] = NULL;
338
}
339
340
lima_tex->num_samplers = new_nr;
341
ctx->dirty |= LIMA_CONTEXT_DIRTY_TEXTURES;
342
}
343
344
static struct pipe_sampler_view *
345
lima_create_sampler_view(struct pipe_context *pctx, struct pipe_resource *prsc,
346
const struct pipe_sampler_view *cso)
347
{
348
struct lima_sampler_view *so = CALLOC_STRUCT(lima_sampler_view);
349
350
if (!so)
351
return NULL;
352
353
so->base = *cso;
354
355
pipe_reference(NULL, &prsc->reference);
356
so->base.texture = prsc;
357
so->base.reference.count = 1;
358
so->base.context = pctx;
359
360
uint8_t sampler_swizzle[4] = { cso->swizzle_r, cso->swizzle_g,
361
cso->swizzle_b, cso->swizzle_a };
362
const uint8_t *format_swizzle = lima_format_get_texel_swizzle(cso->format);
363
util_format_compose_swizzles(format_swizzle, sampler_swizzle, so->swizzle);
364
365
return &so->base;
366
}
367
368
static void
369
lima_sampler_view_destroy(struct pipe_context *pctx,
370
struct pipe_sampler_view *pview)
371
{
372
struct lima_sampler_view *view = lima_sampler_view(pview);
373
374
pipe_resource_reference(&pview->texture, NULL);
375
376
free(view);
377
}
378
379
static void
380
lima_set_sampler_views(struct pipe_context *pctx,
381
enum pipe_shader_type shader,
382
unsigned start, unsigned nr,
383
unsigned unbind_num_trailing_slots,
384
struct pipe_sampler_view **views)
385
{
386
struct lima_context *ctx = lima_context(pctx);
387
struct lima_texture_stateobj *lima_tex = &ctx->tex_stateobj;
388
int i;
389
unsigned new_nr = 0;
390
391
assert(start == 0);
392
393
for (i = 0; i < nr; i++) {
394
if (views[i])
395
new_nr = i + 1;
396
pipe_sampler_view_reference(&lima_tex->textures[i], views[i]);
397
}
398
399
for (; i < lima_tex->num_textures; i++) {
400
pipe_sampler_view_reference(&lima_tex->textures[i], NULL);
401
}
402
403
lima_tex->num_textures = new_nr;
404
ctx->dirty |= LIMA_CONTEXT_DIRTY_TEXTURES;
405
}
406
407
static void
408
lima_set_sample_mask(struct pipe_context *pctx,
409
unsigned sample_mask)
410
{
411
}
412
413
void
414
lima_state_init(struct lima_context *ctx)
415
{
416
ctx->base.set_framebuffer_state = lima_set_framebuffer_state;
417
ctx->base.set_polygon_stipple = lima_set_polygon_stipple;
418
ctx->base.set_viewport_states = lima_set_viewport_states;
419
ctx->base.set_scissor_states = lima_set_scissor_states;
420
ctx->base.set_blend_color = lima_set_blend_color;
421
ctx->base.set_stencil_ref = lima_set_stencil_ref;
422
ctx->base.set_clip_state = lima_set_clip_state;
423
424
ctx->base.set_vertex_buffers = lima_set_vertex_buffers;
425
ctx->base.set_constant_buffer = lima_set_constant_buffer;
426
427
ctx->base.create_depth_stencil_alpha_state = lima_create_depth_stencil_alpha_state;
428
ctx->base.bind_depth_stencil_alpha_state = lima_bind_depth_stencil_alpha_state;
429
ctx->base.delete_depth_stencil_alpha_state = lima_delete_depth_stencil_alpha_state;
430
431
ctx->base.create_rasterizer_state = lima_create_rasterizer_state;
432
ctx->base.bind_rasterizer_state = lima_bind_rasterizer_state;
433
ctx->base.delete_rasterizer_state = lima_delete_rasterizer_state;
434
435
ctx->base.create_blend_state = lima_create_blend_state;
436
ctx->base.bind_blend_state = lima_bind_blend_state;
437
ctx->base.delete_blend_state = lima_delete_blend_state;
438
439
ctx->base.create_vertex_elements_state = lima_create_vertex_elements_state;
440
ctx->base.bind_vertex_elements_state = lima_bind_vertex_elements_state;
441
ctx->base.delete_vertex_elements_state = lima_delete_vertex_elements_state;
442
443
ctx->base.create_sampler_state = lima_create_sampler_state;
444
ctx->base.delete_sampler_state = lima_sampler_state_delete;
445
ctx->base.bind_sampler_states = lima_sampler_states_bind;
446
447
ctx->base.create_sampler_view = lima_create_sampler_view;
448
ctx->base.sampler_view_destroy = lima_sampler_view_destroy;
449
ctx->base.set_sampler_views = lima_set_sampler_views;
450
451
ctx->base.set_sample_mask = lima_set_sample_mask;
452
}
453
454
void
455
lima_state_fini(struct lima_context *ctx)
456
{
457
struct lima_context_vertex_buffer *so = &ctx->vertex_buffers;
458
459
util_set_vertex_buffers_mask(so->vb, &so->enabled_mask, NULL,
460
0, 0, ARRAY_SIZE(so->vb), false);
461
462
pipe_surface_reference(&ctx->framebuffer.base.cbufs[0], NULL);
463
pipe_surface_reference(&ctx->framebuffer.base.zsbuf, NULL);
464
}
465
466