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