Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mesa
Path: blob/21.2-virgl/src/gallium/drivers/swr/swr_state.cpp
4570 views
1
/****************************************************************************
2
* Copyright (C) 2015 Intel Corporation. All Rights Reserved.
3
*
4
* Permission is hereby granted, free of charge, to any person obtaining a
5
* copy of this software and associated documentation files (the "Software"),
6
* to deal in the Software without restriction, including without limitation
7
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
8
* and/or sell copies of the Software, and to permit persons to whom the
9
* Software is furnished to do so, subject to the following conditions:
10
*
11
* The above copyright notice and this permission notice (including the next
12
* paragraph) shall be included in all copies or substantial portions of the
13
* Software.
14
*
15
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21
* IN THE SOFTWARE.
22
***************************************************************************/
23
24
#include <llvm/Config/llvm-config.h>
25
26
#if LLVM_VERSION_MAJOR < 7
27
// llvm redefines DEBUG
28
#pragma push_macro("DEBUG")
29
#undef DEBUG
30
#endif
31
32
#include <rasterizer/core/state.h>
33
#include "JitManager.h"
34
35
#if LLVM_VERSION_MAJOR < 7
36
#pragma pop_macro("DEBUG")
37
#endif
38
39
#include "common/os.h"
40
#include "jit_api.h"
41
#include "gen_state_llvm.h"
42
#include "core/multisample.h"
43
#include "core/state_funcs.h"
44
45
#include "gallivm/lp_bld_tgsi.h"
46
#include "util/format/u_format.h"
47
48
#include "util/u_memory.h"
49
#include "util/u_inlines.h"
50
#include "util/u_helpers.h"
51
#include "util/u_framebuffer.h"
52
#include "util/u_viewport.h"
53
#include "util/u_prim.h"
54
55
#include "swr_state.h"
56
#include "swr_context.h"
57
#include "gen_surf_state_llvm.h"
58
#include "gen_swr_context_llvm.h"
59
#include "swr_screen.h"
60
#include "swr_resource.h"
61
#include "swr_tex_sample.h"
62
#include "swr_scratch.h"
63
#include "swr_shader.h"
64
#include "swr_fence.h"
65
66
/* These should be pulled out into separate files as necessary
67
* Just initializing everything here to get going. */
68
69
static void *
70
swr_create_blend_state(struct pipe_context *pipe,
71
const struct pipe_blend_state *blend)
72
{
73
struct swr_blend_state *state = CALLOC_STRUCT(swr_blend_state);
74
assert(state != nullptr);
75
76
memcpy(&state->pipe, blend, sizeof(*blend));
77
78
struct pipe_blend_state *pipe_blend = &state->pipe;
79
80
for (int target = 0;
81
target < std::min(SWR_NUM_RENDERTARGETS, PIPE_MAX_COLOR_BUFS);
82
target++) {
83
84
struct pipe_rt_blend_state *rt_blend = &pipe_blend->rt[target];
85
SWR_RENDER_TARGET_BLEND_STATE &blendState =
86
state->blendState.renderTarget[target];
87
RENDER_TARGET_BLEND_COMPILE_STATE &compileState =
88
state->compileState[target];
89
90
if (target != 0 && !pipe_blend->independent_blend_enable) {
91
memcpy(&compileState,
92
&state->compileState[0],
93
sizeof(RENDER_TARGET_BLEND_COMPILE_STATE));
94
continue;
95
}
96
97
compileState.blendEnable = rt_blend->blend_enable;
98
if (compileState.blendEnable) {
99
compileState.sourceAlphaBlendFactor =
100
swr_convert_blend_factor(rt_blend->alpha_src_factor);
101
compileState.destAlphaBlendFactor =
102
swr_convert_blend_factor(rt_blend->alpha_dst_factor);
103
compileState.sourceBlendFactor =
104
swr_convert_blend_factor(rt_blend->rgb_src_factor);
105
compileState.destBlendFactor =
106
swr_convert_blend_factor(rt_blend->rgb_dst_factor);
107
108
compileState.colorBlendFunc =
109
swr_convert_blend_func(rt_blend->rgb_func);
110
compileState.alphaBlendFunc =
111
swr_convert_blend_func(rt_blend->alpha_func);
112
}
113
compileState.logicOpEnable = state->pipe.logicop_enable;
114
if (compileState.logicOpEnable) {
115
compileState.logicOpFunc =
116
swr_convert_logic_op(state->pipe.logicop_func);
117
}
118
119
blendState.writeDisableRed =
120
(rt_blend->colormask & PIPE_MASK_R) ? 0 : 1;
121
blendState.writeDisableGreen =
122
(rt_blend->colormask & PIPE_MASK_G) ? 0 : 1;
123
blendState.writeDisableBlue =
124
(rt_blend->colormask & PIPE_MASK_B) ? 0 : 1;
125
blendState.writeDisableAlpha =
126
(rt_blend->colormask & PIPE_MASK_A) ? 0 : 1;
127
128
if (rt_blend->colormask == 0)
129
compileState.blendEnable = false;
130
}
131
132
return state;
133
}
134
135
static void
136
swr_bind_blend_state(struct pipe_context *pipe, void *blend)
137
{
138
struct swr_context *ctx = swr_context(pipe);
139
140
if (ctx->blend == blend)
141
return;
142
143
ctx->blend = (swr_blend_state *)blend;
144
145
ctx->dirty |= SWR_NEW_BLEND;
146
}
147
148
static void
149
swr_delete_blend_state(struct pipe_context *pipe, void *blend)
150
{
151
FREE(blend);
152
}
153
154
static void
155
swr_set_blend_color(struct pipe_context *pipe,
156
const struct pipe_blend_color *color)
157
{
158
struct swr_context *ctx = swr_context(pipe);
159
160
ctx->blend_color = *color;
161
162
ctx->dirty |= SWR_NEW_BLEND;
163
}
164
165
static void
166
swr_set_stencil_ref(struct pipe_context *pipe,
167
const struct pipe_stencil_ref ref)
168
{
169
struct swr_context *ctx = swr_context(pipe);
170
171
ctx->stencil_ref = ref;
172
173
ctx->dirty |= SWR_NEW_DEPTH_STENCIL_ALPHA;
174
}
175
176
static void *
177
swr_create_depth_stencil_state(
178
struct pipe_context *pipe,
179
const struct pipe_depth_stencil_alpha_state *depth_stencil)
180
{
181
struct pipe_depth_stencil_alpha_state *state;
182
183
state = (pipe_depth_stencil_alpha_state *)mem_dup(depth_stencil,
184
sizeof *depth_stencil);
185
186
return state;
187
}
188
189
static void
190
swr_bind_depth_stencil_state(struct pipe_context *pipe, void *depth_stencil)
191
{
192
struct swr_context *ctx = swr_context(pipe);
193
194
if (ctx->depth_stencil == (pipe_depth_stencil_alpha_state *)depth_stencil)
195
return;
196
197
ctx->depth_stencil = (pipe_depth_stencil_alpha_state *)depth_stencil;
198
199
ctx->dirty |= SWR_NEW_DEPTH_STENCIL_ALPHA;
200
}
201
202
static void
203
swr_delete_depth_stencil_state(struct pipe_context *pipe, void *depth)
204
{
205
FREE(depth);
206
}
207
208
209
static void *
210
swr_create_rasterizer_state(struct pipe_context *pipe,
211
const struct pipe_rasterizer_state *rast)
212
{
213
struct pipe_rasterizer_state *state;
214
state = (pipe_rasterizer_state *)mem_dup(rast, sizeof *rast);
215
216
return state;
217
}
218
219
static void
220
swr_bind_rasterizer_state(struct pipe_context *pipe, void *handle)
221
{
222
struct swr_context *ctx = swr_context(pipe);
223
const struct pipe_rasterizer_state *rasterizer =
224
(const struct pipe_rasterizer_state *)handle;
225
226
if (ctx->rasterizer == (pipe_rasterizer_state *)rasterizer)
227
return;
228
229
ctx->rasterizer = (pipe_rasterizer_state *)rasterizer;
230
231
ctx->dirty |= SWR_NEW_RASTERIZER;
232
}
233
234
static void
235
swr_delete_rasterizer_state(struct pipe_context *pipe, void *rasterizer)
236
{
237
FREE(rasterizer);
238
}
239
240
241
static void *
242
swr_create_sampler_state(struct pipe_context *pipe,
243
const struct pipe_sampler_state *sampler)
244
{
245
struct pipe_sampler_state *state =
246
(pipe_sampler_state *)mem_dup(sampler, sizeof *sampler);
247
248
return state;
249
}
250
251
static void
252
swr_bind_sampler_states(struct pipe_context *pipe,
253
enum pipe_shader_type shader,
254
unsigned start,
255
unsigned num,
256
void **samplers)
257
{
258
struct swr_context *ctx = swr_context(pipe);
259
unsigned i;
260
261
assert(shader < PIPE_SHADER_TYPES);
262
assert(start + num <= ARRAY_SIZE(ctx->samplers[shader]));
263
264
/* set the new samplers */
265
ctx->num_samplers[shader] = num;
266
for (i = 0; i < num; i++) {
267
ctx->samplers[shader][start + i] = (pipe_sampler_state *)samplers[i];
268
}
269
270
ctx->dirty |= SWR_NEW_SAMPLER;
271
}
272
273
static void
274
swr_delete_sampler_state(struct pipe_context *pipe, void *sampler)
275
{
276
FREE(sampler);
277
}
278
279
280
static struct pipe_sampler_view *
281
swr_create_sampler_view(struct pipe_context *pipe,
282
struct pipe_resource *texture,
283
const struct pipe_sampler_view *templ)
284
{
285
struct pipe_sampler_view *view = CALLOC_STRUCT(pipe_sampler_view);
286
287
if (view) {
288
*view = *templ;
289
view->reference.count = 1;
290
view->texture = NULL;
291
pipe_resource_reference(&view->texture, texture);
292
view->context = pipe;
293
}
294
295
return view;
296
}
297
298
static void
299
swr_set_sampler_views(struct pipe_context *pipe,
300
enum pipe_shader_type shader,
301
unsigned start,
302
unsigned num,
303
unsigned unbind_num_trailing_slots,
304
struct pipe_sampler_view **views)
305
{
306
struct swr_context *ctx = swr_context(pipe);
307
uint i;
308
309
assert(num <= PIPE_MAX_SHADER_SAMPLER_VIEWS);
310
311
assert(shader < PIPE_SHADER_TYPES);
312
assert(start + num <= ARRAY_SIZE(ctx->sampler_views[shader]));
313
314
/* set the new sampler views */
315
ctx->num_sampler_views[shader] = num;
316
for (i = 0; i < num; i++) {
317
pipe_sampler_view_reference(&ctx->sampler_views[shader][start + i],
318
views[i]);
319
}
320
for (; i < num + unbind_num_trailing_slots; i++) {
321
pipe_sampler_view_reference(&ctx->sampler_views[shader][start + i],
322
NULL);
323
}
324
325
ctx->dirty |= SWR_NEW_SAMPLER_VIEW;
326
}
327
328
static void
329
swr_sampler_view_destroy(struct pipe_context *pipe,
330
struct pipe_sampler_view *view)
331
{
332
pipe_resource_reference(&view->texture, NULL);
333
FREE(view);
334
}
335
336
static void *
337
swr_create_vs_state(struct pipe_context *pipe,
338
const struct pipe_shader_state *vs)
339
{
340
struct swr_vertex_shader *swr_vs = new swr_vertex_shader;
341
if (!swr_vs)
342
return NULL;
343
344
swr_vs->pipe.tokens = tgsi_dup_tokens(vs->tokens);
345
swr_vs->pipe.stream_output = vs->stream_output;
346
347
lp_build_tgsi_info(vs->tokens, &swr_vs->info);
348
349
swr_vs->soState = {0};
350
351
if (swr_vs->pipe.stream_output.num_outputs) {
352
pipe_stream_output_info *stream_output = &swr_vs->pipe.stream_output;
353
354
swr_vs->soState.soEnable = true;
355
// soState.rasterizerDisable set on state dirty
356
// soState.streamToRasterizer not used
357
358
for (uint32_t i = 0; i < stream_output->num_outputs; i++) {
359
unsigned attrib_slot = stream_output->output[i].register_index;
360
attrib_slot = swr_so_adjust_attrib(attrib_slot, swr_vs);
361
swr_vs->soState.streamMasks[stream_output->output[i].stream] |=
362
(1 << attrib_slot);
363
}
364
for (uint32_t i = 0; i < MAX_SO_STREAMS; i++) {
365
swr_vs->soState.streamNumEntries[i] =
366
_mm_popcnt_u32(swr_vs->soState.streamMasks[i]);
367
}
368
}
369
370
return swr_vs;
371
}
372
373
static void
374
swr_bind_vs_state(struct pipe_context *pipe, void *vs)
375
{
376
struct swr_context *ctx = swr_context(pipe);
377
378
if (ctx->vs == vs)
379
return;
380
381
ctx->vs = (swr_vertex_shader *)vs;
382
ctx->dirty |= SWR_NEW_VS;
383
}
384
385
static void
386
swr_delete_vs_state(struct pipe_context *pipe, void *vs)
387
{
388
struct swr_vertex_shader *swr_vs = (swr_vertex_shader *)vs;
389
FREE((void *)swr_vs->pipe.tokens);
390
struct swr_screen *screen = swr_screen(pipe->screen);
391
392
/* Defer deletion of vs state */
393
swr_fence_work_delete_vs(screen->flush_fence, swr_vs);
394
}
395
396
static void *
397
swr_create_fs_state(struct pipe_context *pipe,
398
const struct pipe_shader_state *fs)
399
{
400
struct swr_fragment_shader *swr_fs = new swr_fragment_shader;
401
if (!swr_fs)
402
return NULL;
403
404
swr_fs->pipe.tokens = tgsi_dup_tokens(fs->tokens);
405
406
lp_build_tgsi_info(fs->tokens, &swr_fs->info);
407
408
return swr_fs;
409
}
410
411
412
static void
413
swr_bind_fs_state(struct pipe_context *pipe, void *fs)
414
{
415
struct swr_context *ctx = swr_context(pipe);
416
417
if (ctx->fs == fs)
418
return;
419
420
ctx->fs = (swr_fragment_shader *)fs;
421
ctx->dirty |= SWR_NEW_FS;
422
}
423
424
static void
425
swr_delete_fs_state(struct pipe_context *pipe, void *fs)
426
{
427
struct swr_fragment_shader *swr_fs = (swr_fragment_shader *)fs;
428
FREE((void *)swr_fs->pipe.tokens);
429
struct swr_screen *screen = swr_screen(pipe->screen);
430
431
/* Defer deleton of fs state */
432
swr_fence_work_delete_fs(screen->flush_fence, swr_fs);
433
}
434
435
static void *
436
swr_create_gs_state(struct pipe_context *pipe,
437
const struct pipe_shader_state *gs)
438
{
439
struct swr_geometry_shader *swr_gs = new swr_geometry_shader;
440
if (!swr_gs)
441
return NULL;
442
443
swr_gs->pipe.tokens = tgsi_dup_tokens(gs->tokens);
444
lp_build_tgsi_info(gs->tokens, &swr_gs->info);
445
return swr_gs;
446
}
447
448
static void
449
swr_bind_gs_state(struct pipe_context *pipe, void *gs)
450
{
451
struct swr_context *ctx = swr_context(pipe);
452
453
if (ctx->gs == gs)
454
return;
455
456
ctx->gs = (swr_geometry_shader *)gs;
457
ctx->dirty |= SWR_NEW_GS;
458
}
459
460
static void
461
swr_delete_gs_state(struct pipe_context *pipe, void *gs)
462
{
463
struct swr_geometry_shader *swr_gs = (swr_geometry_shader *)gs;
464
FREE((void *)swr_gs->pipe.tokens);
465
struct swr_screen *screen = swr_screen(pipe->screen);
466
467
/* Defer deleton of fs state */
468
swr_fence_work_delete_gs(screen->flush_fence, swr_gs);
469
}
470
471
static void *
472
swr_create_tcs_state(struct pipe_context *pipe,
473
const struct pipe_shader_state *tcs)
474
{
475
struct swr_tess_control_shader *swr_tcs = new swr_tess_control_shader;
476
if (!swr_tcs)
477
return NULL;
478
479
swr_tcs->pipe.tokens = tgsi_dup_tokens(tcs->tokens);
480
lp_build_tgsi_info(tcs->tokens, &swr_tcs->info);
481
return swr_tcs;
482
}
483
484
static void
485
swr_bind_tcs_state(struct pipe_context *pipe, void *tcs)
486
{
487
struct swr_context *ctx = swr_context(pipe);
488
489
if (ctx->tcs == tcs)
490
return;
491
492
ctx->tcs = (swr_tess_control_shader *)tcs;
493
ctx->dirty |= SWR_NEW_TCS;
494
ctx->dirty |= SWR_NEW_TS;
495
}
496
497
static void
498
swr_delete_tcs_state(struct pipe_context *pipe, void *tcs)
499
{
500
struct swr_tess_control_shader *swr_tcs = (swr_tess_control_shader *)tcs;
501
FREE((void *)swr_tcs->pipe.tokens);
502
struct swr_screen *screen = swr_screen(pipe->screen);
503
504
/* Defer deleton of tcs state */
505
swr_fence_work_delete_tcs(screen->flush_fence, swr_tcs);
506
}
507
508
static void *
509
swr_create_tes_state(struct pipe_context *pipe,
510
const struct pipe_shader_state *tes)
511
{
512
struct swr_tess_evaluation_shader *swr_tes = new swr_tess_evaluation_shader;
513
if (!swr_tes)
514
return NULL;
515
516
swr_tes->pipe.tokens = tgsi_dup_tokens(tes->tokens);
517
lp_build_tgsi_info(tes->tokens, &swr_tes->info);
518
return swr_tes;
519
}
520
521
static void
522
swr_bind_tes_state(struct pipe_context *pipe, void *tes)
523
{
524
struct swr_context *ctx = swr_context(pipe);
525
526
if (ctx->tes == tes)
527
return;
528
529
// Save current tessellator state first
530
if (ctx->tes != nullptr) {
531
ctx->tes->ts_state = ctx->tsState;
532
}
533
534
ctx->tes = (swr_tess_evaluation_shader *)tes;
535
536
ctx->dirty |= SWR_NEW_TES;
537
ctx->dirty |= SWR_NEW_TS;
538
}
539
540
static void
541
swr_delete_tes_state(struct pipe_context *pipe, void *tes)
542
{
543
struct swr_tess_evaluation_shader *swr_tes = (swr_tess_evaluation_shader *)tes;
544
FREE((void *)swr_tes->pipe.tokens);
545
struct swr_screen *screen = swr_screen(pipe->screen);
546
547
/* Defer deleton of tes state */
548
swr_fence_work_delete_tes(screen->flush_fence, swr_tes);
549
}
550
551
static void
552
swr_set_constant_buffer(struct pipe_context *pipe,
553
enum pipe_shader_type shader,
554
uint index, bool take_ownership,
555
const struct pipe_constant_buffer *cb)
556
{
557
struct swr_context *ctx = swr_context(pipe);
558
struct pipe_resource *constants = cb ? cb->buffer : NULL;
559
560
assert(shader < PIPE_SHADER_TYPES);
561
assert(index < ARRAY_SIZE(ctx->constants[shader]));
562
563
/* note: reference counting */
564
util_copy_constant_buffer(&ctx->constants[shader][index], cb, take_ownership);
565
566
if (shader == PIPE_SHADER_VERTEX) {
567
ctx->dirty |= SWR_NEW_VSCONSTANTS;
568
} else if (shader == PIPE_SHADER_FRAGMENT) {
569
ctx->dirty |= SWR_NEW_FSCONSTANTS;
570
} else if (shader == PIPE_SHADER_GEOMETRY) {
571
ctx->dirty |= SWR_NEW_GSCONSTANTS;
572
} else if (shader == PIPE_SHADER_TESS_CTRL) {
573
ctx->dirty |= SWR_NEW_TCSCONSTANTS;
574
} else if (shader == PIPE_SHADER_TESS_EVAL) {
575
ctx->dirty |= SWR_NEW_TESCONSTANTS;
576
}
577
if (cb && cb->user_buffer) {
578
pipe_resource_reference(&constants, NULL);
579
}
580
}
581
582
583
static void *
584
swr_create_vertex_elements_state(struct pipe_context *pipe,
585
unsigned num_elements,
586
const struct pipe_vertex_element *attribs)
587
{
588
struct swr_vertex_element_state *velems;
589
assert(num_elements <= PIPE_MAX_ATTRIBS);
590
velems = new swr_vertex_element_state;
591
if (velems) {
592
memset((void*)&velems->fsState, 0, sizeof(velems->fsState));
593
velems->fsState.bVertexIDOffsetEnable = true;
594
velems->fsState.numAttribs = num_elements;
595
for (unsigned i = 0; i < num_elements; i++) {
596
// XXX: we should do this keyed on the VS usage info
597
598
const struct util_format_description *desc =
599
util_format_description(attribs[i].src_format);
600
601
velems->fsState.layout[i].AlignedByteOffset = attribs[i].src_offset;
602
velems->fsState.layout[i].Format =
603
mesa_to_swr_format(attribs[i].src_format);
604
velems->fsState.layout[i].StreamIndex =
605
attribs[i].vertex_buffer_index;
606
velems->fsState.layout[i].InstanceEnable =
607
attribs[i].instance_divisor != 0;
608
velems->fsState.layout[i].ComponentControl0 =
609
desc->channel[0].type != UTIL_FORMAT_TYPE_VOID
610
? ComponentControl::StoreSrc
611
: ComponentControl::Store0;
612
velems->fsState.layout[i].ComponentControl1 =
613
desc->channel[1].type != UTIL_FORMAT_TYPE_VOID
614
? ComponentControl::StoreSrc
615
: ComponentControl::Store0;
616
velems->fsState.layout[i].ComponentControl2 =
617
desc->channel[2].type != UTIL_FORMAT_TYPE_VOID
618
? ComponentControl::StoreSrc
619
: ComponentControl::Store0;
620
velems->fsState.layout[i].ComponentControl3 =
621
desc->channel[3].type != UTIL_FORMAT_TYPE_VOID
622
? ComponentControl::StoreSrc
623
: ComponentControl::Store1Fp;
624
velems->fsState.layout[i].ComponentPacking = ComponentEnable::XYZW;
625
velems->fsState.layout[i].InstanceAdvancementState =
626
attribs[i].instance_divisor;
627
628
/* Calculate the pitch of each stream */
629
const SWR_FORMAT_INFO &swr_desc = GetFormatInfo(
630
mesa_to_swr_format(attribs[i].src_format));
631
velems->stream_pitch[attribs[i].vertex_buffer_index] += swr_desc.Bpp;
632
633
if (attribs[i].instance_divisor != 0) {
634
velems->instanced_bufs |= 1U << attribs[i].vertex_buffer_index;
635
uint32_t *min_instance_div =
636
&velems->min_instance_div[attribs[i].vertex_buffer_index];
637
if (!*min_instance_div ||
638
attribs[i].instance_divisor < *min_instance_div)
639
*min_instance_div = attribs[i].instance_divisor;
640
}
641
}
642
}
643
644
return velems;
645
}
646
647
static void
648
swr_bind_vertex_elements_state(struct pipe_context *pipe, void *velems)
649
{
650
struct swr_context *ctx = swr_context(pipe);
651
struct swr_vertex_element_state *swr_velems =
652
(struct swr_vertex_element_state *)velems;
653
654
ctx->velems = swr_velems;
655
ctx->dirty |= SWR_NEW_VERTEX;
656
}
657
658
static void
659
swr_delete_vertex_elements_state(struct pipe_context *pipe, void *velems)
660
{
661
struct swr_vertex_element_state *swr_velems =
662
(struct swr_vertex_element_state *) velems;
663
/* XXX Need to destroy fetch shader? */
664
delete swr_velems;
665
}
666
667
668
static void
669
swr_set_vertex_buffers(struct pipe_context *pipe,
670
unsigned start_slot,
671
unsigned num_elements,
672
unsigned unbind_num_trailing_slots,
673
bool take_ownership,
674
const struct pipe_vertex_buffer *buffers)
675
{
676
struct swr_context *ctx = swr_context(pipe);
677
678
assert(num_elements <= PIPE_MAX_ATTRIBS);
679
680
util_set_vertex_buffers_count(ctx->vertex_buffer,
681
&ctx->num_vertex_buffers,
682
buffers,
683
start_slot,
684
num_elements,
685
unbind_num_trailing_slots,
686
take_ownership);
687
688
ctx->dirty |= SWR_NEW_VERTEX;
689
}
690
691
692
static void
693
swr_set_polygon_stipple(struct pipe_context *pipe,
694
const struct pipe_poly_stipple *stipple)
695
{
696
struct swr_context *ctx = swr_context(pipe);
697
698
ctx->poly_stipple.pipe = *stipple; /* struct copy */
699
ctx->dirty |= SWR_NEW_STIPPLE;
700
}
701
702
static void
703
swr_set_clip_state(struct pipe_context *pipe,
704
const struct pipe_clip_state *clip)
705
{
706
struct swr_context *ctx = swr_context(pipe);
707
708
ctx->clip = *clip;
709
/* XXX Unimplemented, but prevents crash */
710
711
ctx->dirty |= SWR_NEW_CLIP;
712
}
713
714
715
static void
716
swr_set_scissor_states(struct pipe_context *pipe,
717
unsigned start_slot,
718
unsigned num_scissors,
719
const struct pipe_scissor_state *scissors)
720
{
721
struct swr_context *ctx = swr_context(pipe);
722
723
memcpy(ctx->scissors + start_slot, scissors,
724
sizeof(struct pipe_scissor_state) * num_scissors);
725
726
for (unsigned i = 0; i < num_scissors; i++) {
727
auto idx = start_slot + i;
728
ctx->swr_scissors[idx].xmin = scissors[idx].minx;
729
ctx->swr_scissors[idx].xmax = scissors[idx].maxx;
730
ctx->swr_scissors[idx].ymin = scissors[idx].miny;
731
ctx->swr_scissors[idx].ymax = scissors[idx].maxy;
732
}
733
ctx->dirty |= SWR_NEW_SCISSOR;
734
}
735
736
static void
737
swr_set_viewport_states(struct pipe_context *pipe,
738
unsigned start_slot,
739
unsigned num_viewports,
740
const struct pipe_viewport_state *vpt)
741
{
742
struct swr_context *ctx = swr_context(pipe);
743
744
memcpy(ctx->viewports + start_slot, vpt, sizeof(struct pipe_viewport_state) * num_viewports);
745
ctx->dirty |= SWR_NEW_VIEWPORT;
746
}
747
748
749
static void
750
swr_set_framebuffer_state(struct pipe_context *pipe,
751
const struct pipe_framebuffer_state *fb)
752
{
753
struct swr_context *ctx = swr_context(pipe);
754
755
bool changed = !util_framebuffer_state_equal(&ctx->framebuffer, fb);
756
757
assert(fb->width <= KNOB_GUARDBAND_WIDTH);
758
assert(fb->height <= KNOB_GUARDBAND_HEIGHT);
759
760
if (changed) {
761
util_copy_framebuffer_state(&ctx->framebuffer, fb);
762
763
/* 0 and 1 both indicate no msaa. Core doesn't understand 0 samples */
764
ctx->framebuffer.samples = std::max((ubyte)1, ctx->framebuffer.samples);
765
766
ctx->dirty |= SWR_NEW_FRAMEBUFFER;
767
}
768
}
769
770
771
static void
772
swr_set_sample_mask(struct pipe_context *pipe, unsigned sample_mask)
773
{
774
struct swr_context *ctx = swr_context(pipe);
775
776
if (sample_mask != ctx->sample_mask) {
777
ctx->sample_mask = sample_mask;
778
ctx->dirty |= SWR_NEW_RASTERIZER;
779
}
780
}
781
782
/*
783
* MSAA fixed sample position table
784
* used by update_derived and get_sample_position
785
* (integer locations on a 16x16 grid)
786
*/
787
static const uint8_t swr_sample_positions[][2] =
788
{ /* 1x*/ { 8, 8},
789
/* 2x*/ {12,12},{ 4, 4},
790
/* 4x*/ { 6, 2},{14, 6},{ 2,10},{10,14},
791
/* 8x*/ { 9, 5},{ 7,11},{13, 9},{ 5, 3},
792
{ 3,13},{ 1, 7},{11,15},{15, 1},
793
/*16x*/ { 9, 9},{ 7, 5},{ 5,10},{12, 7},
794
{ 3, 6},{10,13},{13,11},{11, 3},
795
{ 6,14},{ 8, 1},{ 4, 2},{ 2,12},
796
{ 0, 8},{15, 4},{14,15},{ 1, 0} };
797
798
static void
799
swr_get_sample_position(struct pipe_context *pipe,
800
unsigned sample_count, unsigned sample_index,
801
float *out_value)
802
{
803
/* validate sample_count */
804
sample_count = GetNumSamples(GetSampleCount(sample_count));
805
806
const uint8_t *sample = swr_sample_positions[sample_count-1 + sample_index];
807
out_value[0] = sample[0] / 16.0f;
808
out_value[1] = sample[1] / 16.0f;
809
}
810
811
812
/*
813
* Update resource in-use status
814
* All resources bound to color or depth targets marked as WRITE resources.
815
* VBO Vertex/index buffers and texture views marked as READ resources.
816
*/
817
void
818
swr_update_resource_status(struct pipe_context *pipe,
819
const struct pipe_draw_info *p_draw_info)
820
{
821
struct swr_context *ctx = swr_context(pipe);
822
struct pipe_framebuffer_state *fb = &ctx->framebuffer;
823
824
/* colorbuffer targets */
825
if (fb->nr_cbufs)
826
for (uint32_t i = 0; i < fb->nr_cbufs; ++i)
827
if (fb->cbufs[i])
828
swr_resource_write(fb->cbufs[i]->texture);
829
830
/* depth/stencil target */
831
if (fb->zsbuf)
832
swr_resource_write(fb->zsbuf->texture);
833
834
/* VBO vertex buffers */
835
for (uint32_t i = 0; i < ctx->num_vertex_buffers; i++) {
836
struct pipe_vertex_buffer *vb = &ctx->vertex_buffer[i];
837
if (!vb->is_user_buffer && vb->buffer.resource)
838
swr_resource_read(vb->buffer.resource);
839
}
840
841
/* VBO index buffer */
842
if (p_draw_info && p_draw_info->index_size) {
843
if (!p_draw_info->has_user_indices)
844
swr_resource_read(p_draw_info->index.resource);
845
}
846
847
/* transform feedback buffers */
848
for (uint32_t i = 0; i < ctx->num_so_targets; i++) {
849
struct pipe_stream_output_target *target = ctx->so_targets[i];
850
if (target && target->buffer)
851
swr_resource_write(target->buffer);
852
}
853
854
/* texture sampler views */
855
for (uint32_t j : {PIPE_SHADER_VERTEX, PIPE_SHADER_FRAGMENT}) {
856
for (uint32_t i = 0; i < ctx->num_sampler_views[j]; i++) {
857
struct pipe_sampler_view *view = ctx->sampler_views[j][i];
858
if (view)
859
swr_resource_read(view->texture);
860
}
861
}
862
863
/* constant buffers */
864
for (uint32_t j : {PIPE_SHADER_VERTEX, PIPE_SHADER_FRAGMENT}) {
865
for (uint32_t i = 0; i < PIPE_MAX_CONSTANT_BUFFERS; i++) {
866
struct pipe_constant_buffer *cb = &ctx->constants[j][i];
867
if (cb->buffer)
868
swr_resource_read(cb->buffer);
869
}
870
}
871
}
872
873
static void
874
swr_update_texture_state(struct swr_context *ctx,
875
enum pipe_shader_type shader_type,
876
unsigned num_sampler_views,
877
swr_jit_texture *textures)
878
{
879
for (unsigned i = 0; i < num_sampler_views; i++) {
880
struct pipe_sampler_view *view =
881
ctx->sampler_views[shader_type][i];
882
struct swr_jit_texture *jit_tex = &textures[i];
883
884
memset(jit_tex, 0, sizeof(*jit_tex));
885
if (view) {
886
struct pipe_resource *res = view->texture;
887
struct swr_resource *swr_res = swr_resource(res);
888
SWR_SURFACE_STATE *swr = &swr_res->swr;
889
size_t *mip_offsets = swr_res->mip_offsets;
890
if (swr_res->has_depth && swr_res->has_stencil &&
891
!util_format_has_depth(util_format_description(view->format))) {
892
swr = &swr_res->secondary;
893
mip_offsets = swr_res->secondary_mip_offsets;
894
}
895
896
jit_tex->width = res->width0;
897
jit_tex->height = res->height0;
898
jit_tex->base_ptr = (uint8_t*)swr->xpBaseAddress;
899
jit_tex->num_samples = swr->numSamples;
900
jit_tex->sample_stride = 0;
901
if (view->target != PIPE_BUFFER) {
902
jit_tex->first_level = view->u.tex.first_level;
903
jit_tex->last_level = view->u.tex.last_level;
904
if (view->target == PIPE_TEXTURE_3D)
905
jit_tex->depth = res->depth0;
906
else
907
jit_tex->depth =
908
view->u.tex.last_layer - view->u.tex.first_layer + 1;
909
jit_tex->base_ptr += view->u.tex.first_layer *
910
swr->qpitch * swr->pitch;
911
} else {
912
unsigned view_blocksize = util_format_get_blocksize(view->format);
913
jit_tex->base_ptr += view->u.buf.offset;
914
jit_tex->width = view->u.buf.size / view_blocksize;
915
jit_tex->depth = 1;
916
}
917
918
for (unsigned level = jit_tex->first_level;
919
level <= jit_tex->last_level;
920
level++) {
921
jit_tex->row_stride[level] = swr->pitch;
922
jit_tex->img_stride[level] = swr->qpitch * swr->pitch;
923
jit_tex->mip_offsets[level] = mip_offsets[level];
924
}
925
}
926
}
927
}
928
929
static void
930
swr_update_sampler_state(struct swr_context *ctx,
931
enum pipe_shader_type shader_type,
932
unsigned num_samplers,
933
swr_jit_sampler *samplers)
934
{
935
for (unsigned i = 0; i < num_samplers; i++) {
936
const struct pipe_sampler_state *sampler =
937
ctx->samplers[shader_type][i];
938
939
if (sampler) {
940
samplers[i].min_lod = sampler->min_lod;
941
samplers[i].max_lod = sampler->max_lod;
942
samplers[i].lod_bias = sampler->lod_bias;
943
COPY_4V(samplers[i].border_color, sampler->border_color.f);
944
}
945
}
946
}
947
948
static void
949
swr_update_constants(struct swr_context *ctx, enum pipe_shader_type shaderType)
950
{
951
swr_draw_context *pDC = &ctx->swrDC;
952
953
const float **constant;
954
uint32_t *num_constants;
955
struct swr_scratch_space *scratch;
956
957
switch (shaderType) {
958
case PIPE_SHADER_VERTEX:
959
constant = pDC->constantVS;
960
num_constants = pDC->num_constantsVS;
961
scratch = &ctx->scratch->vs_constants;
962
break;
963
case PIPE_SHADER_FRAGMENT:
964
constant = pDC->constantFS;
965
num_constants = pDC->num_constantsFS;
966
scratch = &ctx->scratch->fs_constants;
967
break;
968
case PIPE_SHADER_GEOMETRY:
969
constant = pDC->constantGS;
970
num_constants = pDC->num_constantsGS;
971
scratch = &ctx->scratch->gs_constants;
972
break;
973
case PIPE_SHADER_TESS_CTRL:
974
constant = pDC->constantTCS;
975
num_constants = pDC->num_constantsTCS;
976
scratch = &ctx->scratch->tcs_constants;
977
break;
978
case PIPE_SHADER_TESS_EVAL:
979
constant = pDC->constantTES;
980
num_constants = pDC->num_constantsTES;
981
scratch = &ctx->scratch->tes_constants;
982
break;
983
default:
984
assert(0 && "Unsupported shader type constants");
985
return;
986
}
987
988
for (UINT i = 0; i < PIPE_MAX_CONSTANT_BUFFERS; i++) {
989
const pipe_constant_buffer *cb = &ctx->constants[shaderType][i];
990
num_constants[i] = cb->buffer_size;
991
if (cb->buffer) {
992
constant[i] =
993
(const float *)(swr_resource_data(cb->buffer) +
994
cb->buffer_offset);
995
} else {
996
/* Need to copy these constants to scratch space */
997
if (cb->user_buffer && cb->buffer_size) {
998
const void *ptr =
999
((const uint8_t *)cb->user_buffer + cb->buffer_offset);
1000
uint32_t size = AlignUp(cb->buffer_size, 4);
1001
ptr = swr_copy_to_scratch_space(ctx, scratch, ptr, size);
1002
constant[i] = (const float *)ptr;
1003
}
1004
}
1005
}
1006
}
1007
1008
static bool
1009
swr_change_rt(struct swr_context *ctx,
1010
unsigned attachment,
1011
const struct pipe_surface *sf)
1012
{
1013
swr_draw_context *pDC = &ctx->swrDC;
1014
struct SWR_SURFACE_STATE *rt = &pDC->renderTargets[attachment];
1015
1016
/* Do nothing if the render target hasn't changed */
1017
if ((!sf || !sf->texture) && (void*)(rt->xpBaseAddress) == nullptr)
1018
return false;
1019
1020
/* Deal with disabling RT up front */
1021
if (!sf || !sf->texture) {
1022
/* If detaching attachment, mark tiles as RESOLVED so core
1023
* won't try to load from non-existent target. */
1024
swr_store_render_target(&ctx->pipe, attachment, SWR_TILE_RESOLVED);
1025
*rt = {0};
1026
return true;
1027
}
1028
1029
const struct swr_resource *swr = swr_resource(sf->texture);
1030
const SWR_SURFACE_STATE *swr_surface = &swr->swr;
1031
SWR_FORMAT fmt = mesa_to_swr_format(sf->format);
1032
1033
if (attachment == SWR_ATTACHMENT_STENCIL && swr->secondary.xpBaseAddress) {
1034
swr_surface = &swr->secondary;
1035
fmt = swr_surface->format;
1036
}
1037
1038
if (rt->xpBaseAddress == swr_surface->xpBaseAddress &&
1039
rt->format == fmt &&
1040
rt->lod == sf->u.tex.level &&
1041
rt->arrayIndex == sf->u.tex.first_layer)
1042
return false;
1043
1044
bool need_fence = false;
1045
1046
/* StoreTile for changed target */
1047
if (rt->xpBaseAddress) {
1048
/* If changing attachment to a new target, mark tiles as
1049
* INVALID so they are reloaded from surface. */
1050
swr_store_render_target(&ctx->pipe, attachment, SWR_TILE_INVALID);
1051
need_fence = true;
1052
} else {
1053
/* if no previous attachment, invalidate tiles that may be marked
1054
* RESOLVED because of an old attachment */
1055
swr_invalidate_render_target(&ctx->pipe, attachment, sf->width, sf->height);
1056
/* no need to set fence here */
1057
}
1058
1059
/* Make new attachment */
1060
*rt = *swr_surface;
1061
rt->format = fmt;
1062
rt->lod = sf->u.tex.level;
1063
rt->arrayIndex = sf->u.tex.first_layer;
1064
1065
return need_fence;
1066
}
1067
1068
/*
1069
* for cases where resources are shared between contexts, invalidate
1070
* this ctx's resource. so it can be fetched fresh. Old ctx's resource
1071
* is already stored during a flush
1072
*/
1073
static inline void
1074
swr_invalidate_buffers_after_ctx_change(struct pipe_context *pipe)
1075
{
1076
struct swr_context *ctx = swr_context(pipe);
1077
1078
for (uint32_t i = 0; i < ctx->framebuffer.nr_cbufs; i++) {
1079
struct pipe_surface *cb = ctx->framebuffer.cbufs[i];
1080
if (cb) {
1081
struct swr_resource *res = swr_resource(cb->texture);
1082
if (res->curr_pipe != pipe) {
1083
/* if curr_pipe is NULL (first use), status should not be WRITE */
1084
assert(res->curr_pipe || !(res->status & SWR_RESOURCE_WRITE));
1085
if (res->status & SWR_RESOURCE_WRITE) {
1086
swr_invalidate_render_target(pipe, i, cb->width, cb->height);
1087
}
1088
}
1089
res->curr_pipe = pipe;
1090
}
1091
}
1092
if (ctx->framebuffer.zsbuf) {
1093
struct pipe_surface *zb = ctx->framebuffer.zsbuf;
1094
if (zb) {
1095
struct swr_resource *res = swr_resource(zb->texture);
1096
if (res->curr_pipe != pipe) {
1097
/* if curr_pipe is NULL (first use), status should not be WRITE */
1098
assert(res->curr_pipe || !(res->status & SWR_RESOURCE_WRITE));
1099
if (res->status & SWR_RESOURCE_WRITE) {
1100
swr_invalidate_render_target(pipe, SWR_ATTACHMENT_DEPTH, zb->width, zb->height);
1101
swr_invalidate_render_target(pipe, SWR_ATTACHMENT_STENCIL, zb->width, zb->height);
1102
}
1103
}
1104
res->curr_pipe = pipe;
1105
}
1106
}
1107
}
1108
1109
static inline void
1110
swr_user_vbuf_range(const struct pipe_draw_info *info,
1111
const struct swr_vertex_element_state *velems,
1112
const struct pipe_vertex_buffer *vb,
1113
uint32_t i,
1114
uint32_t *totelems,
1115
uint32_t *base,
1116
uint32_t *size,
1117
int index_bias)
1118
{
1119
/* FIXME: The size is too large - we don't access the full extra stride. */
1120
unsigned elems;
1121
unsigned elem_pitch = vb->stride + velems->stream_pitch[i];
1122
if (velems->instanced_bufs & (1U << i)) {
1123
elems = info->instance_count / velems->min_instance_div[i] + 1;
1124
*totelems = info->start_instance + elems;
1125
*base = info->start_instance * vb->stride;
1126
*size = elems * elem_pitch;
1127
} else if (vb->stride) {
1128
elems = info->max_index - info->min_index + 1;
1129
*totelems = (info->max_index + (info->index_size ? index_bias : 0)) + 1;
1130
*base = (info->min_index + (info->index_size ? index_bias : 0)) * vb->stride;
1131
*size = elems * elem_pitch;
1132
} else {
1133
*totelems = 1;
1134
*base = 0;
1135
*size = velems->stream_pitch[i];
1136
}
1137
}
1138
1139
static void
1140
swr_update_poly_stipple(struct swr_context *ctx)
1141
{
1142
struct swr_draw_context *pDC = &ctx->swrDC;
1143
1144
assert(sizeof(ctx->poly_stipple.pipe.stipple) == sizeof(pDC->polyStipple));
1145
memcpy(pDC->polyStipple,
1146
ctx->poly_stipple.pipe.stipple,
1147
sizeof(ctx->poly_stipple.pipe.stipple));
1148
}
1149
1150
1151
static struct tgsi_shader_info *
1152
swr_get_last_fe(const struct swr_context *ctx)
1153
{
1154
tgsi_shader_info *pLastFE = &ctx->vs->info.base;
1155
1156
if (ctx->gs) {
1157
pLastFE = &ctx->gs->info.base;
1158
}
1159
else if (ctx->tes) {
1160
pLastFE = &ctx->tes->info.base;
1161
}
1162
else if (ctx->tcs) {
1163
pLastFE = &ctx->tcs->info.base;
1164
}
1165
return pLastFE;
1166
}
1167
1168
1169
void
1170
swr_update_derived(struct pipe_context *pipe,
1171
const struct pipe_draw_info *p_draw_info,
1172
const struct pipe_draw_start_count_bias *draw)
1173
{
1174
struct swr_context *ctx = swr_context(pipe);
1175
struct swr_screen *screen = swr_screen(pipe->screen);
1176
1177
/* When called from swr_clear (p_draw_info = null), set any null
1178
* state-objects to the dummy state objects to prevent nullptr dereference
1179
* in validation below.
1180
*
1181
* Important that this remains static for zero initialization. These
1182
* aren't meant to be proper state objects, just empty structs. They will
1183
* not be written to.
1184
*
1185
* Shaders can't be part of the union since they contain std::unordered_map
1186
*/
1187
static struct {
1188
union {
1189
struct pipe_rasterizer_state rasterizer;
1190
struct pipe_depth_stencil_alpha_state depth_stencil;
1191
struct swr_blend_state blend;
1192
} state;
1193
struct swr_vertex_shader vs;
1194
struct swr_fragment_shader fs;
1195
} swr_dummy;
1196
1197
if (!p_draw_info) {
1198
if (!ctx->rasterizer)
1199
ctx->rasterizer = &swr_dummy.state.rasterizer;
1200
if (!ctx->depth_stencil)
1201
ctx->depth_stencil = &swr_dummy.state.depth_stencil;
1202
if (!ctx->blend)
1203
ctx->blend = &swr_dummy.state.blend;
1204
if (!ctx->vs)
1205
ctx->vs = &swr_dummy.vs;
1206
if (!ctx->fs)
1207
ctx->fs = &swr_dummy.fs;
1208
}
1209
1210
/* Update screen->pipe to current pipe context. */
1211
screen->pipe = pipe;
1212
1213
/* Any state that requires dirty flags to be re-triggered sets this mask */
1214
/* For example, user_buffer vertex and index buffers. */
1215
unsigned post_update_dirty_flags = 0;
1216
1217
/* bring resources that changed context up-to-date */
1218
swr_invalidate_buffers_after_ctx_change(pipe);
1219
1220
/* Render Targets */
1221
if (ctx->dirty & SWR_NEW_FRAMEBUFFER) {
1222
struct pipe_framebuffer_state *fb = &ctx->framebuffer;
1223
const struct util_format_description *desc = NULL;
1224
bool need_fence = false;
1225
1226
/* colorbuffer targets */
1227
if (fb->nr_cbufs) {
1228
for (unsigned i = 0; i < fb->nr_cbufs; ++i)
1229
need_fence |= swr_change_rt(
1230
ctx, SWR_ATTACHMENT_COLOR0 + i, fb->cbufs[i]);
1231
}
1232
for (unsigned i = fb->nr_cbufs; i < SWR_NUM_RENDERTARGETS; ++i)
1233
need_fence |= swr_change_rt(ctx, SWR_ATTACHMENT_COLOR0 + i, NULL);
1234
1235
/* depth/stencil target */
1236
if (fb->zsbuf)
1237
desc = util_format_description(fb->zsbuf->format);
1238
if (fb->zsbuf && util_format_has_depth(desc))
1239
need_fence |= swr_change_rt(ctx, SWR_ATTACHMENT_DEPTH, fb->zsbuf);
1240
else
1241
need_fence |= swr_change_rt(ctx, SWR_ATTACHMENT_DEPTH, NULL);
1242
1243
if (fb->zsbuf && util_format_has_stencil(desc))
1244
need_fence |= swr_change_rt(ctx, SWR_ATTACHMENT_STENCIL, fb->zsbuf);
1245
else
1246
need_fence |= swr_change_rt(ctx, SWR_ATTACHMENT_STENCIL, NULL);
1247
1248
/* This fence ensures any attachment changes are resolved before the
1249
* next draw */
1250
if (need_fence)
1251
swr_fence_submit(ctx, screen->flush_fence);
1252
}
1253
1254
/* Raster state */
1255
if (ctx->dirty & (SWR_NEW_RASTERIZER |
1256
SWR_NEW_VS | // clipping
1257
SWR_NEW_TES |
1258
SWR_NEW_TCS |
1259
SWR_NEW_FRAMEBUFFER)) {
1260
pipe_rasterizer_state *rasterizer = ctx->rasterizer;
1261
pipe_framebuffer_state *fb = &ctx->framebuffer;
1262
1263
SWR_RASTSTATE *rastState = &ctx->derived.rastState;
1264
rastState->cullMode = swr_convert_cull_mode(rasterizer->cull_face);
1265
rastState->frontWinding = rasterizer->front_ccw
1266
? SWR_FRONTWINDING_CCW
1267
: SWR_FRONTWINDING_CW;
1268
rastState->scissorEnable = rasterizer->scissor;
1269
rastState->pointSize = rasterizer->point_size > 0.0f
1270
? rasterizer->point_size
1271
: 1.0f;
1272
rastState->lineWidth = rasterizer->line_width > 0.0f
1273
? rasterizer->line_width
1274
: 1.0f;
1275
1276
rastState->pointParam = rasterizer->point_size_per_vertex;
1277
1278
rastState->pointSpriteEnable = rasterizer->sprite_coord_enable;
1279
rastState->pointSpriteTopOrigin =
1280
rasterizer->sprite_coord_mode == PIPE_SPRITE_COORD_UPPER_LEFT;
1281
1282
/* If SWR_MSAA_FORCE_ENABLE is set, turn msaa on */
1283
if (screen->msaa_force_enable && !rasterizer->multisample) {
1284
/* Force enable and use the value the surface was created with */
1285
rasterizer->multisample = true;
1286
fb->samples = swr_resource(fb->cbufs[0]->texture)->swr.numSamples;
1287
fprintf(stderr,"msaa force enable: %d samples\n", fb->samples);
1288
}
1289
1290
rastState->sampleCount = GetSampleCount(fb->samples);
1291
rastState->forcedSampleCount = false;
1292
rastState->bIsCenterPattern = !rasterizer->multisample;
1293
rastState->pixelLocation = SWR_PIXEL_LOCATION_CENTER;
1294
1295
/* Only initialize sample positions if msaa is enabled */
1296
if (rasterizer->multisample) {
1297
for (uint32_t i = 0; i < fb->samples; i++) {
1298
const uint8_t *sample = swr_sample_positions[fb->samples-1 + i];
1299
rastState->samplePositions.SetXi(i, sample[0] << 4);
1300
rastState->samplePositions.SetYi(i, sample[1] << 4);
1301
rastState->samplePositions.SetX (i, sample[0] / 16.0f);
1302
rastState->samplePositions.SetY (i, sample[1] / 16.0f);
1303
}
1304
rastState->samplePositions.PrecalcSampleData(fb->samples);
1305
}
1306
1307
bool do_offset = false;
1308
switch (rasterizer->fill_front) {
1309
case PIPE_POLYGON_MODE_FILL:
1310
do_offset = rasterizer->offset_tri;
1311
break;
1312
case PIPE_POLYGON_MODE_LINE:
1313
do_offset = rasterizer->offset_line;
1314
break;
1315
case PIPE_POLYGON_MODE_POINT:
1316
do_offset = rasterizer->offset_point;
1317
break;
1318
}
1319
1320
if (do_offset) {
1321
rastState->depthBias = rasterizer->offset_units;
1322
rastState->slopeScaledDepthBias = rasterizer->offset_scale;
1323
rastState->depthBiasClamp = rasterizer->offset_clamp;
1324
} else {
1325
rastState->depthBias = 0;
1326
rastState->slopeScaledDepthBias = 0;
1327
rastState->depthBiasClamp = 0;
1328
}
1329
1330
/* translate polygon mode, at least for the front==back case */
1331
rastState->fillMode = swr_convert_fill_mode(rasterizer->fill_front);
1332
1333
struct pipe_surface *zb = fb->zsbuf;
1334
if (zb && swr_resource(zb->texture)->has_depth)
1335
rastState->depthFormat = swr_resource(zb->texture)->swr.format;
1336
1337
rastState->depthClipEnable = rasterizer->depth_clip_near;
1338
rastState->clipEnable = rasterizer->depth_clip_near | rasterizer->depth_clip_far;
1339
rastState->clipHalfZ = rasterizer->clip_halfz;
1340
1341
ctx->api.pfnSwrSetRastState(ctx->swrContext, rastState);
1342
}
1343
1344
/* Viewport */
1345
if (ctx->dirty & (SWR_NEW_VIEWPORT | SWR_NEW_FRAMEBUFFER
1346
| SWR_NEW_RASTERIZER)) {
1347
pipe_viewport_state *state = &ctx->viewports[0];
1348
pipe_framebuffer_state *fb = &ctx->framebuffer;
1349
pipe_rasterizer_state *rasterizer = ctx->rasterizer;
1350
1351
SWR_VIEWPORT *vp = &ctx->derived.vp[0];
1352
SWR_VIEWPORT_MATRICES *vpm = &ctx->derived.vpm;
1353
1354
for (unsigned i = 0; i < KNOB_NUM_VIEWPORTS_SCISSORS; i++) {
1355
vp->x = state->translate[0] - state->scale[0];
1356
vp->width = 2 * state->scale[0];
1357
vp->y = state->translate[1] - fabs(state->scale[1]);
1358
vp->height = 2 * fabs(state->scale[1]);
1359
util_viewport_zmin_zmax(state, rasterizer->clip_halfz,
1360
&vp->minZ, &vp->maxZ);
1361
1362
if (rasterizer->depth_clip_near) {
1363
vp->minZ = 0.0f;
1364
}
1365
1366
if (rasterizer->depth_clip_far) {
1367
vp->maxZ = 1.0f;
1368
}
1369
1370
vpm->m00[i] = state->scale[0];
1371
vpm->m11[i] = state->scale[1];
1372
vpm->m22[i] = state->scale[2];
1373
vpm->m30[i] = state->translate[0];
1374
vpm->m31[i] = state->translate[1];
1375
vpm->m32[i] = state->translate[2];
1376
1377
/* Now that the matrix is calculated, clip the view coords to screen
1378
* size. OpenGL allows for -ve x,y in the viewport. */
1379
if (vp->x < 0.0f) {
1380
vp->width += vp->x;
1381
vp->x = 0.0f;
1382
}
1383
if (vp->y < 0.0f) {
1384
vp->height += vp->y;
1385
vp->y = 0.0f;
1386
}
1387
vp->width = std::min(vp->width, (float) fb->width - vp->x);
1388
vp->height = std::min(vp->height, (float) fb->height - vp->y);
1389
1390
vp++;
1391
state++;
1392
}
1393
ctx->api.pfnSwrSetViewports(ctx->swrContext, KNOB_NUM_VIEWPORTS_SCISSORS,
1394
&ctx->derived.vp[0], &ctx->derived.vpm);
1395
}
1396
1397
/* When called from swr_clear (p_draw_info = null), render targets,
1398
* rasterState and viewports (dependent on render targets) are the only
1399
* necessary validation. Defer remaining validation by setting
1400
* post_update_dirty_flags and clear all dirty flags. BackendState is
1401
* still unconditionally validated below */
1402
if (!p_draw_info) {
1403
post_update_dirty_flags = ctx->dirty & ~(SWR_NEW_FRAMEBUFFER |
1404
SWR_NEW_RASTERIZER |
1405
SWR_NEW_VIEWPORT);
1406
ctx->dirty = 0;
1407
}
1408
1409
/* Scissor */
1410
if (ctx->dirty & SWR_NEW_SCISSOR) {
1411
ctx->api.pfnSwrSetScissorRects(ctx->swrContext, KNOB_NUM_VIEWPORTS_SCISSORS, ctx->swr_scissors);
1412
}
1413
1414
/* Set vertex & index buffers */
1415
if (ctx->dirty & SWR_NEW_VERTEX) {
1416
const struct pipe_draw_info &info = *p_draw_info;
1417
1418
/* vertex buffers */
1419
SWR_VERTEX_BUFFER_STATE swrVertexBuffers[PIPE_MAX_ATTRIBS];
1420
for (UINT i = 0; i < ctx->num_vertex_buffers; i++) {
1421
uint32_t size = 0, pitch = 0, elems = 0, partial_inbounds = 0;
1422
uint32_t min_vertex_index = 0;
1423
const uint8_t *p_data;
1424
struct pipe_vertex_buffer *vb = &ctx->vertex_buffer[i];
1425
1426
pitch = vb->stride;
1427
if (vb->is_user_buffer) {
1428
/* Client buffer
1429
* client memory is one-time use, re-trigger SWR_NEW_VERTEX to
1430
* revalidate on each draw */
1431
post_update_dirty_flags |= SWR_NEW_VERTEX;
1432
1433
uint32_t base;
1434
swr_user_vbuf_range(&info, ctx->velems, vb, i, &elems, &base, &size, draw->index_bias);
1435
partial_inbounds = 0;
1436
min_vertex_index = info.min_index + (info.index_size ? draw->index_bias : 0);
1437
1438
size = AlignUp(size, 4);
1439
/* If size of client memory copy is too large, don't copy. The
1440
* draw will access user-buffer directly and then block. This is
1441
* faster than queuing many large client draws. */
1442
if (size >= screen->client_copy_limit) {
1443
post_update_dirty_flags |= SWR_BLOCK_CLIENT_DRAW;
1444
p_data = (const uint8_t *) vb->buffer.user;
1445
} else {
1446
/* Copy only needed vertices to scratch space */
1447
const void *ptr = (const uint8_t *) vb->buffer.user + base;
1448
ptr = (uint8_t *)swr_copy_to_scratch_space(
1449
ctx, &ctx->scratch->vertex_buffer, ptr, size);
1450
p_data = (const uint8_t *)ptr - base;
1451
}
1452
} else if (vb->buffer.resource) {
1453
/* VBO */
1454
if (!pitch) {
1455
/* If pitch=0 (ie vb->stride), buffer contains a single
1456
* constant attribute. Use the stream_pitch which was
1457
* calculated during creation of vertex_elements_state for the
1458
* size of the attribute. */
1459
size = ctx->velems->stream_pitch[i];
1460
elems = 1;
1461
partial_inbounds = 0;
1462
min_vertex_index = 0;
1463
} else {
1464
/* size is based on buffer->width0 rather than info.max_index
1465
* to prevent having to validate VBO on each draw. */
1466
size = vb->buffer.resource->width0;
1467
elems = size / pitch;
1468
partial_inbounds = size % pitch;
1469
min_vertex_index = 0;
1470
}
1471
1472
p_data = swr_resource_data(vb->buffer.resource) + vb->buffer_offset;
1473
} else
1474
p_data = NULL;
1475
1476
swrVertexBuffers[i] = {0};
1477
swrVertexBuffers[i].index = i;
1478
swrVertexBuffers[i].pitch = pitch;
1479
swrVertexBuffers[i].xpData = (gfxptr_t) p_data;
1480
swrVertexBuffers[i].size = size;
1481
swrVertexBuffers[i].minVertex = min_vertex_index;
1482
swrVertexBuffers[i].maxVertex = elems;
1483
swrVertexBuffers[i].partialInboundsSize = partial_inbounds;
1484
}
1485
1486
ctx->api.pfnSwrSetVertexBuffers(
1487
ctx->swrContext, ctx->num_vertex_buffers, swrVertexBuffers);
1488
1489
/* index buffer, if required (info passed in by swr_draw_vbo) */
1490
SWR_FORMAT index_type = R32_UINT; /* Default for non-indexed draws */
1491
if (info.index_size) {
1492
const uint8_t *p_data;
1493
uint32_t size, pitch;
1494
1495
pitch = info.index_size ? info.index_size : sizeof(uint32_t);
1496
index_type = swr_convert_index_type(pitch);
1497
1498
if (!info.has_user_indices) {
1499
/* VBO
1500
* size is based on buffer->width0 rather than info.count
1501
* to prevent having to validate VBO on each draw */
1502
size = info.index.resource->width0;
1503
p_data = swr_resource_data(info.index.resource);
1504
} else {
1505
/* Client buffer
1506
* client memory is one-time use, re-trigger SWR_NEW_VERTEX to
1507
* revalidate on each draw */
1508
post_update_dirty_flags |= SWR_NEW_VERTEX;
1509
1510
size = draw->count * pitch;
1511
1512
size = AlignUp(size, 4);
1513
/* If size of client memory copy is too large, don't copy. The
1514
* draw will access user-buffer directly and then block. This is
1515
* faster than queuing many large client draws. */
1516
if (size >= screen->client_copy_limit) {
1517
post_update_dirty_flags |= SWR_BLOCK_CLIENT_DRAW;
1518
p_data = (const uint8_t *) info.index.user +
1519
draw->start * info.index_size;
1520
} else {
1521
/* Copy indices to scratch space */
1522
const void *ptr = (char*)info.index.user +
1523
draw->start * info.index_size;
1524
ptr = swr_copy_to_scratch_space(
1525
ctx, &ctx->scratch->index_buffer, ptr, size);
1526
p_data = (const uint8_t *)ptr;
1527
}
1528
}
1529
1530
SWR_INDEX_BUFFER_STATE swrIndexBuffer;
1531
swrIndexBuffer.format = swr_convert_index_type(info.index_size);
1532
swrIndexBuffer.xpIndices = (gfxptr_t) p_data;
1533
swrIndexBuffer.size = size;
1534
1535
ctx->api.pfnSwrSetIndexBuffer(ctx->swrContext, &swrIndexBuffer);
1536
}
1537
1538
struct swr_vertex_element_state *velems = ctx->velems;
1539
if (velems && velems->fsState.indexType != index_type) {
1540
velems->fsFunc = NULL;
1541
velems->fsState.indexType = index_type;
1542
}
1543
}
1544
1545
/* GeometryShader */
1546
if (ctx->dirty & (SWR_NEW_GS |
1547
SWR_NEW_VS |
1548
SWR_NEW_TCS |
1549
SWR_NEW_TES |
1550
SWR_NEW_SAMPLER |
1551
SWR_NEW_SAMPLER_VIEW)) {
1552
if (ctx->gs) {
1553
swr_jit_gs_key key;
1554
swr_generate_gs_key(key, ctx, ctx->gs);
1555
auto search = ctx->gs->map.find(key);
1556
PFN_GS_FUNC func;
1557
if (search != ctx->gs->map.end()) {
1558
func = search->second->shader;
1559
} else {
1560
func = swr_compile_gs(ctx, key);
1561
}
1562
ctx->api.pfnSwrSetGsFunc(ctx->swrContext, func);
1563
1564
/* JIT sampler state */
1565
if (ctx->dirty & SWR_NEW_SAMPLER) {
1566
swr_update_sampler_state(ctx,
1567
PIPE_SHADER_GEOMETRY,
1568
key.nr_samplers,
1569
ctx->swrDC.samplersGS);
1570
}
1571
1572
/* JIT sampler view state */
1573
if (ctx->dirty & (SWR_NEW_SAMPLER_VIEW | SWR_NEW_FRAMEBUFFER)) {
1574
swr_update_texture_state(ctx,
1575
PIPE_SHADER_GEOMETRY,
1576
key.nr_sampler_views,
1577
ctx->swrDC.texturesGS);
1578
}
1579
1580
ctx->api.pfnSwrSetGsState(ctx->swrContext, &ctx->gs->gsState);
1581
} else {
1582
SWR_GS_STATE state = { 0 };
1583
ctx->api.pfnSwrSetGsState(ctx->swrContext, &state);
1584
ctx->api.pfnSwrSetGsFunc(ctx->swrContext, NULL);
1585
}
1586
}
1587
1588
// We may need to restore tessellation state
1589
// This restored state may be however overwritten
1590
// during shader compilation
1591
if (ctx->dirty & SWR_NEW_TS) {
1592
if (ctx->tes != nullptr) {
1593
ctx->tsState = ctx->tes->ts_state;
1594
ctx->api.pfnSwrSetTsState(ctx->swrContext, &ctx->tsState);
1595
} else {
1596
SWR_TS_STATE state = { 0 };
1597
ctx->api.pfnSwrSetTsState(ctx->swrContext, &state);
1598
}
1599
}
1600
1601
// Tessellation Evaluation Shader
1602
// Compile TES first, because TCS is optional
1603
if (ctx->dirty & (SWR_NEW_GS |
1604
SWR_NEW_VS |
1605
SWR_NEW_TCS |
1606
SWR_NEW_TES |
1607
SWR_NEW_SAMPLER |
1608
SWR_NEW_SAMPLER_VIEW)) {
1609
if (ctx->tes) {
1610
swr_jit_tes_key key;
1611
swr_generate_tes_key(key, ctx, ctx->tes);
1612
1613
auto search = ctx->tes->map.find(key);
1614
PFN_TES_FUNC func;
1615
if (search != ctx->tes->map.end()) {
1616
func = search->second->shader;
1617
} else {
1618
func = swr_compile_tes(ctx, key);
1619
}
1620
1621
ctx->api.pfnSwrSetDsFunc(ctx->swrContext, func);
1622
1623
/* JIT sampler state */
1624
if (ctx->dirty & SWR_NEW_SAMPLER) {
1625
swr_update_sampler_state(ctx,
1626
PIPE_SHADER_TESS_EVAL,
1627
key.nr_samplers,
1628
ctx->swrDC.samplersTES);
1629
}
1630
1631
/* JIT sampler view state */
1632
if (ctx->dirty & (SWR_NEW_SAMPLER_VIEW | SWR_NEW_FRAMEBUFFER)) {
1633
swr_update_texture_state(ctx,
1634
PIPE_SHADER_TESS_EVAL,
1635
key.nr_sampler_views,
1636
ctx->swrDC.texturesTES);
1637
}
1638
1639
// Update tessellation state in case it's been updated
1640
ctx->api.pfnSwrSetTsState(ctx->swrContext, &ctx->tsState);
1641
} else {
1642
ctx->api.pfnSwrSetDsFunc(ctx->swrContext, NULL);
1643
}
1644
}
1645
1646
/* Tessellation Control Shader */
1647
if (ctx->dirty & (SWR_NEW_GS |
1648
SWR_NEW_VS |
1649
SWR_NEW_TCS |
1650
SWR_NEW_TES |
1651
SWR_NEW_SAMPLER |
1652
SWR_NEW_SAMPLER_VIEW)) {
1653
if (ctx->tcs) {
1654
ctx->tcs->vertices_per_patch = p_draw_info->vertices_per_patch;
1655
1656
swr_jit_tcs_key key;
1657
swr_generate_tcs_key(key, ctx, ctx->tcs);
1658
1659
auto search = ctx->tcs->map.find(key);
1660
PFN_TCS_FUNC func;
1661
if (search != ctx->tcs->map.end()) {
1662
func = search->second->shader;
1663
} else {
1664
func = swr_compile_tcs(ctx, key);
1665
}
1666
1667
ctx->api.pfnSwrSetHsFunc(ctx->swrContext, func);
1668
1669
/* JIT sampler state */
1670
if (ctx->dirty & SWR_NEW_SAMPLER) {
1671
swr_update_sampler_state(ctx,
1672
PIPE_SHADER_TESS_CTRL,
1673
key.nr_samplers,
1674
ctx->swrDC.samplersTCS);
1675
}
1676
1677
/* JIT sampler view state */
1678
if (ctx->dirty & (SWR_NEW_SAMPLER_VIEW | SWR_NEW_FRAMEBUFFER)) {
1679
swr_update_texture_state(ctx,
1680
PIPE_SHADER_TESS_CTRL,
1681
key.nr_sampler_views,
1682
ctx->swrDC.texturesTCS);
1683
}
1684
1685
// Update tessellation state in case it's been updated
1686
ctx->api.pfnSwrSetTsState(ctx->swrContext, &ctx->tsState);
1687
} else {
1688
ctx->api.pfnSwrSetHsFunc(ctx->swrContext, NULL);
1689
}
1690
}
1691
1692
/* VertexShader */
1693
if (ctx->dirty
1694
& (SWR_NEW_VS | SWR_NEW_RASTERIZER | // for clip planes
1695
SWR_NEW_SAMPLER | SWR_NEW_SAMPLER_VIEW | SWR_NEW_FRAMEBUFFER)) {
1696
swr_jit_vs_key key;
1697
swr_generate_vs_key(key, ctx, ctx->vs);
1698
auto search = ctx->vs->map.find(key);
1699
PFN_VERTEX_FUNC func;
1700
if (search != ctx->vs->map.end()) {
1701
func = search->second->shader;
1702
} else {
1703
func = swr_compile_vs(ctx, key);
1704
}
1705
ctx->api.pfnSwrSetVertexFunc(ctx->swrContext, func);
1706
1707
/* JIT sampler state */
1708
if (ctx->dirty & SWR_NEW_SAMPLER) {
1709
swr_update_sampler_state(
1710
ctx, PIPE_SHADER_VERTEX, key.nr_samplers, ctx->swrDC.samplersVS);
1711
}
1712
1713
/* JIT sampler view state */
1714
if (ctx->dirty & (SWR_NEW_SAMPLER_VIEW | SWR_NEW_FRAMEBUFFER)) {
1715
swr_update_texture_state(ctx,
1716
PIPE_SHADER_VERTEX,
1717
key.nr_sampler_views,
1718
ctx->swrDC.texturesVS);
1719
}
1720
}
1721
1722
/* work around the fact that poly stipple also affects lines */
1723
/* and points, since we rasterize them as triangles, too */
1724
/* Has to be before fragment shader, since it sets SWR_NEW_FS */
1725
if (p_draw_info) {
1726
bool new_prim_is_poly =
1727
(u_reduced_prim(p_draw_info->mode) == PIPE_PRIM_TRIANGLES) &&
1728
(ctx->derived.rastState.fillMode == SWR_FILLMODE_SOLID);
1729
if (new_prim_is_poly != ctx->poly_stipple.prim_is_poly) {
1730
ctx->dirty |= SWR_NEW_FS;
1731
ctx->poly_stipple.prim_is_poly = new_prim_is_poly;
1732
}
1733
}
1734
1735
/* FragmentShader */
1736
if (ctx->dirty & (SWR_NEW_FS |
1737
SWR_NEW_VS |
1738
SWR_NEW_GS |
1739
SWR_NEW_TES |
1740
SWR_NEW_TCS |
1741
SWR_NEW_RASTERIZER |
1742
SWR_NEW_SAMPLER |
1743
SWR_NEW_SAMPLER_VIEW |
1744
SWR_NEW_FRAMEBUFFER)) {
1745
swr_jit_fs_key key;
1746
swr_generate_fs_key(key, ctx, ctx->fs);
1747
auto search = ctx->fs->map.find(key);
1748
PFN_PIXEL_KERNEL func;
1749
if (search != ctx->fs->map.end()) {
1750
func = search->second->shader;
1751
} else {
1752
func = swr_compile_fs(ctx, key);
1753
}
1754
SWR_PS_STATE psState = {0};
1755
psState.pfnPixelShader = func;
1756
psState.killsPixel = ctx->fs->info.base.uses_kill;
1757
psState.inputCoverage = SWR_INPUT_COVERAGE_NORMAL;
1758
psState.writesODepth = ctx->fs->info.base.writes_z;
1759
psState.usesSourceDepth = ctx->fs->info.base.reads_z;
1760
psState.shadingRate = SWR_SHADING_RATE_PIXEL;
1761
psState.renderTargetMask = (1 << ctx->framebuffer.nr_cbufs) - 1;
1762
psState.posOffset = SWR_PS_POSITION_SAMPLE_NONE;
1763
uint32_t barycentricsMask = 0;
1764
#if 0
1765
// when we switch to mesa-master
1766
if (ctx->fs->info.base.uses_persp_center ||
1767
ctx->fs->info.base.uses_linear_center)
1768
barycentricsMask |= SWR_BARYCENTRIC_PER_PIXEL_MASK;
1769
if (ctx->fs->info.base.uses_persp_centroid ||
1770
ctx->fs->info.base.uses_linear_centroid)
1771
barycentricsMask |= SWR_BARYCENTRIC_CENTROID_MASK;
1772
if (ctx->fs->info.base.uses_persp_sample ||
1773
ctx->fs->info.base.uses_linear_sample)
1774
barycentricsMask |= SWR_BARYCENTRIC_PER_SAMPLE_MASK;
1775
#else
1776
for (unsigned i = 0; i < ctx->fs->info.base.num_inputs; i++) {
1777
switch (ctx->fs->info.base.input_interpolate_loc[i]) {
1778
case TGSI_INTERPOLATE_LOC_CENTER:
1779
barycentricsMask |= SWR_BARYCENTRIC_PER_PIXEL_MASK;
1780
break;
1781
case TGSI_INTERPOLATE_LOC_CENTROID:
1782
barycentricsMask |= SWR_BARYCENTRIC_CENTROID_MASK;
1783
break;
1784
case TGSI_INTERPOLATE_LOC_SAMPLE:
1785
barycentricsMask |= SWR_BARYCENTRIC_PER_SAMPLE_MASK;
1786
break;
1787
}
1788
}
1789
#endif
1790
psState.barycentricsMask = barycentricsMask;
1791
psState.usesUAV = false; // XXX
1792
psState.forceEarlyZ = false;
1793
ctx->api.pfnSwrSetPixelShaderState(ctx->swrContext, &psState);
1794
1795
/* JIT sampler state */
1796
if (ctx->dirty & (SWR_NEW_SAMPLER |
1797
SWR_NEW_FS)) {
1798
swr_update_sampler_state(ctx,
1799
PIPE_SHADER_FRAGMENT,
1800
key.nr_samplers,
1801
ctx->swrDC.samplersFS);
1802
}
1803
1804
/* JIT sampler view state */
1805
if (ctx->dirty & (SWR_NEW_SAMPLER_VIEW |
1806
SWR_NEW_FRAMEBUFFER |
1807
SWR_NEW_FS)) {
1808
swr_update_texture_state(ctx,
1809
PIPE_SHADER_FRAGMENT,
1810
key.nr_sampler_views,
1811
ctx->swrDC.texturesFS);
1812
}
1813
}
1814
1815
1816
/* VertexShader Constants */
1817
if (ctx->dirty & SWR_NEW_VSCONSTANTS) {
1818
swr_update_constants(ctx, PIPE_SHADER_VERTEX);
1819
}
1820
1821
/* FragmentShader Constants */
1822
if (ctx->dirty & SWR_NEW_FSCONSTANTS) {
1823
swr_update_constants(ctx, PIPE_SHADER_FRAGMENT);
1824
}
1825
1826
/* GeometryShader Constants */
1827
if (ctx->dirty & SWR_NEW_GSCONSTANTS) {
1828
swr_update_constants(ctx, PIPE_SHADER_GEOMETRY);
1829
}
1830
1831
/* Tessellation Control Shader Constants */
1832
if (ctx->dirty & SWR_NEW_TCSCONSTANTS) {
1833
swr_update_constants(ctx, PIPE_SHADER_TESS_CTRL);
1834
}
1835
1836
/* Tessellation Evaluation Shader Constants */
1837
if (ctx->dirty & SWR_NEW_TESCONSTANTS) {
1838
swr_update_constants(ctx, PIPE_SHADER_TESS_EVAL);
1839
}
1840
1841
/* Depth/stencil state */
1842
if (ctx->dirty & (SWR_NEW_DEPTH_STENCIL_ALPHA | SWR_NEW_FRAMEBUFFER)) {
1843
struct pipe_depth_stencil_alpha_state *depth = ctx->depth_stencil;
1844
struct pipe_stencil_state *stencil = depth->stencil;
1845
SWR_DEPTH_STENCIL_STATE depthStencilState = {{0}};
1846
SWR_DEPTH_BOUNDS_STATE depthBoundsState = {0};
1847
1848
/* XXX, incomplete. Need to flesh out stencil & alpha test state
1849
struct pipe_stencil_state *front_stencil =
1850
ctx->depth_stencil.stencil[0];
1851
struct pipe_stencil_state *back_stencil = ctx->depth_stencil.stencil[1];
1852
*/
1853
if (stencil[0].enabled) {
1854
depthStencilState.stencilWriteEnable = 1;
1855
depthStencilState.stencilTestEnable = 1;
1856
depthStencilState.stencilTestFunc =
1857
swr_convert_depth_func(stencil[0].func);
1858
1859
depthStencilState.stencilPassDepthPassOp =
1860
swr_convert_stencil_op(stencil[0].zpass_op);
1861
depthStencilState.stencilPassDepthFailOp =
1862
swr_convert_stencil_op(stencil[0].zfail_op);
1863
depthStencilState.stencilFailOp =
1864
swr_convert_stencil_op(stencil[0].fail_op);
1865
depthStencilState.stencilWriteMask = stencil[0].writemask;
1866
depthStencilState.stencilTestMask = stencil[0].valuemask;
1867
depthStencilState.stencilRefValue = ctx->stencil_ref.ref_value[0];
1868
}
1869
if (stencil[1].enabled) {
1870
depthStencilState.doubleSidedStencilTestEnable = 1;
1871
1872
depthStencilState.backfaceStencilTestFunc =
1873
swr_convert_depth_func(stencil[1].func);
1874
1875
depthStencilState.backfaceStencilPassDepthPassOp =
1876
swr_convert_stencil_op(stencil[1].zpass_op);
1877
depthStencilState.backfaceStencilPassDepthFailOp =
1878
swr_convert_stencil_op(stencil[1].zfail_op);
1879
depthStencilState.backfaceStencilFailOp =
1880
swr_convert_stencil_op(stencil[1].fail_op);
1881
depthStencilState.backfaceStencilWriteMask = stencil[1].writemask;
1882
depthStencilState.backfaceStencilTestMask = stencil[1].valuemask;
1883
1884
depthStencilState.backfaceStencilRefValue =
1885
ctx->stencil_ref.ref_value[1];
1886
}
1887
1888
depthStencilState.depthTestEnable = depth->depth_enabled;
1889
depthStencilState.depthTestFunc = swr_convert_depth_func(depth->depth_func);
1890
depthStencilState.depthWriteEnable = depth->depth_writemask;
1891
ctx->api.pfnSwrSetDepthStencilState(ctx->swrContext, &depthStencilState);
1892
1893
depthBoundsState.depthBoundsTestEnable = depth->depth_bounds_test;
1894
depthBoundsState.depthBoundsTestMinValue = depth->depth_bounds_min;
1895
depthBoundsState.depthBoundsTestMaxValue = depth->depth_bounds_max;
1896
ctx->api.pfnSwrSetDepthBoundsState(ctx->swrContext, &depthBoundsState);
1897
}
1898
1899
/* Blend State */
1900
if (ctx->dirty & (SWR_NEW_BLEND |
1901
SWR_NEW_RASTERIZER |
1902
SWR_NEW_FRAMEBUFFER |
1903
SWR_NEW_DEPTH_STENCIL_ALPHA)) {
1904
struct pipe_framebuffer_state *fb = &ctx->framebuffer;
1905
1906
SWR_BLEND_STATE blendState;
1907
memcpy(&blendState, &ctx->blend->blendState, sizeof(blendState));
1908
blendState.constantColor[0] = ctx->blend_color.color[0];
1909
blendState.constantColor[1] = ctx->blend_color.color[1];
1910
blendState.constantColor[2] = ctx->blend_color.color[2];
1911
blendState.constantColor[3] = ctx->blend_color.color[3];
1912
blendState.alphaTestReference =
1913
*((uint32_t*)&ctx->depth_stencil->alpha_ref_value);
1914
1915
blendState.sampleMask = ctx->sample_mask;
1916
blendState.sampleCount = GetSampleCount(fb->samples);
1917
1918
/* If there are no color buffers bound, disable writes on RT0
1919
* and skip loop */
1920
if (fb->nr_cbufs == 0) {
1921
blendState.renderTarget[0].writeDisableRed = 1;
1922
blendState.renderTarget[0].writeDisableGreen = 1;
1923
blendState.renderTarget[0].writeDisableBlue = 1;
1924
blendState.renderTarget[0].writeDisableAlpha = 1;
1925
ctx->api.pfnSwrSetBlendFunc(ctx->swrContext, 0, NULL);
1926
}
1927
else
1928
for (int target = 0;
1929
target < std::min(SWR_NUM_RENDERTARGETS,
1930
PIPE_MAX_COLOR_BUFS);
1931
target++) {
1932
if (!fb->cbufs[target])
1933
continue;
1934
1935
struct swr_resource *colorBuffer =
1936
swr_resource(fb->cbufs[target]->texture);
1937
1938
BLEND_COMPILE_STATE compileState;
1939
memset(&compileState, 0, sizeof(compileState));
1940
compileState.format = colorBuffer->swr.format;
1941
memcpy(&compileState.blendState,
1942
&ctx->blend->compileState[target],
1943
sizeof(compileState.blendState));
1944
1945
const SWR_FORMAT_INFO& info = GetFormatInfo(compileState.format);
1946
if (compileState.blendState.logicOpEnable &&
1947
((info.type[0] == SWR_TYPE_FLOAT) || info.isSRGB)) {
1948
compileState.blendState.logicOpEnable = false;
1949
}
1950
1951
if (info.type[0] == SWR_TYPE_SINT || info.type[0] == SWR_TYPE_UINT)
1952
compileState.blendState.blendEnable = false;
1953
1954
if (compileState.blendState.blendEnable == false &&
1955
compileState.blendState.logicOpEnable == false &&
1956
ctx->depth_stencil->alpha_enabled == 0) {
1957
ctx->api.pfnSwrSetBlendFunc(ctx->swrContext, target, NULL);
1958
continue;
1959
}
1960
1961
compileState.desc.alphaTestEnable =
1962
ctx->depth_stencil->alpha_enabled;
1963
compileState.desc.independentAlphaBlendEnable =
1964
(compileState.blendState.sourceBlendFactor !=
1965
compileState.blendState.sourceAlphaBlendFactor) ||
1966
(compileState.blendState.destBlendFactor !=
1967
compileState.blendState.destAlphaBlendFactor) ||
1968
(compileState.blendState.colorBlendFunc !=
1969
compileState.blendState.alphaBlendFunc);
1970
compileState.desc.alphaToCoverageEnable =
1971
ctx->blend->pipe.alpha_to_coverage;
1972
compileState.desc.sampleMaskEnable = (blendState.sampleMask != 0);
1973
compileState.desc.numSamples = fb->samples;
1974
1975
compileState.alphaTestFunction =
1976
swr_convert_depth_func(ctx->depth_stencil->alpha_func);
1977
compileState.alphaTestFormat = ALPHA_TEST_FLOAT32; // xxx
1978
1979
compileState.Canonicalize();
1980
1981
PFN_BLEND_JIT_FUNC func = NULL;
1982
auto search = ctx->blendJIT->find(compileState);
1983
if (search != ctx->blendJIT->end()) {
1984
func = search->second;
1985
} else {
1986
HANDLE hJitMgr = screen->hJitMgr;
1987
func = JitCompileBlend(hJitMgr, compileState);
1988
debug_printf("BLEND shader %p\n", func);
1989
assert(func && "Error: BlendShader = NULL");
1990
1991
ctx->blendJIT->insert(std::make_pair(compileState, func));
1992
}
1993
ctx->api.pfnSwrSetBlendFunc(ctx->swrContext, target, func);
1994
}
1995
1996
ctx->api.pfnSwrSetBlendState(ctx->swrContext, &blendState);
1997
}
1998
1999
if (ctx->dirty & SWR_NEW_STIPPLE) {
2000
swr_update_poly_stipple(ctx);
2001
}
2002
2003
if (ctx->dirty & (SWR_NEW_VS | SWR_NEW_TCS | SWR_NEW_TES | SWR_NEW_SO | SWR_NEW_RASTERIZER)) {
2004
ctx->vs->soState.rasterizerDisable =
2005
ctx->rasterizer->rasterizer_discard;
2006
ctx->api.pfnSwrSetSoState(ctx->swrContext, &ctx->vs->soState);
2007
2008
pipe_stream_output_info *stream_output = &ctx->vs->pipe.stream_output;
2009
2010
for (uint32_t i = 0; i < MAX_SO_STREAMS; i++) {
2011
SWR_STREAMOUT_BUFFER buffer = {0};
2012
if (ctx->so_targets[i]) {
2013
buffer.enable = true;
2014
buffer.pBuffer =
2015
(gfxptr_t)(swr_resource_data(ctx->so_targets[i]->buffer) +
2016
ctx->so_targets[i]->buffer_offset);
2017
buffer.bufferSize = ctx->so_targets[i]->buffer_size >> 2;
2018
buffer.pitch = stream_output->stride[i];
2019
buffer.streamOffset = 0;
2020
}
2021
2022
ctx->api.pfnSwrSetSoBuffers(ctx->swrContext, &buffer, i);
2023
}
2024
}
2025
2026
2027
if (ctx->dirty & (SWR_NEW_CLIP | SWR_NEW_RASTERIZER | SWR_NEW_VS)) {
2028
// shader exporting clip distances overrides all user clip planes
2029
if (ctx->rasterizer->clip_plane_enable &&
2030
!swr_get_last_fe(ctx)->num_written_clipdistance)
2031
{
2032
swr_draw_context *pDC = &ctx->swrDC;
2033
memcpy(pDC->userClipPlanes,
2034
ctx->clip.ucp,
2035
sizeof(pDC->userClipPlanes));
2036
}
2037
}
2038
2039
// set up backend state
2040
SWR_BACKEND_STATE backendState = {0};
2041
if (ctx->gs) {
2042
backendState.numAttributes = ctx->gs->info.base.num_outputs - 1;
2043
} else
2044
if (ctx->tes) {
2045
backendState.numAttributes = ctx->tes->info.base.num_outputs - 1;
2046
// no case for TCS, because if TCS is active, TES must be active
2047
// as well - pipeline stages after tessellation does not support patches
2048
} else {
2049
backendState.numAttributes = ctx->vs->info.base.num_outputs - 1;
2050
if (ctx->fs->info.base.uses_primid) {
2051
backendState.numAttributes++;
2052
backendState.swizzleEnable = true;
2053
for (unsigned i = 0; i < sizeof(backendState.numComponents); i++) {
2054
backendState.swizzleMap[i].sourceAttrib = i;
2055
}
2056
backendState.swizzleMap[ctx->vs->info.base.num_outputs - 1].constantSource =
2057
SWR_CONSTANT_SOURCE_PRIM_ID;
2058
backendState.swizzleMap[ctx->vs->info.base.num_outputs - 1].componentOverrideMask = 1;
2059
}
2060
}
2061
if (ctx->rasterizer->sprite_coord_enable)
2062
backendState.numAttributes++;
2063
2064
backendState.numAttributes = std::min((size_t)backendState.numAttributes,
2065
sizeof(backendState.numComponents));
2066
for (unsigned i = 0; i < backendState.numAttributes; i++)
2067
backendState.numComponents[i] = 4;
2068
backendState.constantInterpolationMask = ctx->fs->constantMask |
2069
(ctx->rasterizer->flatshade ? ctx->fs->flatConstantMask : 0);
2070
backendState.pointSpriteTexCoordMask = ctx->fs->pointSpriteMask;
2071
2072
struct tgsi_shader_info *pLastFE = swr_get_last_fe(ctx);
2073
2074
backendState.readRenderTargetArrayIndex = pLastFE->writes_layer;
2075
backendState.readViewportArrayIndex = pLastFE->writes_viewport_index;
2076
backendState.vertexAttribOffset = VERTEX_ATTRIB_START_SLOT; // TODO: optimize
2077
2078
backendState.clipDistanceMask =
2079
pLastFE->num_written_clipdistance ?
2080
pLastFE->clipdist_writemask & ctx->rasterizer->clip_plane_enable :
2081
ctx->rasterizer->clip_plane_enable;
2082
2083
backendState.cullDistanceMask =
2084
pLastFE->culldist_writemask << pLastFE->num_written_clipdistance;
2085
2086
// Assume old layout of SGV, POSITION, CLIPCULL, ATTRIB
2087
backendState.vertexClipCullOffset = backendState.vertexAttribOffset - 2;
2088
2089
ctx->api.pfnSwrSetBackendState(ctx->swrContext, &backendState);
2090
2091
/* Ensure that any in-progress attachment change StoreTiles finish */
2092
if (swr_is_fence_pending(screen->flush_fence))
2093
swr_fence_finish(pipe->screen, NULL, screen->flush_fence, 0);
2094
2095
/* Finally, update the in-use status of all resources involved in draw */
2096
swr_update_resource_status(pipe, p_draw_info);
2097
2098
ctx->dirty = post_update_dirty_flags;
2099
}
2100
2101
2102
static struct pipe_stream_output_target *
2103
swr_create_so_target(struct pipe_context *pipe,
2104
struct pipe_resource *buffer,
2105
unsigned buffer_offset,
2106
unsigned buffer_size)
2107
{
2108
struct pipe_stream_output_target *target;
2109
2110
target = CALLOC_STRUCT(pipe_stream_output_target);
2111
if (!target)
2112
return NULL;
2113
2114
target->context = pipe;
2115
target->reference.count = 1;
2116
pipe_resource_reference(&target->buffer, buffer);
2117
target->buffer_offset = buffer_offset;
2118
target->buffer_size = buffer_size;
2119
return target;
2120
}
2121
2122
static void
2123
swr_destroy_so_target(struct pipe_context *pipe,
2124
struct pipe_stream_output_target *target)
2125
{
2126
pipe_resource_reference(&target->buffer, NULL);
2127
FREE(target);
2128
}
2129
2130
static void
2131
swr_set_so_targets(struct pipe_context *pipe,
2132
unsigned num_targets,
2133
struct pipe_stream_output_target **targets,
2134
const unsigned *offsets)
2135
{
2136
struct swr_context *swr = swr_context(pipe);
2137
uint32_t i;
2138
2139
assert(num_targets <= MAX_SO_STREAMS);
2140
2141
for (i = 0; i < num_targets; i++) {
2142
pipe_so_target_reference(
2143
(struct pipe_stream_output_target **)&swr->so_targets[i],
2144
targets[i]);
2145
}
2146
2147
for (/* fall-through */; i < swr->num_so_targets; i++) {
2148
pipe_so_target_reference(
2149
(struct pipe_stream_output_target **)&swr->so_targets[i], NULL);
2150
}
2151
2152
swr->num_so_targets = num_targets;
2153
swr->swrDC.soPrims = &swr->so_primCounter;
2154
2155
swr->dirty |= SWR_NEW_SO;
2156
}
2157
2158
2159
void
2160
swr_state_init(struct pipe_context *pipe)
2161
{
2162
pipe->create_blend_state = swr_create_blend_state;
2163
pipe->bind_blend_state = swr_bind_blend_state;
2164
pipe->delete_blend_state = swr_delete_blend_state;
2165
2166
pipe->create_depth_stencil_alpha_state = swr_create_depth_stencil_state;
2167
pipe->bind_depth_stencil_alpha_state = swr_bind_depth_stencil_state;
2168
pipe->delete_depth_stencil_alpha_state = swr_delete_depth_stencil_state;
2169
2170
pipe->create_rasterizer_state = swr_create_rasterizer_state;
2171
pipe->bind_rasterizer_state = swr_bind_rasterizer_state;
2172
pipe->delete_rasterizer_state = swr_delete_rasterizer_state;
2173
2174
pipe->create_sampler_state = swr_create_sampler_state;
2175
pipe->bind_sampler_states = swr_bind_sampler_states;
2176
pipe->delete_sampler_state = swr_delete_sampler_state;
2177
2178
pipe->create_sampler_view = swr_create_sampler_view;
2179
pipe->set_sampler_views = swr_set_sampler_views;
2180
pipe->sampler_view_destroy = swr_sampler_view_destroy;
2181
2182
pipe->create_vs_state = swr_create_vs_state;
2183
pipe->bind_vs_state = swr_bind_vs_state;
2184
pipe->delete_vs_state = swr_delete_vs_state;
2185
2186
pipe->create_fs_state = swr_create_fs_state;
2187
pipe->bind_fs_state = swr_bind_fs_state;
2188
pipe->delete_fs_state = swr_delete_fs_state;
2189
2190
pipe->create_gs_state = swr_create_gs_state;
2191
pipe->bind_gs_state = swr_bind_gs_state;
2192
pipe->delete_gs_state = swr_delete_gs_state;
2193
2194
pipe->create_tcs_state = swr_create_tcs_state;
2195
pipe->bind_tcs_state = swr_bind_tcs_state;
2196
pipe->delete_tcs_state = swr_delete_tcs_state;
2197
2198
pipe->create_tes_state = swr_create_tes_state;
2199
pipe->bind_tes_state = swr_bind_tes_state;
2200
pipe->delete_tes_state = swr_delete_tes_state;
2201
2202
pipe->set_constant_buffer = swr_set_constant_buffer;
2203
2204
pipe->create_vertex_elements_state = swr_create_vertex_elements_state;
2205
pipe->bind_vertex_elements_state = swr_bind_vertex_elements_state;
2206
pipe->delete_vertex_elements_state = swr_delete_vertex_elements_state;
2207
2208
pipe->set_vertex_buffers = swr_set_vertex_buffers;
2209
2210
pipe->set_polygon_stipple = swr_set_polygon_stipple;
2211
pipe->set_clip_state = swr_set_clip_state;
2212
pipe->set_scissor_states = swr_set_scissor_states;
2213
pipe->set_viewport_states = swr_set_viewport_states;
2214
2215
pipe->set_framebuffer_state = swr_set_framebuffer_state;
2216
2217
pipe->set_blend_color = swr_set_blend_color;
2218
pipe->set_stencil_ref = swr_set_stencil_ref;
2219
2220
pipe->set_sample_mask = swr_set_sample_mask;
2221
pipe->get_sample_position = swr_get_sample_position;
2222
2223
pipe->create_stream_output_target = swr_create_so_target;
2224
pipe->stream_output_target_destroy = swr_destroy_so_target;
2225
pipe->set_stream_output_targets = swr_set_so_targets;
2226
}
2227
2228