3e023225433bc0eba2155068fe5f266eb6928ce5
[mesa.git] / src / gallium / drivers / swr / swr_state.cpp
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 // llvm redefines DEBUG
25 #pragma push_macro("DEBUG")
26 #undef DEBUG
27 #include "JitManager.h"
28 #pragma pop_macro("DEBUG")
29
30 #include "common/os.h"
31 #include "jit_api.h"
32 #include "state_llvm.h"
33
34 #include "gallivm/lp_bld_tgsi.h"
35 #include "util/u_format.h"
36
37 #include "util/u_memory.h"
38 #include "util/u_inlines.h"
39 #include "util/u_helpers.h"
40 #include "util/u_framebuffer.h"
41
42 #include "swr_state.h"
43 #include "swr_context.h"
44 #include "swr_context_llvm.h"
45 #include "swr_screen.h"
46 #include "swr_resource.h"
47 #include "swr_tex_sample.h"
48 #include "swr_scratch.h"
49 #include "swr_shader.h"
50 #include "swr_fence.h"
51
52 /* These should be pulled out into separate files as necessary
53 * Just initializing everything here to get going. */
54
55 static void *
56 swr_create_blend_state(struct pipe_context *pipe,
57 const struct pipe_blend_state *blend)
58 {
59 struct swr_blend_state *state = CALLOC_STRUCT(swr_blend_state);
60
61 memcpy(&state->pipe, blend, sizeof(*blend));
62
63 struct pipe_blend_state *pipe_blend = &state->pipe;
64
65 for (int target = 0;
66 target < std::min(SWR_NUM_RENDERTARGETS, PIPE_MAX_COLOR_BUFS);
67 target++) {
68
69 struct pipe_rt_blend_state *rt_blend = &pipe_blend->rt[target];
70 SWR_RENDER_TARGET_BLEND_STATE &blendState =
71 state->blendState.renderTarget[target];
72 RENDER_TARGET_BLEND_COMPILE_STATE &compileState =
73 state->compileState[target];
74
75 if (target != 0 && !pipe_blend->independent_blend_enable) {
76 memcpy(&compileState,
77 &state->compileState[0],
78 sizeof(RENDER_TARGET_BLEND_COMPILE_STATE));
79 continue;
80 }
81
82 compileState.blendEnable = rt_blend->blend_enable;
83 if (compileState.blendEnable) {
84 compileState.sourceAlphaBlendFactor =
85 swr_convert_blend_factor(rt_blend->alpha_src_factor);
86 compileState.destAlphaBlendFactor =
87 swr_convert_blend_factor(rt_blend->alpha_dst_factor);
88 compileState.sourceBlendFactor =
89 swr_convert_blend_factor(rt_blend->rgb_src_factor);
90 compileState.destBlendFactor =
91 swr_convert_blend_factor(rt_blend->rgb_dst_factor);
92
93 compileState.colorBlendFunc =
94 swr_convert_blend_func(rt_blend->rgb_func);
95 compileState.alphaBlendFunc =
96 swr_convert_blend_func(rt_blend->alpha_func);
97 }
98 compileState.logicOpEnable = state->pipe.logicop_enable;
99 if (compileState.logicOpEnable) {
100 compileState.logicOpFunc =
101 swr_convert_logic_op(state->pipe.logicop_func);
102 }
103
104 blendState.writeDisableRed =
105 (rt_blend->colormask & PIPE_MASK_R) ? 0 : 1;
106 blendState.writeDisableGreen =
107 (rt_blend->colormask & PIPE_MASK_G) ? 0 : 1;
108 blendState.writeDisableBlue =
109 (rt_blend->colormask & PIPE_MASK_B) ? 0 : 1;
110 blendState.writeDisableAlpha =
111 (rt_blend->colormask & PIPE_MASK_A) ? 0 : 1;
112
113 if (rt_blend->colormask == 0)
114 compileState.blendEnable = false;
115 }
116
117 return state;
118 }
119
120 static void
121 swr_bind_blend_state(struct pipe_context *pipe, void *blend)
122 {
123 struct swr_context *ctx = swr_context(pipe);
124
125 if (ctx->blend == blend)
126 return;
127
128 ctx->blend = (swr_blend_state *)blend;
129
130 ctx->dirty |= SWR_NEW_BLEND;
131 }
132
133 static void
134 swr_delete_blend_state(struct pipe_context *pipe, void *blend)
135 {
136 FREE(blend);
137 }
138
139 static void
140 swr_set_blend_color(struct pipe_context *pipe,
141 const struct pipe_blend_color *color)
142 {
143 struct swr_context *ctx = swr_context(pipe);
144
145 ctx->blend_color = *color;
146
147 ctx->dirty |= SWR_NEW_BLEND;
148 }
149
150 static void
151 swr_set_stencil_ref(struct pipe_context *pipe,
152 const struct pipe_stencil_ref *ref)
153 {
154 struct swr_context *ctx = swr_context(pipe);
155
156 ctx->stencil_ref = *ref;
157
158 ctx->dirty |= SWR_NEW_DEPTH_STENCIL_ALPHA;
159 }
160
161 static void *
162 swr_create_depth_stencil_state(
163 struct pipe_context *pipe,
164 const struct pipe_depth_stencil_alpha_state *depth_stencil)
165 {
166 struct pipe_depth_stencil_alpha_state *state;
167
168 state = (pipe_depth_stencil_alpha_state *)mem_dup(depth_stencil,
169 sizeof *depth_stencil);
170
171 return state;
172 }
173
174 static void
175 swr_bind_depth_stencil_state(struct pipe_context *pipe, void *depth_stencil)
176 {
177 struct swr_context *ctx = swr_context(pipe);
178
179 if (ctx->depth_stencil == (pipe_depth_stencil_alpha_state *)depth_stencil)
180 return;
181
182 ctx->depth_stencil = (pipe_depth_stencil_alpha_state *)depth_stencil;
183
184 ctx->dirty |= SWR_NEW_DEPTH_STENCIL_ALPHA;
185 }
186
187 static void
188 swr_delete_depth_stencil_state(struct pipe_context *pipe, void *depth)
189 {
190 FREE(depth);
191 }
192
193
194 static void *
195 swr_create_rasterizer_state(struct pipe_context *pipe,
196 const struct pipe_rasterizer_state *rast)
197 {
198 struct pipe_rasterizer_state *state;
199 state = (pipe_rasterizer_state *)mem_dup(rast, sizeof *rast);
200
201 return state;
202 }
203
204 static void
205 swr_bind_rasterizer_state(struct pipe_context *pipe, void *handle)
206 {
207 struct swr_context *ctx = swr_context(pipe);
208 const struct pipe_rasterizer_state *rasterizer =
209 (const struct pipe_rasterizer_state *)handle;
210
211 if (ctx->rasterizer == (pipe_rasterizer_state *)rasterizer)
212 return;
213
214 ctx->rasterizer = (pipe_rasterizer_state *)rasterizer;
215
216 ctx->dirty |= SWR_NEW_RASTERIZER;
217 }
218
219 static void
220 swr_delete_rasterizer_state(struct pipe_context *pipe, void *rasterizer)
221 {
222 FREE(rasterizer);
223 }
224
225
226 static void *
227 swr_create_sampler_state(struct pipe_context *pipe,
228 const struct pipe_sampler_state *sampler)
229 {
230 struct pipe_sampler_state *state =
231 (pipe_sampler_state *)mem_dup(sampler, sizeof *sampler);
232
233 return state;
234 }
235
236 static void
237 swr_bind_sampler_states(struct pipe_context *pipe,
238 enum pipe_shader_type shader,
239 unsigned start,
240 unsigned num,
241 void **samplers)
242 {
243 struct swr_context *ctx = swr_context(pipe);
244 unsigned i;
245
246 assert(shader < PIPE_SHADER_TYPES);
247 assert(start + num <= ARRAY_SIZE(ctx->samplers[shader]));
248
249 /* set the new samplers */
250 ctx->num_samplers[shader] = num;
251 for (i = 0; i < num; i++) {
252 ctx->samplers[shader][start + i] = (pipe_sampler_state *)samplers[i];
253 }
254
255 ctx->dirty |= SWR_NEW_SAMPLER;
256 }
257
258 static void
259 swr_delete_sampler_state(struct pipe_context *pipe, void *sampler)
260 {
261 FREE(sampler);
262 }
263
264
265 static struct pipe_sampler_view *
266 swr_create_sampler_view(struct pipe_context *pipe,
267 struct pipe_resource *texture,
268 const struct pipe_sampler_view *templ)
269 {
270 struct pipe_sampler_view *view = CALLOC_STRUCT(pipe_sampler_view);
271
272 if (view) {
273 *view = *templ;
274 view->reference.count = 1;
275 view->texture = NULL;
276 pipe_resource_reference(&view->texture, texture);
277 view->context = pipe;
278 }
279
280 return view;
281 }
282
283 static void
284 swr_set_sampler_views(struct pipe_context *pipe,
285 enum pipe_shader_type shader,
286 unsigned start,
287 unsigned num,
288 struct pipe_sampler_view **views)
289 {
290 struct swr_context *ctx = swr_context(pipe);
291 uint i;
292
293 assert(num <= PIPE_MAX_SHADER_SAMPLER_VIEWS);
294
295 assert(shader < PIPE_SHADER_TYPES);
296 assert(start + num <= ARRAY_SIZE(ctx->sampler_views[shader]));
297
298 /* set the new sampler views */
299 ctx->num_sampler_views[shader] = num;
300 for (i = 0; i < num; i++) {
301 /* Note: we're using pipe_sampler_view_release() here to work around
302 * a possible crash when the old view belongs to another context that
303 * was already destroyed.
304 */
305 pipe_sampler_view_release(pipe, &ctx->sampler_views[shader][start + i]);
306 pipe_sampler_view_reference(&ctx->sampler_views[shader][start + i],
307 views[i]);
308 }
309
310 ctx->dirty |= SWR_NEW_SAMPLER_VIEW;
311 }
312
313 static void
314 swr_sampler_view_destroy(struct pipe_context *pipe,
315 struct pipe_sampler_view *view)
316 {
317 pipe_resource_reference(&view->texture, NULL);
318 FREE(view);
319 }
320
321 static void *
322 swr_create_vs_state(struct pipe_context *pipe,
323 const struct pipe_shader_state *vs)
324 {
325 struct swr_vertex_shader *swr_vs = new swr_vertex_shader;
326 if (!swr_vs)
327 return NULL;
328
329 swr_vs->pipe.tokens = tgsi_dup_tokens(vs->tokens);
330 swr_vs->pipe.stream_output = vs->stream_output;
331
332 lp_build_tgsi_info(vs->tokens, &swr_vs->info);
333
334 swr_vs->soState = {0};
335
336 if (swr_vs->pipe.stream_output.num_outputs) {
337 pipe_stream_output_info *stream_output = &swr_vs->pipe.stream_output;
338
339 swr_vs->soState.soEnable = true;
340 // soState.rasterizerDisable set on state dirty
341 // soState.streamToRasterizer not used
342
343 for (uint32_t i = 0; i < stream_output->num_outputs; i++) {
344 swr_vs->soState.streamMasks[stream_output->output[i].stream] |=
345 1 << (stream_output->output[i].register_index - 1);
346 }
347 for (uint32_t i = 0; i < MAX_SO_STREAMS; i++) {
348 swr_vs->soState.streamNumEntries[i] =
349 _mm_popcnt_u32(swr_vs->soState.streamMasks[i]);
350 }
351 }
352
353 return swr_vs;
354 }
355
356 static void
357 swr_bind_vs_state(struct pipe_context *pipe, void *vs)
358 {
359 struct swr_context *ctx = swr_context(pipe);
360
361 if (ctx->vs == vs)
362 return;
363
364 ctx->vs = (swr_vertex_shader *)vs;
365 ctx->dirty |= SWR_NEW_VS;
366 }
367
368 static void
369 swr_delete_vs_state(struct pipe_context *pipe, void *vs)
370 {
371 struct swr_vertex_shader *swr_vs = (swr_vertex_shader *)vs;
372 FREE((void *)swr_vs->pipe.tokens);
373 delete swr_vs;
374 }
375
376 static void *
377 swr_create_fs_state(struct pipe_context *pipe,
378 const struct pipe_shader_state *fs)
379 {
380 struct swr_fragment_shader *swr_fs = new swr_fragment_shader;
381 if (!swr_fs)
382 return NULL;
383
384 swr_fs->pipe.tokens = tgsi_dup_tokens(fs->tokens);
385
386 lp_build_tgsi_info(fs->tokens, &swr_fs->info);
387
388 return swr_fs;
389 }
390
391
392 static void
393 swr_bind_fs_state(struct pipe_context *pipe, void *fs)
394 {
395 struct swr_context *ctx = swr_context(pipe);
396
397 if (ctx->fs == fs)
398 return;
399
400 ctx->fs = (swr_fragment_shader *)fs;
401 ctx->dirty |= SWR_NEW_FS;
402 }
403
404 static void
405 swr_delete_fs_state(struct pipe_context *pipe, void *fs)
406 {
407 struct swr_fragment_shader *swr_fs = (swr_fragment_shader *)fs;
408 FREE((void *)swr_fs->pipe.tokens);
409 delete swr_fs;
410 }
411
412
413 static void
414 swr_set_constant_buffer(struct pipe_context *pipe,
415 uint shader,
416 uint index,
417 const struct pipe_constant_buffer *cb)
418 {
419 struct swr_context *ctx = swr_context(pipe);
420 struct pipe_resource *constants = cb ? cb->buffer : NULL;
421
422 assert(shader < PIPE_SHADER_TYPES);
423 assert(index < ARRAY_SIZE(ctx->constants[shader]));
424
425 /* note: reference counting */
426 util_copy_constant_buffer(&ctx->constants[shader][index], cb);
427
428 if (shader == PIPE_SHADER_VERTEX || shader == PIPE_SHADER_GEOMETRY) {
429 ctx->dirty |= SWR_NEW_VSCONSTANTS;
430 } else if (shader == PIPE_SHADER_FRAGMENT) {
431 ctx->dirty |= SWR_NEW_FSCONSTANTS;
432 }
433
434 if (cb && cb->user_buffer) {
435 pipe_resource_reference(&constants, NULL);
436 }
437 }
438
439
440 static void *
441 swr_create_vertex_elements_state(struct pipe_context *pipe,
442 unsigned num_elements,
443 const struct pipe_vertex_element *attribs)
444 {
445 struct swr_vertex_element_state *velems;
446 assert(num_elements <= PIPE_MAX_ATTRIBS);
447 velems = CALLOC_STRUCT(swr_vertex_element_state);
448 if (velems) {
449 velems->fsState.numAttribs = num_elements;
450 for (unsigned i = 0; i < num_elements; i++) {
451 // XXX: we should do this keyed on the VS usage info
452
453 const struct util_format_description *desc =
454 util_format_description(attribs[i].src_format);
455
456 velems->fsState.layout[i].AlignedByteOffset = attribs[i].src_offset;
457 velems->fsState.layout[i].Format =
458 mesa_to_swr_format(attribs[i].src_format);
459 velems->fsState.layout[i].StreamIndex =
460 attribs[i].vertex_buffer_index;
461 velems->fsState.layout[i].InstanceEnable =
462 attribs[i].instance_divisor != 0;
463 velems->fsState.layout[i].ComponentControl0 =
464 desc->channel[0].type != UTIL_FORMAT_TYPE_VOID
465 ? ComponentControl::StoreSrc
466 : ComponentControl::Store0;
467 velems->fsState.layout[i].ComponentControl1 =
468 desc->channel[1].type != UTIL_FORMAT_TYPE_VOID
469 ? ComponentControl::StoreSrc
470 : ComponentControl::Store0;
471 velems->fsState.layout[i].ComponentControl2 =
472 desc->channel[2].type != UTIL_FORMAT_TYPE_VOID
473 ? ComponentControl::StoreSrc
474 : ComponentControl::Store0;
475 velems->fsState.layout[i].ComponentControl3 =
476 desc->channel[3].type != UTIL_FORMAT_TYPE_VOID
477 ? ComponentControl::StoreSrc
478 : ComponentControl::Store1Fp;
479 velems->fsState.layout[i].ComponentPacking = ComponentEnable::XYZW;
480 velems->fsState.layout[i].InstanceDataStepRate =
481 attribs[i].instance_divisor;
482
483 /* Calculate the pitch of each stream */
484 const SWR_FORMAT_INFO &swr_desc = GetFormatInfo(
485 mesa_to_swr_format(attribs[i].src_format));
486 velems->stream_pitch[attribs[i].vertex_buffer_index] += swr_desc.Bpp;
487 }
488 }
489
490 return velems;
491 }
492
493 static void
494 swr_bind_vertex_elements_state(struct pipe_context *pipe, void *velems)
495 {
496 struct swr_context *ctx = swr_context(pipe);
497 struct swr_vertex_element_state *swr_velems =
498 (struct swr_vertex_element_state *)velems;
499
500 ctx->velems = swr_velems;
501 ctx->dirty |= SWR_NEW_VERTEX;
502 }
503
504 static void
505 swr_delete_vertex_elements_state(struct pipe_context *pipe, void *velems)
506 {
507 /* XXX Need to destroy fetch shader? */
508 FREE(velems);
509 }
510
511
512 static void
513 swr_set_vertex_buffers(struct pipe_context *pipe,
514 unsigned start_slot,
515 unsigned num_elements,
516 const struct pipe_vertex_buffer *buffers)
517 {
518 struct swr_context *ctx = swr_context(pipe);
519
520 assert(num_elements <= PIPE_MAX_ATTRIBS);
521
522 util_set_vertex_buffers_count(ctx->vertex_buffer,
523 &ctx->num_vertex_buffers,
524 buffers,
525 start_slot,
526 num_elements);
527
528 ctx->dirty |= SWR_NEW_VERTEX;
529 }
530
531
532 static void
533 swr_set_index_buffer(struct pipe_context *pipe,
534 const struct pipe_index_buffer *ib)
535 {
536 struct swr_context *ctx = swr_context(pipe);
537
538 if (ib)
539 memcpy(&ctx->index_buffer, ib, sizeof(ctx->index_buffer));
540 else
541 memset(&ctx->index_buffer, 0, sizeof(ctx->index_buffer));
542
543 ctx->dirty |= SWR_NEW_VERTEX;
544 }
545
546 static void
547 swr_set_polygon_stipple(struct pipe_context *pipe,
548 const struct pipe_poly_stipple *stipple)
549 {
550 struct swr_context *ctx = swr_context(pipe);
551
552 ctx->poly_stipple = *stipple; /* struct copy */
553 ctx->dirty |= SWR_NEW_STIPPLE;
554 }
555
556 static void
557 swr_set_clip_state(struct pipe_context *pipe,
558 const struct pipe_clip_state *clip)
559 {
560 struct swr_context *ctx = swr_context(pipe);
561
562 ctx->clip = *clip;
563 /* XXX Unimplemented, but prevents crash */
564
565 ctx->dirty |= SWR_NEW_CLIP;
566 }
567
568
569 static void
570 swr_set_scissor_states(struct pipe_context *pipe,
571 unsigned start_slot,
572 unsigned num_viewports,
573 const struct pipe_scissor_state *scissor)
574 {
575 struct swr_context *ctx = swr_context(pipe);
576
577 ctx->scissor = *scissor;
578 ctx->swr_scissor.xmin = scissor->minx;
579 ctx->swr_scissor.xmax = scissor->maxx;
580 ctx->swr_scissor.ymin = scissor->miny;
581 ctx->swr_scissor.ymax = scissor->maxy;
582 ctx->dirty |= SWR_NEW_SCISSOR;
583 }
584
585 static void
586 swr_set_viewport_states(struct pipe_context *pipe,
587 unsigned start_slot,
588 unsigned num_viewports,
589 const struct pipe_viewport_state *vpt)
590 {
591 struct swr_context *ctx = swr_context(pipe);
592
593 ctx->viewport = *vpt;
594 ctx->dirty |= SWR_NEW_VIEWPORT;
595 }
596
597
598 static void
599 swr_set_framebuffer_state(struct pipe_context *pipe,
600 const struct pipe_framebuffer_state *fb)
601 {
602 struct swr_context *ctx = swr_context(pipe);
603
604 boolean changed = !util_framebuffer_state_equal(&ctx->framebuffer, fb);
605
606 assert(fb->width <= KNOB_GUARDBAND_WIDTH);
607 assert(fb->height <= KNOB_GUARDBAND_HEIGHT);
608
609 if (changed) {
610 unsigned i;
611 for (i = 0; i < fb->nr_cbufs; ++i)
612 pipe_surface_reference(&ctx->framebuffer.cbufs[i], fb->cbufs[i]);
613 for (; i < ctx->framebuffer.nr_cbufs; ++i)
614 pipe_surface_reference(&ctx->framebuffer.cbufs[i], NULL);
615
616 ctx->framebuffer.nr_cbufs = fb->nr_cbufs;
617
618 ctx->framebuffer.width = fb->width;
619 ctx->framebuffer.height = fb->height;
620
621 pipe_surface_reference(&ctx->framebuffer.zsbuf, fb->zsbuf);
622
623 ctx->dirty |= SWR_NEW_FRAMEBUFFER;
624 }
625 }
626
627
628 static void
629 swr_set_sample_mask(struct pipe_context *pipe, unsigned sample_mask)
630 {
631 struct swr_context *ctx = swr_context(pipe);
632
633 if (sample_mask != ctx->sample_mask) {
634 ctx->sample_mask = sample_mask;
635 ctx->dirty |= SWR_NEW_RASTERIZER;
636 }
637 }
638
639 /*
640 * Update resource in-use status
641 * All resources bound to color or depth targets marked as WRITE resources.
642 * VBO Vertex/index buffers and texture views marked as READ resources.
643 */
644 void
645 swr_update_resource_status(struct pipe_context *pipe,
646 const struct pipe_draw_info *p_draw_info)
647 {
648 struct swr_context *ctx = swr_context(pipe);
649 struct pipe_framebuffer_state *fb = &ctx->framebuffer;
650
651 /* colorbuffer targets */
652 if (fb->nr_cbufs)
653 for (uint32_t i = 0; i < fb->nr_cbufs; ++i)
654 if (fb->cbufs[i])
655 swr_resource_write(fb->cbufs[i]->texture);
656
657 /* depth/stencil target */
658 if (fb->zsbuf)
659 swr_resource_write(fb->zsbuf->texture);
660
661 /* VBO vertex buffers */
662 for (uint32_t i = 0; i < ctx->num_vertex_buffers; i++) {
663 struct pipe_vertex_buffer *vb = &ctx->vertex_buffer[i];
664 if (!vb->user_buffer)
665 swr_resource_read(vb->buffer);
666 }
667
668 /* VBO index buffer */
669 if (p_draw_info && p_draw_info->indexed) {
670 struct pipe_index_buffer *ib = &ctx->index_buffer;
671 if (!ib->user_buffer)
672 swr_resource_read(ib->buffer);
673 }
674
675 /* texture sampler views */
676 for (uint32_t i = 0; i < PIPE_MAX_SHADER_SAMPLER_VIEWS; i++) {
677 struct pipe_sampler_view *view =
678 ctx->sampler_views[PIPE_SHADER_FRAGMENT][i];
679 if (view)
680 swr_resource_read(view->texture);
681 }
682 }
683
684 static void
685 swr_update_texture_state(struct swr_context *ctx,
686 unsigned shader_type,
687 unsigned num_sampler_views,
688 swr_jit_texture *textures)
689 {
690 for (unsigned i = 0; i < num_sampler_views; i++) {
691 struct pipe_sampler_view *view =
692 ctx->sampler_views[shader_type][i];
693
694 if (view) {
695 struct pipe_resource *res = view->texture;
696 struct swr_resource *swr_res = swr_resource(res);
697 struct swr_jit_texture *jit_tex = &textures[i];
698 memset(jit_tex, 0, sizeof(*jit_tex));
699 jit_tex->width = res->width0;
700 jit_tex->height = res->height0;
701 jit_tex->depth = res->depth0;
702 jit_tex->first_level = view->u.tex.first_level;
703 jit_tex->last_level = view->u.tex.last_level;
704 jit_tex->base_ptr = swr_res->swr.pBaseAddress;
705
706 for (unsigned level = jit_tex->first_level;
707 level <= jit_tex->last_level;
708 level++) {
709 jit_tex->row_stride[level] = swr_res->row_stride[level];
710 jit_tex->img_stride[level] = swr_res->img_stride[level];
711 jit_tex->mip_offsets[level] = swr_res->mip_offsets[level];
712 }
713 }
714 }
715 }
716
717 static void
718 swr_update_sampler_state(struct swr_context *ctx,
719 unsigned shader_type,
720 unsigned num_samplers,
721 swr_jit_sampler *samplers)
722 {
723 for (unsigned i = 0; i < num_samplers; i++) {
724 const struct pipe_sampler_state *sampler =
725 ctx->samplers[shader_type][i];
726
727 if (sampler) {
728 samplers[i].min_lod = sampler->min_lod;
729 samplers[i].max_lod = sampler->max_lod;
730 samplers[i].lod_bias = sampler->lod_bias;
731 COPY_4V(samplers[i].border_color, sampler->border_color.f);
732 }
733 }
734 }
735
736 static void
737 swr_update_constants(struct swr_context *ctx, enum pipe_shader_type shaderType)
738 {
739 swr_draw_context *pDC = &ctx->swrDC;
740
741 const float **constant;
742 uint32_t *num_constants;
743 struct swr_scratch_space *scratch;
744
745 switch (shaderType) {
746 case PIPE_SHADER_VERTEX:
747 constant = pDC->constantVS;
748 num_constants = pDC->num_constantsVS;
749 scratch = &ctx->scratch->vs_constants;
750 break;
751 case PIPE_SHADER_FRAGMENT:
752 constant = pDC->constantFS;
753 num_constants = pDC->num_constantsFS;
754 scratch = &ctx->scratch->fs_constants;
755 break;
756 default:
757 debug_printf("Unsupported shader type constants\n");
758 return;
759 }
760
761 for (UINT i = 0; i < PIPE_MAX_CONSTANT_BUFFERS; i++) {
762 const pipe_constant_buffer *cb = &ctx->constants[shaderType][i];
763 num_constants[i] = cb->buffer_size;
764 if (cb->buffer) {
765 constant[i] =
766 (const float *)(swr_resource_data(cb->buffer) +
767 cb->buffer_offset);
768 } else {
769 /* Need to copy these constants to scratch space */
770 if (cb->user_buffer && cb->buffer_size) {
771 const void *ptr =
772 ((const uint8_t *)cb->user_buffer + cb->buffer_offset);
773 uint32_t size = AlignUp(cb->buffer_size, 4);
774 ptr = swr_copy_to_scratch_space(ctx, scratch, ptr, size);
775 constant[i] = (const float *)ptr;
776 }
777 }
778 }
779 }
780
781 void
782 swr_update_derived(struct pipe_context *pipe,
783 const struct pipe_draw_info *p_draw_info)
784 {
785 struct swr_context *ctx = swr_context(pipe);
786 struct swr_screen *screen = swr_screen(ctx->pipe.screen);
787
788 /* Update screen->pipe to current pipe context. */
789 if (screen->pipe != pipe)
790 screen->pipe = pipe;
791
792 /* Any state that requires dirty flags to be re-triggered sets this mask */
793 /* For example, user_buffer vertex and index buffers. */
794 unsigned post_update_dirty_flags = 0;
795
796 /* Render Targets */
797 if (ctx->dirty & SWR_NEW_FRAMEBUFFER) {
798 struct pipe_framebuffer_state *fb = &ctx->framebuffer;
799 SWR_SURFACE_STATE *new_attachment[SWR_NUM_ATTACHMENTS] = {0};
800 UINT i;
801
802 /* colorbuffer targets */
803 if (fb->nr_cbufs)
804 for (i = 0; i < fb->nr_cbufs; ++i)
805 if (fb->cbufs[i]) {
806 struct swr_resource *colorBuffer =
807 swr_resource(fb->cbufs[i]->texture);
808 new_attachment[SWR_ATTACHMENT_COLOR0 + i] = &colorBuffer->swr;
809 }
810
811 /* depth/stencil target */
812 if (fb->zsbuf) {
813 struct swr_resource *depthStencilBuffer =
814 swr_resource(fb->zsbuf->texture);
815 if (depthStencilBuffer->has_depth) {
816 new_attachment[SWR_ATTACHMENT_DEPTH] = &depthStencilBuffer->swr;
817
818 if (depthStencilBuffer->has_stencil)
819 new_attachment[SWR_ATTACHMENT_STENCIL] =
820 &depthStencilBuffer->secondary;
821
822 } else if (depthStencilBuffer->has_stencil)
823 new_attachment[SWR_ATTACHMENT_STENCIL] = &depthStencilBuffer->swr;
824 }
825
826 /* Make the attachment updates */
827 swr_draw_context *pDC = &ctx->swrDC;
828 SWR_SURFACE_STATE *renderTargets = pDC->renderTargets;
829 unsigned need_fence = FALSE;
830 for (i = 0; i < SWR_NUM_ATTACHMENTS; i++) {
831 void *new_base = nullptr;
832 if (new_attachment[i])
833 new_base = new_attachment[i]->pBaseAddress;
834
835 /* StoreTile for changed target */
836 if (renderTargets[i].pBaseAddress != new_base) {
837 if (renderTargets[i].pBaseAddress) {
838 /* If changing attachment to a new target, mark tiles as
839 * INVALID so they are reloaded from surface.
840 * If detaching attachment, mark tiles as RESOLVED so core
841 * won't try to load from non-existent target. */
842 enum SWR_TILE_STATE post_state = (new_attachment[i]
843 ? SWR_TILE_INVALID : SWR_TILE_RESOLVED);
844 swr_store_render_target(pipe, i, post_state);
845
846 need_fence |= TRUE;
847 }
848
849 /* Make new attachment */
850 if (new_attachment[i])
851 renderTargets[i] = *new_attachment[i];
852 else
853 if (renderTargets[i].pBaseAddress)
854 renderTargets[i] = {0};
855 }
856 }
857
858 /* This fence ensures any attachment changes are resolved before the
859 * next draw */
860 if (need_fence)
861 swr_fence_submit(ctx, screen->flush_fence);
862 }
863
864 /* Raster state */
865 if (ctx->dirty & (SWR_NEW_RASTERIZER |
866 SWR_NEW_VS | // clipping
867 SWR_NEW_FRAMEBUFFER)) {
868 pipe_rasterizer_state *rasterizer = ctx->rasterizer;
869 pipe_framebuffer_state *fb = &ctx->framebuffer;
870
871 SWR_RASTSTATE *rastState = &ctx->derived.rastState;
872 rastState->cullMode = swr_convert_cull_mode(rasterizer->cull_face);
873 rastState->frontWinding = rasterizer->front_ccw
874 ? SWR_FRONTWINDING_CCW
875 : SWR_FRONTWINDING_CW;
876 rastState->scissorEnable = rasterizer->scissor;
877 rastState->pointSize = rasterizer->point_size > 0.0f
878 ? rasterizer->point_size
879 : 1.0f;
880 rastState->lineWidth = rasterizer->line_width > 0.0f
881 ? rasterizer->line_width
882 : 1.0f;
883
884 rastState->pointParam = rasterizer->point_size_per_vertex;
885
886 rastState->pointSpriteEnable = rasterizer->sprite_coord_enable;
887 rastState->pointSpriteTopOrigin =
888 rasterizer->sprite_coord_mode == PIPE_SPRITE_COORD_UPPER_LEFT;
889
890 /* XXX TODO: Add multisample */
891 rastState->msaaRastEnable = false;
892 rastState->rastMode = SWR_MSAA_RASTMODE_OFF_PIXEL;
893 rastState->sampleCount = SWR_MULTISAMPLE_1X;
894 rastState->forcedSampleCount = false;
895
896 bool do_offset = false;
897 switch (rasterizer->fill_front) {
898 case PIPE_POLYGON_MODE_FILL:
899 do_offset = rasterizer->offset_tri;
900 break;
901 case PIPE_POLYGON_MODE_LINE:
902 do_offset = rasterizer->offset_line;
903 break;
904 case PIPE_POLYGON_MODE_POINT:
905 do_offset = rasterizer->offset_point;
906 break;
907 }
908
909 if (do_offset) {
910 rastState->depthBias = rasterizer->offset_units;
911 rastState->slopeScaledDepthBias = rasterizer->offset_scale;
912 rastState->depthBiasClamp = rasterizer->offset_clamp;
913 } else {
914 rastState->depthBias = 0;
915 rastState->slopeScaledDepthBias = 0;
916 rastState->depthBiasClamp = 0;
917 }
918 struct pipe_surface *zb = fb->zsbuf;
919 if (zb && swr_resource(zb->texture)->has_depth)
920 rastState->depthFormat = swr_resource(zb->texture)->swr.format;
921
922 rastState->depthClipEnable = rasterizer->depth_clip;
923
924 rastState->clipDistanceMask =
925 ctx->vs->info.base.num_written_clipdistance ?
926 ctx->vs->info.base.clipdist_writemask & rasterizer->clip_plane_enable :
927 rasterizer->clip_plane_enable;
928
929 rastState->cullDistanceMask =
930 ctx->vs->info.base.culldist_writemask << ctx->vs->info.base.num_written_clipdistance;
931
932 SwrSetRastState(ctx->swrContext, rastState);
933 }
934
935 /* Scissor */
936 if (ctx->dirty & SWR_NEW_SCISSOR) {
937 SwrSetScissorRects(ctx->swrContext, 1, &ctx->swr_scissor);
938 }
939
940 /* Viewport */
941 if (ctx->dirty & (SWR_NEW_VIEWPORT | SWR_NEW_FRAMEBUFFER
942 | SWR_NEW_RASTERIZER)) {
943 pipe_viewport_state *state = &ctx->viewport;
944 pipe_framebuffer_state *fb = &ctx->framebuffer;
945 pipe_rasterizer_state *rasterizer = ctx->rasterizer;
946
947 SWR_VIEWPORT *vp = &ctx->derived.vp;
948 SWR_VIEWPORT_MATRICES *vpm = &ctx->derived.vpm;
949
950 vp->x = state->translate[0] - state->scale[0];
951 vp->width = state->translate[0] + state->scale[0];
952 vp->y = state->translate[1] - fabs(state->scale[1]);
953 vp->height = state->translate[1] + fabs(state->scale[1]);
954 if (rasterizer->clip_halfz == 0) {
955 vp->minZ = state->translate[2] - state->scale[2];
956 vp->maxZ = state->translate[2] + state->scale[2];
957 } else {
958 vp->minZ = state->translate[2];
959 vp->maxZ = state->translate[2] + state->scale[2];
960 }
961
962 vpm->m00[0] = state->scale[0];
963 vpm->m11[0] = state->scale[1];
964 vpm->m22[0] = state->scale[2];
965 vpm->m30[0] = state->translate[0];
966 vpm->m31[0] = state->translate[1];
967 vpm->m32[0] = state->translate[2];
968
969 /* Now that the matrix is calculated, clip the view coords to screen
970 * size. OpenGL allows for -ve x,y in the viewport. */
971 vp->x = std::max(vp->x, 0.0f);
972 vp->y = std::max(vp->y, 0.0f);
973 vp->width = std::min(vp->width, (float)fb->width);
974 vp->height = std::min(vp->height, (float)fb->height);
975
976 SwrSetViewports(ctx->swrContext, 1, vp, vpm);
977 }
978
979 /* Set vertex & index buffers */
980 /* (using draw info if called by swr_draw_vbo) */
981 if (ctx->dirty & SWR_NEW_VERTEX) {
982 uint32_t size, pitch, max_vertex, partial_inbounds;
983 const uint8_t *p_data;
984
985 /* If being called by swr_draw_vbo, copy draw details */
986 struct pipe_draw_info info = {0};
987 if (p_draw_info)
988 info = *p_draw_info;
989
990 /* vertex buffers */
991 SWR_VERTEX_BUFFER_STATE swrVertexBuffers[PIPE_MAX_ATTRIBS];
992 for (UINT i = 0; i < ctx->num_vertex_buffers; i++) {
993 struct pipe_vertex_buffer *vb = &ctx->vertex_buffer[i];
994
995 pitch = vb->stride;
996 if (!vb->user_buffer) {
997 /* VBO
998 * size is based on buffer->width0 rather than info.max_index
999 * to prevent having to validate VBO on each draw */
1000 size = vb->buffer->width0;
1001 max_vertex = size / pitch;
1002 partial_inbounds = size % pitch;
1003
1004 p_data = swr_resource_data(vb->buffer) + vb->buffer_offset;
1005 } else {
1006 /* Client buffer
1007 * client memory is one-time use, re-trigger SWR_NEW_VERTEX to
1008 * revalidate on each draw */
1009 post_update_dirty_flags |= SWR_NEW_VERTEX;
1010
1011 if (pitch) {
1012 size = (info.max_index - info.min_index + 1) * pitch;
1013 } else {
1014 /* pitch = 0, means constant value
1015 * set size to 1 vertex */
1016 size = ctx->velems->stream_pitch[i];
1017 }
1018
1019 max_vertex = info.max_index + 1;
1020 partial_inbounds = 0;
1021
1022 /* Copy only needed vertices to scratch space */
1023 size = AlignUp(size, 4);
1024 const void *ptr = (const uint8_t *) vb->user_buffer
1025 + info.min_index * pitch;
1026 ptr = swr_copy_to_scratch_space(
1027 ctx, &ctx->scratch->vertex_buffer, ptr, size);
1028 p_data = (const uint8_t *)ptr - info.min_index * pitch;
1029 }
1030
1031 swrVertexBuffers[i] = {0};
1032 swrVertexBuffers[i].index = i;
1033 swrVertexBuffers[i].pitch = pitch;
1034 swrVertexBuffers[i].pData = p_data;
1035 swrVertexBuffers[i].size = size;
1036 swrVertexBuffers[i].maxVertex = max_vertex;
1037 swrVertexBuffers[i].partialInboundsSize = partial_inbounds;
1038 }
1039
1040 SwrSetVertexBuffers(
1041 ctx->swrContext, ctx->num_vertex_buffers, swrVertexBuffers);
1042
1043 /* index buffer, if required (info passed in by swr_draw_vbo) */
1044 SWR_FORMAT index_type = R32_UINT; /* Default for non-indexed draws */
1045 if (info.indexed) {
1046 struct pipe_index_buffer *ib = &ctx->index_buffer;
1047
1048 pitch = ib->index_size ? ib->index_size : sizeof(uint32_t);
1049 index_type = swr_convert_index_type(pitch);
1050
1051 if (!ib->user_buffer) {
1052 /* VBO
1053 * size is based on buffer->width0 rather than info.count
1054 * to prevent having to validate VBO on each draw */
1055 size = ib->buffer->width0;
1056 p_data = swr_resource_data(ib->buffer) + ib->offset;
1057 } else {
1058 /* Client buffer
1059 * client memory is one-time use, re-trigger SWR_NEW_VERTEX to
1060 * revalidate on each draw */
1061 post_update_dirty_flags |= SWR_NEW_VERTEX;
1062
1063 size = info.count * pitch;
1064 size = AlignUp(size, 4);
1065
1066 /* Copy indices to scratch space */
1067 const void *ptr = ib->user_buffer;
1068 ptr = swr_copy_to_scratch_space(
1069 ctx, &ctx->scratch->index_buffer, ptr, size);
1070 p_data = (const uint8_t *)ptr;
1071 }
1072
1073 SWR_INDEX_BUFFER_STATE swrIndexBuffer;
1074 swrIndexBuffer.format = swr_convert_index_type(ib->index_size);
1075 swrIndexBuffer.pIndices = p_data;
1076 swrIndexBuffer.size = size;
1077
1078 SwrSetIndexBuffer(ctx->swrContext, &swrIndexBuffer);
1079 }
1080
1081 struct swr_vertex_element_state *velems = ctx->velems;
1082 if (velems && velems->fsState.indexType != index_type) {
1083 velems->fsFunc = NULL;
1084 velems->fsState.indexType = index_type;
1085 }
1086 }
1087
1088 /* VertexShader */
1089 if (ctx->dirty & (SWR_NEW_VS |
1090 SWR_NEW_RASTERIZER | // for clip planes
1091 SWR_NEW_SAMPLER |
1092 SWR_NEW_SAMPLER_VIEW |
1093 SWR_NEW_FRAMEBUFFER)) {
1094 swr_jit_vs_key key;
1095 swr_generate_vs_key(key, ctx, ctx->vs);
1096 auto search = ctx->vs->map.find(key);
1097 PFN_VERTEX_FUNC func;
1098 if (search != ctx->vs->map.end()) {
1099 func = search->second->shader;
1100 } else {
1101 func = swr_compile_vs(ctx, key);
1102 }
1103 SwrSetVertexFunc(ctx->swrContext, func);
1104
1105 /* JIT sampler state */
1106 if (ctx->dirty & SWR_NEW_SAMPLER) {
1107 swr_update_sampler_state(ctx,
1108 PIPE_SHADER_VERTEX,
1109 key.nr_samplers,
1110 ctx->swrDC.samplersVS);
1111 }
1112
1113 /* JIT sampler view state */
1114 if (ctx->dirty & (SWR_NEW_SAMPLER_VIEW | SWR_NEW_FRAMEBUFFER)) {
1115 swr_update_texture_state(ctx,
1116 PIPE_SHADER_VERTEX,
1117 key.nr_sampler_views,
1118 ctx->swrDC.texturesVS);
1119 }
1120 }
1121
1122 /* FragmentShader */
1123 if (ctx->dirty & (SWR_NEW_FS | SWR_NEW_SAMPLER | SWR_NEW_SAMPLER_VIEW
1124 | SWR_NEW_RASTERIZER | SWR_NEW_FRAMEBUFFER)) {
1125 swr_jit_fs_key key;
1126 swr_generate_fs_key(key, ctx, ctx->fs);
1127 auto search = ctx->fs->map.find(key);
1128 PFN_PIXEL_KERNEL func;
1129 if (search != ctx->fs->map.end()) {
1130 func = search->second->shader;
1131 } else {
1132 func = swr_compile_fs(ctx, key);
1133 }
1134 SWR_PS_STATE psState = {0};
1135 psState.pfnPixelShader = func;
1136 psState.killsPixel = ctx->fs->info.base.uses_kill;
1137 psState.inputCoverage = SWR_INPUT_COVERAGE_NORMAL;
1138 psState.writesODepth = ctx->fs->info.base.writes_z;
1139 psState.usesSourceDepth = ctx->fs->info.base.reads_z;
1140 psState.shadingRate = SWR_SHADING_RATE_PIXEL; // XXX
1141 psState.numRenderTargets = ctx->framebuffer.nr_cbufs;
1142 psState.posOffset = SWR_PS_POSITION_SAMPLE_NONE; // XXX msaa
1143 uint32_t barycentricsMask = 0;
1144 #if 0
1145 // when we switch to mesa-master
1146 if (ctx->fs->info.base.uses_persp_center ||
1147 ctx->fs->info.base.uses_linear_center)
1148 barycentricsMask |= SWR_BARYCENTRIC_PER_PIXEL_MASK;
1149 if (ctx->fs->info.base.uses_persp_centroid ||
1150 ctx->fs->info.base.uses_linear_centroid)
1151 barycentricsMask |= SWR_BARYCENTRIC_CENTROID_MASK;
1152 if (ctx->fs->info.base.uses_persp_sample ||
1153 ctx->fs->info.base.uses_linear_sample)
1154 barycentricsMask |= SWR_BARYCENTRIC_PER_SAMPLE_MASK;
1155 #else
1156 for (unsigned i = 0; i < ctx->fs->info.base.num_inputs; i++) {
1157 switch (ctx->fs->info.base.input_interpolate_loc[i]) {
1158 case TGSI_INTERPOLATE_LOC_CENTER:
1159 barycentricsMask |= SWR_BARYCENTRIC_PER_PIXEL_MASK;
1160 break;
1161 case TGSI_INTERPOLATE_LOC_CENTROID:
1162 barycentricsMask |= SWR_BARYCENTRIC_CENTROID_MASK;
1163 break;
1164 case TGSI_INTERPOLATE_LOC_SAMPLE:
1165 barycentricsMask |= SWR_BARYCENTRIC_PER_SAMPLE_MASK;
1166 break;
1167 }
1168 }
1169 #endif
1170 psState.barycentricsMask = barycentricsMask;
1171 psState.usesUAV = false; // XXX
1172 psState.forceEarlyZ = false;
1173 SwrSetPixelShaderState(ctx->swrContext, &psState);
1174
1175 /* JIT sampler state */
1176 if (ctx->dirty & SWR_NEW_SAMPLER) {
1177 swr_update_sampler_state(ctx,
1178 PIPE_SHADER_FRAGMENT,
1179 key.nr_samplers,
1180 ctx->swrDC.samplersFS);
1181 }
1182
1183 /* JIT sampler view state */
1184 if (ctx->dirty & (SWR_NEW_SAMPLER_VIEW | SWR_NEW_FRAMEBUFFER)) {
1185 swr_update_texture_state(ctx,
1186 PIPE_SHADER_FRAGMENT,
1187 key.nr_sampler_views,
1188 ctx->swrDC.texturesFS);
1189 }
1190 }
1191
1192
1193 /* VertexShader Constants */
1194 if (ctx->dirty & SWR_NEW_VSCONSTANTS) {
1195 swr_update_constants(ctx, PIPE_SHADER_VERTEX);
1196 }
1197
1198 /* FragmentShader Constants */
1199 if (ctx->dirty & SWR_NEW_FSCONSTANTS) {
1200 swr_update_constants(ctx, PIPE_SHADER_FRAGMENT);
1201 }
1202
1203 /* Depth/stencil state */
1204 if (ctx->dirty & (SWR_NEW_DEPTH_STENCIL_ALPHA | SWR_NEW_FRAMEBUFFER)) {
1205 struct pipe_depth_state *depth = &(ctx->depth_stencil->depth);
1206 struct pipe_stencil_state *stencil = ctx->depth_stencil->stencil;
1207 SWR_DEPTH_STENCIL_STATE depthStencilState = {{0}};
1208
1209 /* XXX, incomplete. Need to flesh out stencil & alpha test state
1210 struct pipe_stencil_state *front_stencil =
1211 ctx->depth_stencil.stencil[0];
1212 struct pipe_stencil_state *back_stencil = ctx->depth_stencil.stencil[1];
1213 struct pipe_alpha_state alpha;
1214 */
1215 if (stencil[0].enabled) {
1216 depthStencilState.stencilWriteEnable = 1;
1217 depthStencilState.stencilTestEnable = 1;
1218 depthStencilState.stencilTestFunc =
1219 swr_convert_depth_func(stencil[0].func);
1220
1221 depthStencilState.stencilPassDepthPassOp =
1222 swr_convert_stencil_op(stencil[0].zpass_op);
1223 depthStencilState.stencilPassDepthFailOp =
1224 swr_convert_stencil_op(stencil[0].zfail_op);
1225 depthStencilState.stencilFailOp =
1226 swr_convert_stencil_op(stencil[0].fail_op);
1227 depthStencilState.stencilWriteMask = stencil[0].writemask;
1228 depthStencilState.stencilTestMask = stencil[0].valuemask;
1229 depthStencilState.stencilRefValue = ctx->stencil_ref.ref_value[0];
1230 }
1231 if (stencil[1].enabled) {
1232 depthStencilState.doubleSidedStencilTestEnable = 1;
1233
1234 depthStencilState.backfaceStencilTestFunc =
1235 swr_convert_depth_func(stencil[1].func);
1236
1237 depthStencilState.backfaceStencilPassDepthPassOp =
1238 swr_convert_stencil_op(stencil[1].zpass_op);
1239 depthStencilState.backfaceStencilPassDepthFailOp =
1240 swr_convert_stencil_op(stencil[1].zfail_op);
1241 depthStencilState.backfaceStencilFailOp =
1242 swr_convert_stencil_op(stencil[1].fail_op);
1243 depthStencilState.backfaceStencilWriteMask = stencil[1].writemask;
1244 depthStencilState.backfaceStencilTestMask = stencil[1].valuemask;
1245
1246 depthStencilState.backfaceStencilRefValue =
1247 ctx->stencil_ref.ref_value[1];
1248 }
1249
1250 depthStencilState.depthTestEnable = depth->enabled;
1251 depthStencilState.depthTestFunc = swr_convert_depth_func(depth->func);
1252 depthStencilState.depthWriteEnable = depth->writemask;
1253 SwrSetDepthStencilState(ctx->swrContext, &depthStencilState);
1254 }
1255
1256 /* Blend State */
1257 if (ctx->dirty & (SWR_NEW_BLEND |
1258 SWR_NEW_FRAMEBUFFER |
1259 SWR_NEW_DEPTH_STENCIL_ALPHA)) {
1260 struct pipe_framebuffer_state *fb = &ctx->framebuffer;
1261
1262 SWR_BLEND_STATE blendState;
1263 memcpy(&blendState, &ctx->blend->blendState, sizeof(blendState));
1264 blendState.constantColor[0] = ctx->blend_color.color[0];
1265 blendState.constantColor[1] = ctx->blend_color.color[1];
1266 blendState.constantColor[2] = ctx->blend_color.color[2];
1267 blendState.constantColor[3] = ctx->blend_color.color[3];
1268 blendState.alphaTestReference =
1269 *((uint32_t*)&ctx->depth_stencil->alpha.ref_value);
1270
1271 // XXX MSAA
1272 blendState.sampleMask = 0;
1273 blendState.sampleCount = SWR_MULTISAMPLE_1X;
1274
1275 /* If there are no color buffers bound, disable writes on RT0
1276 * and skip loop */
1277 if (fb->nr_cbufs == 0) {
1278 blendState.renderTarget[0].writeDisableRed = 1;
1279 blendState.renderTarget[0].writeDisableGreen = 1;
1280 blendState.renderTarget[0].writeDisableBlue = 1;
1281 blendState.renderTarget[0].writeDisableAlpha = 1;
1282 SwrSetBlendFunc(ctx->swrContext, 0, NULL);
1283 }
1284 else
1285 for (int target = 0;
1286 target < std::min(SWR_NUM_RENDERTARGETS,
1287 PIPE_MAX_COLOR_BUFS);
1288 target++) {
1289 if (!fb->cbufs[target])
1290 continue;
1291
1292 struct swr_resource *colorBuffer =
1293 swr_resource(fb->cbufs[target]->texture);
1294
1295 BLEND_COMPILE_STATE compileState;
1296 memset(&compileState, 0, sizeof(compileState));
1297 compileState.format = colorBuffer->swr.format;
1298 memcpy(&compileState.blendState,
1299 &ctx->blend->compileState[target],
1300 sizeof(compileState.blendState));
1301
1302 if (compileState.blendState.blendEnable == false &&
1303 compileState.blendState.logicOpEnable == false) {
1304 SwrSetBlendFunc(ctx->swrContext, target, NULL);
1305 continue;
1306 }
1307
1308 compileState.desc.alphaTestEnable =
1309 ctx->depth_stencil->alpha.enabled;
1310 compileState.desc.independentAlphaBlendEnable =
1311 ctx->blend->pipe.independent_blend_enable;
1312 compileState.desc.alphaToCoverageEnable =
1313 ctx->blend->pipe.alpha_to_coverage;
1314 compileState.desc.sampleMaskEnable = 0; // XXX
1315 compileState.desc.numSamples = 1; // XXX
1316
1317 compileState.alphaTestFunction =
1318 swr_convert_depth_func(ctx->depth_stencil->alpha.func);
1319 compileState.alphaTestFormat = ALPHA_TEST_FLOAT32; // xxx
1320
1321 compileState.Canonicalize();
1322
1323 PFN_BLEND_JIT_FUNC func = NULL;
1324 auto search = ctx->blendJIT->find(compileState);
1325 if (search != ctx->blendJIT->end()) {
1326 func = search->second;
1327 } else {
1328 HANDLE hJitMgr = screen->hJitMgr;
1329 func = JitCompileBlend(hJitMgr, compileState);
1330 debug_printf("BLEND shader %p\n", func);
1331 assert(func && "Error: BlendShader = NULL");
1332
1333 ctx->blendJIT->insert(std::make_pair(compileState, func));
1334 }
1335 SwrSetBlendFunc(ctx->swrContext, target, func);
1336 }
1337
1338 SwrSetBlendState(ctx->swrContext, &blendState);
1339 }
1340
1341 if (ctx->dirty & SWR_NEW_STIPPLE) {
1342 /* XXX What to do with this one??? SWR doesn't stipple */
1343 }
1344
1345 if (ctx->dirty & (SWR_NEW_VS | SWR_NEW_SO | SWR_NEW_RASTERIZER)) {
1346 ctx->vs->soState.rasterizerDisable =
1347 ctx->rasterizer->rasterizer_discard;
1348 SwrSetSoState(ctx->swrContext, &ctx->vs->soState);
1349
1350 pipe_stream_output_info *stream_output = &ctx->vs->pipe.stream_output;
1351
1352 for (uint32_t i = 0; i < ctx->num_so_targets; i++) {
1353 SWR_STREAMOUT_BUFFER buffer = {0};
1354 if (!ctx->so_targets[i])
1355 continue;
1356 buffer.enable = true;
1357 buffer.pBuffer =
1358 (uint32_t *)swr_resource_data(ctx->so_targets[i]->buffer);
1359 buffer.bufferSize = ctx->so_targets[i]->buffer_size >> 2;
1360 buffer.pitch = stream_output->stride[i];
1361 buffer.streamOffset = ctx->so_targets[i]->buffer_offset >> 2;
1362
1363 SwrSetSoBuffers(ctx->swrContext, &buffer, i);
1364 }
1365 }
1366
1367 if (ctx->dirty & SWR_NEW_CLIP) {
1368 // shader exporting clip distances overrides all user clip planes
1369 if (ctx->rasterizer->clip_plane_enable &&
1370 !ctx->vs->info.base.num_written_clipdistance)
1371 {
1372 swr_draw_context *pDC = &ctx->swrDC;
1373 memcpy(pDC->userClipPlanes,
1374 ctx->clip.ucp,
1375 sizeof(pDC->userClipPlanes));
1376 }
1377 }
1378
1379 // set up backend state
1380 SWR_BACKEND_STATE backendState = {0};
1381 backendState.numAttributes =
1382 ctx->vs->info.base.num_outputs - 1 +
1383 (ctx->rasterizer->sprite_coord_enable ? 1 : 0);
1384 for (unsigned i = 0; i < backendState.numAttributes; i++)
1385 backendState.numComponents[i] = 4;
1386 backendState.constantInterpolationMask =
1387 ctx->rasterizer->flatshade ?
1388 ctx->fs->flatConstantMask :
1389 ctx->fs->constantMask;
1390 backendState.pointSpriteTexCoordMask = ctx->fs->pointSpriteMask;
1391
1392 SwrSetBackendState(ctx->swrContext, &backendState);
1393
1394 /* Ensure that any in-progress attachment change StoreTiles finish */
1395 if (swr_is_fence_pending(screen->flush_fence))
1396 swr_fence_finish(pipe->screen, NULL, screen->flush_fence, 0);
1397
1398 /* Finally, update the in-use status of all resources involved in draw */
1399 swr_update_resource_status(pipe, p_draw_info);
1400
1401 ctx->dirty = post_update_dirty_flags;
1402 }
1403
1404
1405 static struct pipe_stream_output_target *
1406 swr_create_so_target(struct pipe_context *pipe,
1407 struct pipe_resource *buffer,
1408 unsigned buffer_offset,
1409 unsigned buffer_size)
1410 {
1411 struct pipe_stream_output_target *target;
1412
1413 target = CALLOC_STRUCT(pipe_stream_output_target);
1414 if (!target)
1415 return NULL;
1416
1417 target->context = pipe;
1418 target->reference.count = 1;
1419 pipe_resource_reference(&target->buffer, buffer);
1420 target->buffer_offset = buffer_offset;
1421 target->buffer_size = buffer_size;
1422 return target;
1423 }
1424
1425 static void
1426 swr_destroy_so_target(struct pipe_context *pipe,
1427 struct pipe_stream_output_target *target)
1428 {
1429 pipe_resource_reference(&target->buffer, NULL);
1430 FREE(target);
1431 }
1432
1433 static void
1434 swr_set_so_targets(struct pipe_context *pipe,
1435 unsigned num_targets,
1436 struct pipe_stream_output_target **targets,
1437 const unsigned *offsets)
1438 {
1439 struct swr_context *swr = swr_context(pipe);
1440 uint32_t i;
1441
1442 assert(num_targets < MAX_SO_STREAMS);
1443
1444 for (i = 0; i < num_targets; i++) {
1445 pipe_so_target_reference(
1446 (struct pipe_stream_output_target **)&swr->so_targets[i],
1447 targets[i]);
1448 }
1449
1450 for (/* fall-through */; i < swr->num_so_targets; i++) {
1451 pipe_so_target_reference(
1452 (struct pipe_stream_output_target **)&swr->so_targets[i], NULL);
1453 }
1454
1455 swr->num_so_targets = num_targets;
1456
1457 swr->dirty = SWR_NEW_SO;
1458 }
1459
1460
1461 void
1462 swr_state_init(struct pipe_context *pipe)
1463 {
1464 pipe->create_blend_state = swr_create_blend_state;
1465 pipe->bind_blend_state = swr_bind_blend_state;
1466 pipe->delete_blend_state = swr_delete_blend_state;
1467
1468 pipe->create_depth_stencil_alpha_state = swr_create_depth_stencil_state;
1469 pipe->bind_depth_stencil_alpha_state = swr_bind_depth_stencil_state;
1470 pipe->delete_depth_stencil_alpha_state = swr_delete_depth_stencil_state;
1471
1472 pipe->create_rasterizer_state = swr_create_rasterizer_state;
1473 pipe->bind_rasterizer_state = swr_bind_rasterizer_state;
1474 pipe->delete_rasterizer_state = swr_delete_rasterizer_state;
1475
1476 pipe->create_sampler_state = swr_create_sampler_state;
1477 pipe->bind_sampler_states = swr_bind_sampler_states;
1478 pipe->delete_sampler_state = swr_delete_sampler_state;
1479
1480 pipe->create_sampler_view = swr_create_sampler_view;
1481 pipe->set_sampler_views = swr_set_sampler_views;
1482 pipe->sampler_view_destroy = swr_sampler_view_destroy;
1483
1484 pipe->create_vs_state = swr_create_vs_state;
1485 pipe->bind_vs_state = swr_bind_vs_state;
1486 pipe->delete_vs_state = swr_delete_vs_state;
1487
1488 pipe->create_fs_state = swr_create_fs_state;
1489 pipe->bind_fs_state = swr_bind_fs_state;
1490 pipe->delete_fs_state = swr_delete_fs_state;
1491
1492 pipe->set_constant_buffer = swr_set_constant_buffer;
1493
1494 pipe->create_vertex_elements_state = swr_create_vertex_elements_state;
1495 pipe->bind_vertex_elements_state = swr_bind_vertex_elements_state;
1496 pipe->delete_vertex_elements_state = swr_delete_vertex_elements_state;
1497
1498 pipe->set_vertex_buffers = swr_set_vertex_buffers;
1499 pipe->set_index_buffer = swr_set_index_buffer;
1500
1501 pipe->set_polygon_stipple = swr_set_polygon_stipple;
1502 pipe->set_clip_state = swr_set_clip_state;
1503 pipe->set_scissor_states = swr_set_scissor_states;
1504 pipe->set_viewport_states = swr_set_viewport_states;
1505
1506 pipe->set_framebuffer_state = swr_set_framebuffer_state;
1507
1508 pipe->set_blend_color = swr_set_blend_color;
1509 pipe->set_stencil_ref = swr_set_stencil_ref;
1510
1511 pipe->set_sample_mask = swr_set_sample_mask;
1512
1513 pipe->create_stream_output_target = swr_create_so_target;
1514 pipe->stream_output_target_destroy = swr_destroy_so_target;
1515 pipe->set_stream_output_targets = swr_set_so_targets;
1516 }