Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mesa
Path: blob/21.2-virgl/src/gallium/auxiliary/draw/draw_pipe_pstipple.c
4565 views
1
/**************************************************************************
2
*
3
* Copyright 2008 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
/**
29
* Polygon stipple stage: implement polygon stipple with texture map and
30
* fragment program. The fragment program samples the texture using the
31
* fragment window coordinate register and does a fragment kill for the
32
* stipple-failing fragments.
33
*
34
* Authors: Brian Paul
35
*/
36
37
38
#include "pipe/p_context.h"
39
#include "pipe/p_defines.h"
40
#include "pipe/p_shader_tokens.h"
41
#include "util/u_inlines.h"
42
43
#include "util/format/u_format.h"
44
#include "util/u_math.h"
45
#include "util/u_memory.h"
46
#include "util/u_pstipple.h"
47
#include "util/u_sampler.h"
48
49
#include "tgsi/tgsi_transform.h"
50
51
#include "draw_context.h"
52
#include "draw_pipe.h"
53
54
#include "nir.h"
55
#include "nir/nir_draw_helpers.h"
56
57
/** Approx number of new tokens for instructions in pstip_transform_inst() */
58
#define NUM_NEW_TOKENS 53
59
60
61
/**
62
* Subclass of pipe_shader_state to carry extra fragment shader info.
63
*/
64
struct pstip_fragment_shader
65
{
66
struct pipe_shader_state state;
67
void *driver_fs;
68
void *pstip_fs;
69
uint sampler_unit;
70
};
71
72
73
/**
74
* Subclass of draw_stage
75
*/
76
struct pstip_stage
77
{
78
struct draw_stage stage;
79
80
void *sampler_cso;
81
struct pipe_resource *texture;
82
struct pipe_sampler_view *sampler_view;
83
uint num_samplers;
84
uint num_sampler_views;
85
86
/*
87
* Currently bound state
88
*/
89
struct pstip_fragment_shader *fs;
90
struct {
91
void *samplers[PIPE_MAX_SAMPLERS];
92
struct pipe_sampler_view *sampler_views[PIPE_MAX_SHADER_SAMPLER_VIEWS];
93
const struct pipe_poly_stipple *stipple;
94
} state;
95
96
/*
97
* Driver interface/override functions
98
*/
99
void * (*driver_create_fs_state)(struct pipe_context *,
100
const struct pipe_shader_state *);
101
void (*driver_bind_fs_state)(struct pipe_context *, void *);
102
void (*driver_delete_fs_state)(struct pipe_context *, void *);
103
104
void (*driver_bind_sampler_states)(struct pipe_context *,
105
enum pipe_shader_type,
106
unsigned, unsigned, void **);
107
108
void (*driver_set_sampler_views)(struct pipe_context *,
109
enum pipe_shader_type shader,
110
unsigned start, unsigned count,
111
unsigned unbind_num_trailing_slots,
112
struct pipe_sampler_view **);
113
114
void (*driver_set_polygon_stipple)(struct pipe_context *,
115
const struct pipe_poly_stipple *);
116
117
struct pipe_context *pipe;
118
};
119
120
121
/**
122
* Generate the frag shader we'll use for doing polygon stipple.
123
* This will be the user's shader prefixed with a TEX and KIL instruction.
124
*/
125
static boolean
126
generate_pstip_fs(struct pstip_stage *pstip)
127
{
128
struct pipe_context *pipe = pstip->pipe;
129
struct pipe_screen *screen = pipe->screen;
130
const struct pipe_shader_state *orig_fs = &pstip->fs->state;
131
/*struct draw_context *draw = pstip->stage.draw;*/
132
struct pipe_shader_state pstip_fs;
133
enum tgsi_file_type wincoord_file;
134
135
wincoord_file = screen->get_param(screen, PIPE_CAP_TGSI_FS_POSITION_IS_SYSVAL) ?
136
TGSI_FILE_SYSTEM_VALUE : TGSI_FILE_INPUT;
137
138
pstip_fs = *orig_fs; /* copy to init */
139
if (orig_fs->type == PIPE_SHADER_IR_TGSI) {
140
pstip_fs.tokens = util_pstipple_create_fragment_shader(orig_fs->tokens,
141
&pstip->fs->sampler_unit,
142
0,
143
wincoord_file);
144
if (pstip_fs.tokens == NULL)
145
return FALSE;
146
} else {
147
pstip_fs.ir.nir = nir_shader_clone(NULL, orig_fs->ir.nir);
148
nir_lower_pstipple_fs(pstip_fs.ir.nir,
149
&pstip->fs->sampler_unit, 0, wincoord_file == TGSI_FILE_SYSTEM_VALUE);
150
}
151
152
assert(pstip->fs->sampler_unit < PIPE_MAX_SAMPLERS);
153
154
pstip->fs->pstip_fs = pstip->driver_create_fs_state(pipe, &pstip_fs);
155
156
FREE((void *)pstip_fs.tokens);
157
158
if (!pstip->fs->pstip_fs)
159
return FALSE;
160
161
return TRUE;
162
}
163
164
165
/**
166
* When we're about to draw our first stipple polygon in a batch, this function
167
* is called to tell the driver to bind our modified fragment shader.
168
*/
169
static boolean
170
bind_pstip_fragment_shader(struct pstip_stage *pstip)
171
{
172
struct draw_context *draw = pstip->stage.draw;
173
if (!pstip->fs->pstip_fs &&
174
!generate_pstip_fs(pstip))
175
return FALSE;
176
177
draw->suspend_flushing = TRUE;
178
pstip->driver_bind_fs_state(pstip->pipe, pstip->fs->pstip_fs);
179
draw->suspend_flushing = FALSE;
180
return TRUE;
181
}
182
183
184
static inline struct pstip_stage *
185
pstip_stage( struct draw_stage *stage )
186
{
187
return (struct pstip_stage *) stage;
188
}
189
190
191
static void
192
pstip_first_tri(struct draw_stage *stage, struct prim_header *header)
193
{
194
struct pstip_stage *pstip = pstip_stage(stage);
195
struct pipe_context *pipe = pstip->pipe;
196
struct draw_context *draw = stage->draw;
197
uint num_samplers;
198
uint num_sampler_views;
199
200
assert(stage->draw->rasterizer->poly_stipple_enable);
201
202
/* bind our fragprog */
203
if (!bind_pstip_fragment_shader(pstip)) {
204
stage->tri = draw_pipe_passthrough_tri;
205
stage->tri(stage, header);
206
return;
207
}
208
209
/* how many samplers? */
210
/* we'll use sampler/texture[pstip->sampler_unit] for the stipple */
211
num_samplers = MAX2(pstip->num_samplers, pstip->fs->sampler_unit + 1);
212
num_sampler_views = MAX2(pstip->num_sampler_views, num_samplers);
213
214
/* plug in our sampler, texture */
215
pstip->state.samplers[pstip->fs->sampler_unit] = pstip->sampler_cso;
216
pipe_sampler_view_reference(&pstip->state.sampler_views[pstip->fs->sampler_unit],
217
pstip->sampler_view);
218
219
assert(num_samplers <= PIPE_MAX_SAMPLERS);
220
221
draw->suspend_flushing = TRUE;
222
223
pstip->driver_bind_sampler_states(pipe, PIPE_SHADER_FRAGMENT, 0,
224
num_samplers, pstip->state.samplers);
225
226
pstip->driver_set_sampler_views(pipe, PIPE_SHADER_FRAGMENT, 0,
227
num_sampler_views, 0, pstip->state.sampler_views);
228
229
draw->suspend_flushing = FALSE;
230
231
/* now really draw first triangle */
232
stage->tri = draw_pipe_passthrough_tri;
233
stage->tri(stage, header);
234
}
235
236
237
static void
238
pstip_flush(struct draw_stage *stage, unsigned flags)
239
{
240
struct draw_context *draw = stage->draw;
241
struct pstip_stage *pstip = pstip_stage(stage);
242
struct pipe_context *pipe = pstip->pipe;
243
244
stage->tri = pstip_first_tri;
245
stage->next->flush( stage->next, flags );
246
247
/* restore original frag shader, texture, sampler state */
248
draw->suspend_flushing = TRUE;
249
pstip->driver_bind_fs_state(pipe, pstip->fs ? pstip->fs->driver_fs : NULL);
250
251
pstip->driver_bind_sampler_states(pipe, PIPE_SHADER_FRAGMENT, 0,
252
pstip->num_samplers,
253
pstip->state.samplers);
254
255
pstip->driver_set_sampler_views(pipe, PIPE_SHADER_FRAGMENT, 0,
256
pstip->num_sampler_views, 0,
257
pstip->state.sampler_views);
258
259
draw->suspend_flushing = FALSE;
260
}
261
262
263
static void
264
pstip_reset_stipple_counter(struct draw_stage *stage)
265
{
266
stage->next->reset_stipple_counter( stage->next );
267
}
268
269
270
static void
271
pstip_destroy(struct draw_stage *stage)
272
{
273
struct pstip_stage *pstip = pstip_stage(stage);
274
uint i;
275
276
for (i = 0; i < PIPE_MAX_SHADER_SAMPLER_VIEWS; i++) {
277
pipe_sampler_view_reference(&pstip->state.sampler_views[i], NULL);
278
}
279
280
pstip->pipe->delete_sampler_state(pstip->pipe, pstip->sampler_cso);
281
282
pipe_resource_reference(&pstip->texture, NULL);
283
284
if (pstip->sampler_view) {
285
pipe_sampler_view_reference(&pstip->sampler_view, NULL);
286
}
287
288
draw_free_temp_verts( stage );
289
FREE( stage );
290
}
291
292
293
/** Create a new polygon stipple drawing stage object */
294
static struct pstip_stage *
295
draw_pstip_stage(struct draw_context *draw, struct pipe_context *pipe)
296
{
297
struct pstip_stage *pstip = CALLOC_STRUCT(pstip_stage);
298
if (!pstip)
299
goto fail;
300
301
pstip->pipe = pipe;
302
303
pstip->stage.draw = draw;
304
pstip->stage.name = "pstip";
305
pstip->stage.next = NULL;
306
pstip->stage.point = draw_pipe_passthrough_point;
307
pstip->stage.line = draw_pipe_passthrough_line;
308
pstip->stage.tri = pstip_first_tri;
309
pstip->stage.flush = pstip_flush;
310
pstip->stage.reset_stipple_counter = pstip_reset_stipple_counter;
311
pstip->stage.destroy = pstip_destroy;
312
313
if (!draw_alloc_temp_verts( &pstip->stage, 8 ))
314
goto fail;
315
316
return pstip;
317
318
fail:
319
if (pstip)
320
pstip->stage.destroy( &pstip->stage );
321
322
return NULL;
323
}
324
325
326
static struct pstip_stage *
327
pstip_stage_from_pipe(struct pipe_context *pipe)
328
{
329
struct draw_context *draw = (struct draw_context *) pipe->draw;
330
return pstip_stage(draw->pipeline.pstipple);
331
}
332
333
334
/**
335
* This function overrides the driver's create_fs_state() function and
336
* will typically be called by the gallium frontend.
337
*/
338
static void *
339
pstip_create_fs_state(struct pipe_context *pipe,
340
const struct pipe_shader_state *fs)
341
{
342
struct pstip_stage *pstip = pstip_stage_from_pipe(pipe);
343
struct pstip_fragment_shader *pstipfs = CALLOC_STRUCT(pstip_fragment_shader);
344
345
if (pstipfs) {
346
pstipfs->state.type = fs->type;
347
if (fs->type == PIPE_SHADER_IR_TGSI)
348
pstipfs->state.tokens = tgsi_dup_tokens(fs->tokens);
349
else
350
pstipfs->state.ir.nir = nir_shader_clone(NULL, fs->ir.nir);
351
352
/* pass-through */
353
pstipfs->driver_fs = pstip->driver_create_fs_state(pstip->pipe, fs);
354
}
355
356
return pstipfs;
357
}
358
359
360
static void
361
pstip_bind_fs_state(struct pipe_context *pipe, void *fs)
362
{
363
struct pstip_stage *pstip = pstip_stage_from_pipe(pipe);
364
struct pstip_fragment_shader *pstipfs = (struct pstip_fragment_shader *) fs;
365
/* save current */
366
pstip->fs = pstipfs;
367
/* pass-through */
368
pstip->driver_bind_fs_state(pstip->pipe,
369
(pstipfs ? pstipfs->driver_fs : NULL));
370
}
371
372
373
static void
374
pstip_delete_fs_state(struct pipe_context *pipe, void *fs)
375
{
376
struct pstip_stage *pstip = pstip_stage_from_pipe(pipe);
377
struct pstip_fragment_shader *pstipfs = (struct pstip_fragment_shader *) fs;
378
/* pass-through */
379
pstip->driver_delete_fs_state(pstip->pipe, pstipfs->driver_fs);
380
381
if (pstipfs->pstip_fs)
382
pstip->driver_delete_fs_state(pstip->pipe, pstipfs->pstip_fs);
383
384
if (pstipfs->state.type == PIPE_SHADER_IR_TGSI)
385
FREE((void*)pstipfs->state.tokens);
386
else
387
ralloc_free(pstipfs->state.ir.nir);
388
FREE(pstipfs);
389
}
390
391
392
static void
393
pstip_bind_sampler_states(struct pipe_context *pipe,
394
enum pipe_shader_type shader,
395
unsigned start, unsigned num, void **sampler)
396
{
397
struct pstip_stage *pstip = pstip_stage_from_pipe(pipe);
398
uint i;
399
400
assert(start == 0);
401
402
if (shader == PIPE_SHADER_FRAGMENT) {
403
/* save current */
404
memcpy(pstip->state.samplers, sampler, num * sizeof(void *));
405
for (i = num; i < PIPE_MAX_SAMPLERS; i++) {
406
pstip->state.samplers[i] = NULL;
407
}
408
pstip->num_samplers = num;
409
}
410
411
/* pass-through */
412
pstip->driver_bind_sampler_states(pstip->pipe, shader, start, num, sampler);
413
}
414
415
416
static void
417
pstip_set_sampler_views(struct pipe_context *pipe,
418
enum pipe_shader_type shader,
419
unsigned start, unsigned num,
420
unsigned unbind_num_trailing_slots,
421
struct pipe_sampler_view **views)
422
{
423
struct pstip_stage *pstip = pstip_stage_from_pipe(pipe);
424
uint i;
425
426
if (shader == PIPE_SHADER_FRAGMENT) {
427
/* save current */
428
for (i = 0; i < num; i++) {
429
pipe_sampler_view_reference(&pstip->state.sampler_views[start + i],
430
views[i]);
431
}
432
for (; i < num + unbind_num_trailing_slots; i++) {
433
pipe_sampler_view_reference(&pstip->state.sampler_views[start + i],
434
NULL);
435
}
436
pstip->num_sampler_views = num;
437
}
438
439
/* pass-through */
440
pstip->driver_set_sampler_views(pstip->pipe, shader, start, num,
441
unbind_num_trailing_slots, views);
442
}
443
444
445
static void
446
pstip_set_polygon_stipple(struct pipe_context *pipe,
447
const struct pipe_poly_stipple *stipple)
448
{
449
struct pstip_stage *pstip = pstip_stage_from_pipe(pipe);
450
451
/* save current */
452
pstip->state.stipple = stipple;
453
454
/* pass-through */
455
pstip->driver_set_polygon_stipple(pstip->pipe, stipple);
456
457
util_pstipple_update_stipple_texture(pstip->pipe, pstip->texture,
458
pstip->state.stipple->stipple);
459
}
460
461
462
/**
463
* Called by drivers that want to install this polygon stipple stage
464
* into the draw module's pipeline. This will not be used if the
465
* hardware has native support for polygon stipple.
466
*/
467
boolean
468
draw_install_pstipple_stage(struct draw_context *draw,
469
struct pipe_context *pipe)
470
{
471
struct pstip_stage *pstip;
472
473
pipe->draw = (void *) draw;
474
475
/*
476
* Create / install pgon stipple drawing / prim stage
477
*/
478
pstip = draw_pstip_stage( draw, pipe );
479
if (!pstip)
480
goto fail;
481
482
draw->pipeline.pstipple = &pstip->stage;
483
484
/* save original driver functions */
485
pstip->driver_create_fs_state = pipe->create_fs_state;
486
pstip->driver_bind_fs_state = pipe->bind_fs_state;
487
pstip->driver_delete_fs_state = pipe->delete_fs_state;
488
489
pstip->driver_bind_sampler_states = pipe->bind_sampler_states;
490
pstip->driver_set_sampler_views = pipe->set_sampler_views;
491
pstip->driver_set_polygon_stipple = pipe->set_polygon_stipple;
492
493
/* create special texture, sampler state */
494
pstip->texture = util_pstipple_create_stipple_texture(pipe, NULL);
495
if (!pstip->texture)
496
goto fail;
497
498
pstip->sampler_view = util_pstipple_create_sampler_view(pipe,
499
pstip->texture);
500
if (!pstip->sampler_view)
501
goto fail;
502
503
pstip->sampler_cso = util_pstipple_create_sampler(pipe);
504
if (!pstip->sampler_cso)
505
goto fail;
506
507
/* override the driver's functions */
508
pipe->create_fs_state = pstip_create_fs_state;
509
pipe->bind_fs_state = pstip_bind_fs_state;
510
pipe->delete_fs_state = pstip_delete_fs_state;
511
512
pipe->bind_sampler_states = pstip_bind_sampler_states;
513
pipe->set_sampler_views = pstip_set_sampler_views;
514
pipe->set_polygon_stipple = pstip_set_polygon_stipple;
515
516
return TRUE;
517
518
fail:
519
if (pstip)
520
pstip->stage.destroy( &pstip->stage );
521
522
return FALSE;
523
}
524
525