1 /****************************************************************************
2 * Copyright (C) 2015 Intel Corporation. All Rights Reserved.
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:
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
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
22 ***************************************************************************/
24 #include "common/os.h"
26 #include "JitManager.h"
27 #include "state_llvm.h"
29 #include "gallivm/lp_bld_tgsi.h"
30 #include "util/u_format.h"
32 #include "util/u_memory.h"
33 #include "util/u_inlines.h"
34 #include "util/u_helpers.h"
35 #include "util/u_framebuffer.h"
37 #include "swr_state.h"
38 #include "swr_context.h"
39 #include "swr_context_llvm.h"
40 #include "swr_screen.h"
41 #include "swr_resource.h"
42 #include "swr_tex_sample.h"
43 #include "swr_scratch.h"
44 #include "swr_shader.h"
45 #include "swr_fence.h"
47 /* These should be pulled out into separate files as necessary
48 * Just initializing everything here to get going. */
51 swr_create_blend_state(struct pipe_context
*pipe
,
52 const struct pipe_blend_state
*blend
)
54 struct swr_blend_state
*state
= CALLOC_STRUCT(swr_blend_state
);
56 memcpy(&state
->pipe
, blend
, sizeof(*blend
));
58 struct pipe_blend_state
*pipe_blend
= &state
->pipe
;
61 target
< std::min(SWR_NUM_RENDERTARGETS
, PIPE_MAX_COLOR_BUFS
);
64 struct pipe_rt_blend_state
*rt_blend
= &pipe_blend
->rt
[target
];
65 SWR_RENDER_TARGET_BLEND_STATE
&blendState
=
66 state
->blendState
.renderTarget
[target
];
67 RENDER_TARGET_BLEND_COMPILE_STATE
&compileState
=
68 state
->compileState
[target
];
70 if (target
!= 0 && !pipe_blend
->independent_blend_enable
) {
72 &state
->compileState
[0],
73 sizeof(RENDER_TARGET_BLEND_COMPILE_STATE
));
77 compileState
.blendEnable
= rt_blend
->blend_enable
;
78 if (compileState
.blendEnable
) {
79 compileState
.sourceAlphaBlendFactor
=
80 swr_convert_blend_factor(rt_blend
->alpha_src_factor
);
81 compileState
.destAlphaBlendFactor
=
82 swr_convert_blend_factor(rt_blend
->alpha_dst_factor
);
83 compileState
.sourceBlendFactor
=
84 swr_convert_blend_factor(rt_blend
->rgb_src_factor
);
85 compileState
.destBlendFactor
=
86 swr_convert_blend_factor(rt_blend
->rgb_dst_factor
);
88 compileState
.colorBlendFunc
=
89 swr_convert_blend_func(rt_blend
->rgb_func
);
90 compileState
.alphaBlendFunc
=
91 swr_convert_blend_func(rt_blend
->alpha_func
);
93 compileState
.logicOpEnable
= state
->pipe
.logicop_enable
;
94 if (compileState
.logicOpEnable
) {
95 compileState
.logicOpFunc
=
96 swr_convert_logic_op(state
->pipe
.logicop_func
);
99 blendState
.writeDisableRed
=
100 (rt_blend
->colormask
& PIPE_MASK_R
) ? 0 : 1;
101 blendState
.writeDisableGreen
=
102 (rt_blend
->colormask
& PIPE_MASK_G
) ? 0 : 1;
103 blendState
.writeDisableBlue
=
104 (rt_blend
->colormask
& PIPE_MASK_B
) ? 0 : 1;
105 blendState
.writeDisableAlpha
=
106 (rt_blend
->colormask
& PIPE_MASK_A
) ? 0 : 1;
108 if (rt_blend
->colormask
== 0)
109 compileState
.blendEnable
= false;
116 swr_bind_blend_state(struct pipe_context
*pipe
, void *blend
)
118 struct swr_context
*ctx
= swr_context(pipe
);
120 if (ctx
->blend
== blend
)
123 ctx
->blend
= (swr_blend_state
*)blend
;
125 ctx
->dirty
|= SWR_NEW_BLEND
;
129 swr_delete_blend_state(struct pipe_context
*pipe
, void *blend
)
135 swr_set_blend_color(struct pipe_context
*pipe
,
136 const struct pipe_blend_color
*color
)
138 struct swr_context
*ctx
= swr_context(pipe
);
140 ctx
->blend_color
= *color
;
142 ctx
->dirty
|= SWR_NEW_BLEND
;
146 swr_set_stencil_ref(struct pipe_context
*pipe
,
147 const struct pipe_stencil_ref
*ref
)
149 struct swr_context
*ctx
= swr_context(pipe
);
151 ctx
->stencil_ref
= *ref
;
153 ctx
->dirty
|= SWR_NEW_DEPTH_STENCIL_ALPHA
;
157 swr_create_depth_stencil_state(
158 struct pipe_context
*pipe
,
159 const struct pipe_depth_stencil_alpha_state
*depth_stencil
)
161 struct pipe_depth_stencil_alpha_state
*state
;
163 state
= (pipe_depth_stencil_alpha_state
*)mem_dup(depth_stencil
,
164 sizeof *depth_stencil
);
170 swr_bind_depth_stencil_state(struct pipe_context
*pipe
, void *depth_stencil
)
172 struct swr_context
*ctx
= swr_context(pipe
);
174 if (ctx
->depth_stencil
== (pipe_depth_stencil_alpha_state
*)depth_stencil
)
177 ctx
->depth_stencil
= (pipe_depth_stencil_alpha_state
*)depth_stencil
;
179 ctx
->dirty
|= SWR_NEW_DEPTH_STENCIL_ALPHA
;
183 swr_delete_depth_stencil_state(struct pipe_context
*pipe
, void *depth
)
190 swr_create_rasterizer_state(struct pipe_context
*pipe
,
191 const struct pipe_rasterizer_state
*rast
)
193 struct pipe_rasterizer_state
*state
;
194 state
= (pipe_rasterizer_state
*)mem_dup(rast
, sizeof *rast
);
200 swr_bind_rasterizer_state(struct pipe_context
*pipe
, void *handle
)
202 struct swr_context
*ctx
= swr_context(pipe
);
203 const struct pipe_rasterizer_state
*rasterizer
=
204 (const struct pipe_rasterizer_state
*)handle
;
206 if (ctx
->rasterizer
== (pipe_rasterizer_state
*)rasterizer
)
209 ctx
->rasterizer
= (pipe_rasterizer_state
*)rasterizer
;
211 ctx
->dirty
|= SWR_NEW_RASTERIZER
;
215 swr_delete_rasterizer_state(struct pipe_context
*pipe
, void *rasterizer
)
222 swr_create_sampler_state(struct pipe_context
*pipe
,
223 const struct pipe_sampler_state
*sampler
)
225 struct pipe_sampler_state
*state
=
226 (pipe_sampler_state
*)mem_dup(sampler
, sizeof *sampler
);
232 swr_bind_sampler_states(struct pipe_context
*pipe
,
238 struct swr_context
*ctx
= swr_context(pipe
);
241 assert(shader
< PIPE_SHADER_TYPES
);
242 assert(start
+ num
<= Elements(ctx
->samplers
[shader
]));
244 /* set the new samplers */
245 ctx
->num_samplers
[shader
] = num
;
246 for (i
= 0; i
< num
; i
++) {
247 ctx
->samplers
[shader
][start
+ i
] = (pipe_sampler_state
*)samplers
[i
];
250 ctx
->dirty
|= SWR_NEW_SAMPLER
;
254 swr_delete_sampler_state(struct pipe_context
*pipe
, void *sampler
)
260 static struct pipe_sampler_view
*
261 swr_create_sampler_view(struct pipe_context
*pipe
,
262 struct pipe_resource
*texture
,
263 const struct pipe_sampler_view
*templ
)
265 struct pipe_sampler_view
*view
= CALLOC_STRUCT(pipe_sampler_view
);
269 view
->reference
.count
= 1;
270 view
->texture
= NULL
;
271 pipe_resource_reference(&view
->texture
, texture
);
272 view
->context
= pipe
;
279 swr_set_sampler_views(struct pipe_context
*pipe
,
283 struct pipe_sampler_view
**views
)
285 struct swr_context
*ctx
= swr_context(pipe
);
288 assert(num
<= PIPE_MAX_SHADER_SAMPLER_VIEWS
);
290 assert(shader
< PIPE_SHADER_TYPES
);
291 assert(start
+ num
<= Elements(ctx
->sampler_views
[shader
]));
293 /* set the new sampler views */
294 ctx
->num_sampler_views
[shader
] = num
;
295 for (i
= 0; i
< num
; i
++) {
296 /* Note: we're using pipe_sampler_view_release() here to work around
297 * a possible crash when the old view belongs to another context that
298 * was already destroyed.
300 pipe_sampler_view_release(pipe
, &ctx
->sampler_views
[shader
][start
+ i
]);
301 pipe_sampler_view_reference(&ctx
->sampler_views
[shader
][start
+ i
],
305 ctx
->dirty
|= SWR_NEW_SAMPLER_VIEW
;
309 swr_sampler_view_destroy(struct pipe_context
*pipe
,
310 struct pipe_sampler_view
*view
)
312 pipe_resource_reference(&view
->texture
, NULL
);
317 swr_create_vs_state(struct pipe_context
*pipe
,
318 const struct pipe_shader_state
*vs
)
320 struct swr_vertex_shader
*swr_vs
= new swr_vertex_shader
;
324 swr_vs
->pipe
.tokens
= tgsi_dup_tokens(vs
->tokens
);
325 swr_vs
->pipe
.stream_output
= vs
->stream_output
;
327 lp_build_tgsi_info(vs
->tokens
, &swr_vs
->info
);
329 swr_vs
->soState
= {0};
331 if (swr_vs
->pipe
.stream_output
.num_outputs
) {
332 pipe_stream_output_info
*stream_output
= &swr_vs
->pipe
.stream_output
;
334 swr_vs
->soState
.soEnable
= true;
335 // soState.rasterizerDisable set on state dirty
336 // soState.streamToRasterizer not used
338 for (uint32_t i
= 0; i
< stream_output
->num_outputs
; i
++) {
339 swr_vs
->soState
.streamMasks
[stream_output
->output
[i
].stream
] |=
340 1 << (stream_output
->output
[i
].register_index
- 1);
342 for (uint32_t i
= 0; i
< MAX_SO_STREAMS
; i
++) {
343 swr_vs
->soState
.streamNumEntries
[i
] =
344 _mm_popcnt_u32(swr_vs
->soState
.streamMasks
[i
]);
352 swr_bind_vs_state(struct pipe_context
*pipe
, void *vs
)
354 struct swr_context
*ctx
= swr_context(pipe
);
359 ctx
->vs
= (swr_vertex_shader
*)vs
;
360 ctx
->dirty
|= SWR_NEW_VS
;
364 swr_delete_vs_state(struct pipe_context
*pipe
, void *vs
)
366 struct swr_vertex_shader
*swr_vs
= (swr_vertex_shader
*)vs
;
367 FREE((void *)swr_vs
->pipe
.tokens
);
372 swr_create_fs_state(struct pipe_context
*pipe
,
373 const struct pipe_shader_state
*fs
)
375 struct swr_fragment_shader
*swr_fs
= new swr_fragment_shader
;
379 swr_fs
->pipe
.tokens
= tgsi_dup_tokens(fs
->tokens
);
381 lp_build_tgsi_info(fs
->tokens
, &swr_fs
->info
);
388 swr_bind_fs_state(struct pipe_context
*pipe
, void *fs
)
390 struct swr_context
*ctx
= swr_context(pipe
);
395 ctx
->fs
= (swr_fragment_shader
*)fs
;
396 ctx
->dirty
|= SWR_NEW_FS
;
400 swr_delete_fs_state(struct pipe_context
*pipe
, void *fs
)
402 struct swr_fragment_shader
*swr_fs
= (swr_fragment_shader
*)fs
;
403 FREE((void *)swr_fs
->pipe
.tokens
);
409 swr_set_constant_buffer(struct pipe_context
*pipe
,
412 struct pipe_constant_buffer
*cb
)
414 struct swr_context
*ctx
= swr_context(pipe
);
415 struct pipe_resource
*constants
= cb
? cb
->buffer
: NULL
;
417 assert(shader
< PIPE_SHADER_TYPES
);
418 assert(index
< Elements(ctx
->constants
[shader
]));
420 /* note: reference counting */
421 util_copy_constant_buffer(&ctx
->constants
[shader
][index
], cb
);
423 if (shader
== PIPE_SHADER_VERTEX
|| shader
== PIPE_SHADER_GEOMETRY
) {
424 ctx
->dirty
|= SWR_NEW_VSCONSTANTS
;
425 } else if (shader
== PIPE_SHADER_FRAGMENT
) {
426 ctx
->dirty
|= SWR_NEW_FSCONSTANTS
;
429 if (cb
&& cb
->user_buffer
) {
430 pipe_resource_reference(&constants
, NULL
);
436 swr_create_vertex_elements_state(struct pipe_context
*pipe
,
437 unsigned num_elements
,
438 const struct pipe_vertex_element
*attribs
)
440 struct swr_vertex_element_state
*velems
;
441 assert(num_elements
<= PIPE_MAX_ATTRIBS
);
442 velems
= CALLOC_STRUCT(swr_vertex_element_state
);
444 velems
->fsState
.numAttribs
= num_elements
;
445 for (unsigned i
= 0; i
< num_elements
; i
++) {
446 // XXX: we should do this keyed on the VS usage info
448 const struct util_format_description
*desc
=
449 util_format_description(attribs
[i
].src_format
);
451 velems
->fsState
.layout
[i
].AlignedByteOffset
= attribs
[i
].src_offset
;
452 velems
->fsState
.layout
[i
].Format
=
453 mesa_to_swr_format(attribs
[i
].src_format
);
454 velems
->fsState
.layout
[i
].StreamIndex
=
455 attribs
[i
].vertex_buffer_index
;
456 velems
->fsState
.layout
[i
].InstanceEnable
=
457 attribs
[i
].instance_divisor
!= 0;
458 velems
->fsState
.layout
[i
].ComponentControl0
=
459 desc
->channel
[0].type
!= UTIL_FORMAT_TYPE_VOID
460 ? ComponentControl::StoreSrc
461 : ComponentControl::Store0
;
462 velems
->fsState
.layout
[i
].ComponentControl1
=
463 desc
->channel
[1].type
!= UTIL_FORMAT_TYPE_VOID
464 ? ComponentControl::StoreSrc
465 : ComponentControl::Store0
;
466 velems
->fsState
.layout
[i
].ComponentControl2
=
467 desc
->channel
[2].type
!= UTIL_FORMAT_TYPE_VOID
468 ? ComponentControl::StoreSrc
469 : ComponentControl::Store0
;
470 velems
->fsState
.layout
[i
].ComponentControl3
=
471 desc
->channel
[3].type
!= UTIL_FORMAT_TYPE_VOID
472 ? ComponentControl::StoreSrc
473 : ComponentControl::Store1Fp
;
474 velems
->fsState
.layout
[i
].ComponentPacking
= ComponentEnable::XYZW
;
475 velems
->fsState
.layout
[i
].InstanceDataStepRate
=
476 attribs
[i
].instance_divisor
;
478 /* Calculate the pitch of each stream */
479 const SWR_FORMAT_INFO
&swr_desc
= GetFormatInfo(
480 mesa_to_swr_format(attribs
[i
].src_format
));
481 velems
->stream_pitch
[attribs
[i
].vertex_buffer_index
] += swr_desc
.Bpp
;
489 swr_bind_vertex_elements_state(struct pipe_context
*pipe
, void *velems
)
491 struct swr_context
*ctx
= swr_context(pipe
);
492 struct swr_vertex_element_state
*swr_velems
=
493 (struct swr_vertex_element_state
*)velems
;
495 ctx
->velems
= swr_velems
;
496 ctx
->dirty
|= SWR_NEW_VERTEX
;
500 swr_delete_vertex_elements_state(struct pipe_context
*pipe
, void *velems
)
502 /* XXX Need to destroy fetch shader? */
508 swr_set_vertex_buffers(struct pipe_context
*pipe
,
510 unsigned num_elements
,
511 const struct pipe_vertex_buffer
*buffers
)
513 struct swr_context
*ctx
= swr_context(pipe
);
515 assert(num_elements
<= PIPE_MAX_ATTRIBS
);
517 util_set_vertex_buffers_count(ctx
->vertex_buffer
,
518 &ctx
->num_vertex_buffers
,
523 ctx
->dirty
|= SWR_NEW_VERTEX
;
528 swr_set_index_buffer(struct pipe_context
*pipe
,
529 const struct pipe_index_buffer
*ib
)
531 struct swr_context
*ctx
= swr_context(pipe
);
534 memcpy(&ctx
->index_buffer
, ib
, sizeof(ctx
->index_buffer
));
536 memset(&ctx
->index_buffer
, 0, sizeof(ctx
->index_buffer
));
538 ctx
->dirty
|= SWR_NEW_VERTEX
;
542 swr_set_polygon_stipple(struct pipe_context
*pipe
,
543 const struct pipe_poly_stipple
*stipple
)
545 struct swr_context
*ctx
= swr_context(pipe
);
547 ctx
->poly_stipple
= *stipple
; /* struct copy */
548 ctx
->dirty
|= SWR_NEW_STIPPLE
;
552 swr_set_clip_state(struct pipe_context
*pipe
,
553 const struct pipe_clip_state
*clip
)
555 struct swr_context
*ctx
= swr_context(pipe
);
558 /* XXX Unimplemented, but prevents crash */
560 ctx
->dirty
|= SWR_NEW_CLIP
;
565 swr_set_scissor_states(struct pipe_context
*pipe
,
567 unsigned num_viewports
,
568 const struct pipe_scissor_state
*scissor
)
570 struct swr_context
*ctx
= swr_context(pipe
);
572 ctx
->scissor
= *scissor
;
573 ctx
->dirty
|= SWR_NEW_SCISSOR
;
577 swr_set_viewport_states(struct pipe_context
*pipe
,
579 unsigned num_viewports
,
580 const struct pipe_viewport_state
*vpt
)
582 struct swr_context
*ctx
= swr_context(pipe
);
584 ctx
->viewport
= *vpt
;
585 ctx
->dirty
|= SWR_NEW_VIEWPORT
;
590 swr_set_framebuffer_state(struct pipe_context
*pipe
,
591 const struct pipe_framebuffer_state
*fb
)
593 struct swr_context
*ctx
= swr_context(pipe
);
595 boolean changed
= !util_framebuffer_state_equal(&ctx
->framebuffer
, fb
);
597 assert(fb
->width
<= KNOB_GUARDBAND_WIDTH
);
598 assert(fb
->height
<= KNOB_GUARDBAND_HEIGHT
);
602 for (i
= 0; i
< fb
->nr_cbufs
; ++i
)
603 pipe_surface_reference(&ctx
->framebuffer
.cbufs
[i
], fb
->cbufs
[i
]);
604 for (; i
< ctx
->framebuffer
.nr_cbufs
; ++i
)
605 pipe_surface_reference(&ctx
->framebuffer
.cbufs
[i
], NULL
);
607 ctx
->framebuffer
.nr_cbufs
= fb
->nr_cbufs
;
609 ctx
->framebuffer
.width
= fb
->width
;
610 ctx
->framebuffer
.height
= fb
->height
;
612 pipe_surface_reference(&ctx
->framebuffer
.zsbuf
, fb
->zsbuf
);
614 ctx
->dirty
|= SWR_NEW_FRAMEBUFFER
;
620 swr_set_sample_mask(struct pipe_context
*pipe
, unsigned sample_mask
)
622 struct swr_context
*ctx
= swr_context(pipe
);
624 if (sample_mask
!= ctx
->sample_mask
) {
625 ctx
->sample_mask
= sample_mask
;
626 ctx
->dirty
|= SWR_NEW_RASTERIZER
;
631 * Update resource in-use status
632 * All resources bound to color or depth targets marked as WRITE resources.
633 * VBO Vertex/index buffers and texture views marked as READ resources.
636 swr_update_resource_status(struct pipe_context
*pipe
,
637 const struct pipe_draw_info
*p_draw_info
)
639 struct swr_context
*ctx
= swr_context(pipe
);
640 struct pipe_framebuffer_state
*fb
= &ctx
->framebuffer
;
642 /* colorbuffer targets */
644 for (uint32_t i
= 0; i
< fb
->nr_cbufs
; ++i
)
646 swr_resource_write(fb
->cbufs
[i
]->texture
);
648 /* depth/stencil target */
650 swr_resource_write(fb
->zsbuf
->texture
);
652 /* VBO vertex buffers */
653 for (uint32_t i
= 0; i
< ctx
->num_vertex_buffers
; i
++) {
654 struct pipe_vertex_buffer
*vb
= &ctx
->vertex_buffer
[i
];
655 if (!vb
->user_buffer
)
656 swr_resource_read(vb
->buffer
);
659 /* VBO index buffer */
660 if (p_draw_info
&& p_draw_info
->indexed
) {
661 struct pipe_index_buffer
*ib
= &ctx
->index_buffer
;
662 if (!ib
->user_buffer
)
663 swr_resource_read(ib
->buffer
);
666 /* texture sampler views */
667 for (uint32_t i
= 0; i
< PIPE_MAX_SHADER_SAMPLER_VIEWS
; i
++) {
668 struct pipe_sampler_view
*view
=
669 ctx
->sampler_views
[PIPE_SHADER_FRAGMENT
][i
];
671 swr_resource_read(view
->texture
);
676 swr_update_texture_state(struct swr_context
*ctx
,
677 unsigned shader_type
,
678 unsigned num_sampler_views
,
679 swr_jit_texture
*textures
)
681 for (unsigned i
= 0; i
< num_sampler_views
; i
++) {
682 struct pipe_sampler_view
*view
=
683 ctx
->sampler_views
[shader_type
][i
];
686 struct pipe_resource
*res
= view
->texture
;
687 struct swr_resource
*swr_res
= swr_resource(res
);
688 struct swr_jit_texture
*jit_tex
= &textures
[i
];
689 memset(jit_tex
, 0, sizeof(*jit_tex
));
690 jit_tex
->width
= res
->width0
;
691 jit_tex
->height
= res
->height0
;
692 jit_tex
->depth
= res
->depth0
;
693 jit_tex
->first_level
= view
->u
.tex
.first_level
;
694 jit_tex
->last_level
= view
->u
.tex
.last_level
;
695 jit_tex
->base_ptr
= swr_res
->swr
.pBaseAddress
;
697 for (unsigned level
= jit_tex
->first_level
;
698 level
<= jit_tex
->last_level
;
700 jit_tex
->row_stride
[level
] = swr_res
->row_stride
[level
];
701 jit_tex
->img_stride
[level
] = swr_res
->img_stride
[level
];
702 jit_tex
->mip_offsets
[level
] = swr_res
->mip_offsets
[level
];
709 swr_update_sampler_state(struct swr_context
*ctx
,
710 unsigned shader_type
,
711 unsigned num_samplers
,
712 swr_jit_sampler
*samplers
)
714 for (unsigned i
= 0; i
< num_samplers
; i
++) {
715 const struct pipe_sampler_state
*sampler
=
716 ctx
->samplers
[shader_type
][i
];
719 samplers
[i
].min_lod
= sampler
->min_lod
;
720 samplers
[i
].max_lod
= sampler
->max_lod
;
721 samplers
[i
].lod_bias
= sampler
->lod_bias
;
722 COPY_4V(samplers
[i
].border_color
, sampler
->border_color
.f
);
728 swr_update_derived(struct pipe_context
*pipe
,
729 const struct pipe_draw_info
*p_draw_info
)
731 struct swr_context
*ctx
= swr_context(pipe
);
732 struct swr_screen
*screen
= swr_screen(ctx
->pipe
.screen
);
734 /* Any state that requires dirty flags to be re-triggered sets this mask */
735 /* For example, user_buffer vertex and index buffers. */
736 unsigned post_update_dirty_flags
= 0;
739 if (ctx
->dirty
& SWR_NEW_FRAMEBUFFER
) {
740 struct pipe_framebuffer_state
*fb
= &ctx
->framebuffer
;
741 SWR_SURFACE_STATE
*new_attachment
[SWR_NUM_ATTACHMENTS
] = {0};
744 /* colorbuffer targets */
746 for (i
= 0; i
< fb
->nr_cbufs
; ++i
)
748 struct swr_resource
*colorBuffer
=
749 swr_resource(fb
->cbufs
[i
]->texture
);
750 new_attachment
[SWR_ATTACHMENT_COLOR0
+ i
] = &colorBuffer
->swr
;
753 /* depth/stencil target */
755 struct swr_resource
*depthStencilBuffer
=
756 swr_resource(fb
->zsbuf
->texture
);
757 if (depthStencilBuffer
->has_depth
) {
758 new_attachment
[SWR_ATTACHMENT_DEPTH
] = &depthStencilBuffer
->swr
;
760 if (depthStencilBuffer
->has_stencil
)
761 new_attachment
[SWR_ATTACHMENT_STENCIL
] =
762 &depthStencilBuffer
->secondary
;
764 } else if (depthStencilBuffer
->has_stencil
)
765 new_attachment
[SWR_ATTACHMENT_STENCIL
] = &depthStencilBuffer
->swr
;
768 /* Make the attachment updates */
769 swr_draw_context
*pDC
= &ctx
->swrDC
;
770 SWR_SURFACE_STATE
*renderTargets
= pDC
->renderTargets
;
771 unsigned need_fence
= FALSE
;
772 for (i
= 0; i
< SWR_NUM_ATTACHMENTS
; i
++) {
773 void *new_base
= nullptr;
774 if (new_attachment
[i
])
775 new_base
= new_attachment
[i
]->pBaseAddress
;
777 /* StoreTile for changed target */
778 if (renderTargets
[i
].pBaseAddress
!= new_base
) {
779 if (renderTargets
[i
].pBaseAddress
) {
780 /* If changing attachment to a new target, mark tiles as
781 * INVALID so they are reloaded from surface.
782 * If detaching attachment, mark tiles as RESOLVED so core
783 * won't try to load from non-existent target. */
784 enum SWR_TILE_STATE post_state
= (new_attachment
[i
]
785 ? SWR_TILE_INVALID
: SWR_TILE_RESOLVED
);
786 swr_store_render_target(pipe
, i
, post_state
);
791 /* Make new attachment */
792 if (new_attachment
[i
])
793 renderTargets
[i
] = *new_attachment
[i
];
795 if (renderTargets
[i
].pBaseAddress
)
796 renderTargets
[i
] = {0};
800 /* This fence ensures any attachment changes are resolved before the
803 swr_fence_submit(ctx
, screen
->flush_fence
);
807 if (ctx
->dirty
& (SWR_NEW_RASTERIZER
| SWR_NEW_FRAMEBUFFER
)) {
808 pipe_rasterizer_state
*rasterizer
= ctx
->rasterizer
;
809 pipe_framebuffer_state
*fb
= &ctx
->framebuffer
;
811 SWR_RASTSTATE
*rastState
= &ctx
->derived
.rastState
;
812 rastState
->cullMode
= swr_convert_cull_mode(rasterizer
->cull_face
);
813 rastState
->frontWinding
= rasterizer
->front_ccw
814 ? SWR_FRONTWINDING_CCW
815 : SWR_FRONTWINDING_CW
;
816 rastState
->scissorEnable
= rasterizer
->scissor
;
817 rastState
->pointSize
= rasterizer
->point_size
> 0.0f
818 ? rasterizer
->point_size
820 rastState
->lineWidth
= rasterizer
->line_width
> 0.0f
821 ? rasterizer
->line_width
824 rastState
->pointParam
= rasterizer
->point_size_per_vertex
;
826 rastState
->pointSpriteEnable
= rasterizer
->sprite_coord_enable
;
827 rastState
->pointSpriteTopOrigin
=
828 rasterizer
->sprite_coord_mode
== PIPE_SPRITE_COORD_UPPER_LEFT
;
830 /* XXX TODO: Add multisample */
831 rastState
->msaaRastEnable
= false;
832 rastState
->rastMode
= SWR_MSAA_RASTMODE_OFF_PIXEL
;
833 rastState
->sampleCount
= SWR_MULTISAMPLE_1X
;
834 rastState
->bForcedSampleCount
= false;
836 bool do_offset
= false;
837 switch (rasterizer
->fill_front
) {
838 case PIPE_POLYGON_MODE_FILL
:
839 do_offset
= rasterizer
->offset_tri
;
841 case PIPE_POLYGON_MODE_LINE
:
842 do_offset
= rasterizer
->offset_line
;
844 case PIPE_POLYGON_MODE_POINT
:
845 do_offset
= rasterizer
->offset_point
;
850 rastState
->depthBias
= rasterizer
->offset_units
;
851 rastState
->slopeScaledDepthBias
= rasterizer
->offset_scale
;
852 rastState
->depthBiasClamp
= rasterizer
->offset_clamp
;
854 rastState
->depthBias
= 0;
855 rastState
->slopeScaledDepthBias
= 0;
856 rastState
->depthBiasClamp
= 0;
858 struct pipe_surface
*zb
= fb
->zsbuf
;
859 if (zb
&& swr_resource(zb
->texture
)->has_depth
)
860 rastState
->depthFormat
= swr_resource(zb
->texture
)->swr
.format
;
862 rastState
->depthClipEnable
= rasterizer
->depth_clip
;
864 SwrSetRastState(ctx
->swrContext
, rastState
);
868 if (ctx
->dirty
& SWR_NEW_SCISSOR
) {
869 pipe_scissor_state
*scissor
= &ctx
->scissor
;
870 BBOX
bbox(scissor
->miny
, scissor
->maxy
,
871 scissor
->minx
, scissor
->maxx
);
872 SwrSetScissorRects(ctx
->swrContext
, 1, &bbox
);
876 if (ctx
->dirty
& (SWR_NEW_VIEWPORT
| SWR_NEW_FRAMEBUFFER
877 | SWR_NEW_RASTERIZER
)) {
878 pipe_viewport_state
*state
= &ctx
->viewport
;
879 pipe_framebuffer_state
*fb
= &ctx
->framebuffer
;
880 pipe_rasterizer_state
*rasterizer
= ctx
->rasterizer
;
882 SWR_VIEWPORT
*vp
= &ctx
->derived
.vp
;
883 SWR_VIEWPORT_MATRIX
*vpm
= &ctx
->derived
.vpm
;
885 vp
->x
= state
->translate
[0] - state
->scale
[0];
886 vp
->width
= state
->translate
[0] + state
->scale
[0];
887 vp
->y
= state
->translate
[1] - fabs(state
->scale
[1]);
888 vp
->height
= state
->translate
[1] + fabs(state
->scale
[1]);
889 if (rasterizer
->clip_halfz
== 0) {
890 vp
->minZ
= state
->translate
[2] - state
->scale
[2];
891 vp
->maxZ
= state
->translate
[2] + state
->scale
[2];
893 vp
->minZ
= state
->translate
[2];
894 vp
->maxZ
= state
->translate
[2] + state
->scale
[2];
897 vpm
->m00
= state
->scale
[0];
898 vpm
->m11
= state
->scale
[1];
899 vpm
->m22
= state
->scale
[2];
900 vpm
->m30
= state
->translate
[0];
901 vpm
->m31
= state
->translate
[1];
902 vpm
->m32
= state
->translate
[2];
904 /* Now that the matrix is calculated, clip the view coords to screen
905 * size. OpenGL allows for -ve x,y in the viewport. */
906 vp
->x
= std::max(vp
->x
, 0.0f
);
907 vp
->y
= std::max(vp
->y
, 0.0f
);
908 vp
->width
= std::min(vp
->width
, (float)fb
->width
);
909 vp
->height
= std::min(vp
->height
, (float)fb
->height
);
911 SwrSetViewports(ctx
->swrContext
, 1, vp
, vpm
);
914 /* Set vertex & index buffers */
915 /* (using draw info if called by swr_draw_vbo) */
916 if (ctx
->dirty
& SWR_NEW_VERTEX
) {
917 uint32_t size
, pitch
, max_vertex
, partial_inbounds
;
918 const uint8_t *p_data
;
920 /* If being called by swr_draw_vbo, copy draw details */
921 struct pipe_draw_info info
= {0};
926 SWR_VERTEX_BUFFER_STATE swrVertexBuffers
[PIPE_MAX_ATTRIBS
];
927 for (UINT i
= 0; i
< ctx
->num_vertex_buffers
; i
++) {
928 struct pipe_vertex_buffer
*vb
= &ctx
->vertex_buffer
[i
];
931 if (!vb
->user_buffer
) {
933 * size is based on buffer->width0 rather than info.max_index
934 * to prevent having to validate VBO on each draw */
935 size
= vb
->buffer
->width0
;
936 max_vertex
= size
/ pitch
;
937 partial_inbounds
= size
% pitch
;
939 p_data
= swr_resource_data(vb
->buffer
) + vb
->buffer_offset
;
942 * client memory is one-time use, re-trigger SWR_NEW_VERTEX to
943 * revalidate on each draw */
944 post_update_dirty_flags
|= SWR_NEW_VERTEX
;
947 size
= (info
.max_index
- info
.min_index
+ 1) * pitch
;
949 /* pitch = 0, means constant value
950 * set size to 1 vertex */
951 size
= ctx
->velems
->stream_pitch
[i
];
954 max_vertex
= info
.max_index
+ 1;
955 partial_inbounds
= 0;
957 /* Copy only needed vertices to scratch space */
958 size
= AlignUp(size
, 4);
959 const void *ptr
= (const uint8_t *) vb
->user_buffer
960 + info
.min_index
* pitch
;
961 ptr
= swr_copy_to_scratch_space(
962 ctx
, &ctx
->scratch
->vertex_buffer
, ptr
, size
);
963 p_data
= (const uint8_t *)ptr
- info
.min_index
* pitch
;
966 swrVertexBuffers
[i
] = {0};
967 swrVertexBuffers
[i
].index
= i
;
968 swrVertexBuffers
[i
].pitch
= pitch
;
969 swrVertexBuffers
[i
].pData
= p_data
;
970 swrVertexBuffers
[i
].size
= size
;
971 swrVertexBuffers
[i
].maxVertex
= max_vertex
;
972 swrVertexBuffers
[i
].partialInboundsSize
= partial_inbounds
;
976 ctx
->swrContext
, ctx
->num_vertex_buffers
, swrVertexBuffers
);
978 /* index buffer, if required (info passed in by swr_draw_vbo) */
979 SWR_FORMAT index_type
= R32_UINT
; /* Default for non-indexed draws */
981 struct pipe_index_buffer
*ib
= &ctx
->index_buffer
;
983 pitch
= ib
->index_size
? ib
->index_size
: sizeof(uint32_t);
984 index_type
= swr_convert_index_type(pitch
);
986 if (!ib
->user_buffer
) {
988 * size is based on buffer->width0 rather than info.count
989 * to prevent having to validate VBO on each draw */
990 size
= ib
->buffer
->width0
;
991 p_data
= swr_resource_data(ib
->buffer
) + ib
->offset
;
994 * client memory is one-time use, re-trigger SWR_NEW_VERTEX to
995 * revalidate on each draw */
996 post_update_dirty_flags
|= SWR_NEW_VERTEX
;
998 size
= info
.count
* pitch
;
999 size
= AlignUp(size
, 4);
1001 /* Copy indices to scratch space */
1002 const void *ptr
= ib
->user_buffer
;
1003 ptr
= swr_copy_to_scratch_space(
1004 ctx
, &ctx
->scratch
->index_buffer
, ptr
, size
);
1005 p_data
= (const uint8_t *)ptr
;
1008 SWR_INDEX_BUFFER_STATE swrIndexBuffer
;
1009 swrIndexBuffer
.format
= swr_convert_index_type(ib
->index_size
);
1010 swrIndexBuffer
.pIndices
= p_data
;
1011 swrIndexBuffer
.size
= size
;
1013 SwrSetIndexBuffer(ctx
->swrContext
, &swrIndexBuffer
);
1016 struct swr_vertex_element_state
*velems
= ctx
->velems
;
1017 if (velems
&& velems
->fsState
.indexType
!= index_type
) {
1018 velems
->fsFunc
= NULL
;
1019 velems
->fsState
.indexType
= index_type
;
1024 if (ctx
->dirty
& (SWR_NEW_VS
|
1026 SWR_NEW_SAMPLER_VIEW
|
1027 SWR_NEW_FRAMEBUFFER
)) {
1029 swr_generate_vs_key(key
, ctx
, ctx
->vs
);
1030 auto search
= ctx
->vs
->map
.find(key
);
1031 PFN_VERTEX_FUNC func
;
1032 if (search
!= ctx
->vs
->map
.end()) {
1033 func
= search
->second
->shader
;
1035 func
= swr_compile_vs(ctx
, key
);
1037 SwrSetVertexFunc(ctx
->swrContext
, func
);
1039 /* JIT sampler state */
1040 if (ctx
->dirty
& SWR_NEW_SAMPLER
) {
1041 swr_update_sampler_state(ctx
,
1044 ctx
->swrDC
.samplersVS
);
1047 /* JIT sampler view state */
1048 if (ctx
->dirty
& (SWR_NEW_SAMPLER_VIEW
| SWR_NEW_FRAMEBUFFER
)) {
1049 swr_update_texture_state(ctx
,
1051 key
.nr_sampler_views
,
1052 ctx
->swrDC
.texturesVS
);
1056 /* FragmentShader */
1057 if (ctx
->dirty
& (SWR_NEW_FS
| SWR_NEW_SAMPLER
| SWR_NEW_SAMPLER_VIEW
1058 | SWR_NEW_RASTERIZER
| SWR_NEW_FRAMEBUFFER
)) {
1060 swr_generate_fs_key(key
, ctx
, ctx
->fs
);
1061 auto search
= ctx
->fs
->map
.find(key
);
1062 PFN_PIXEL_KERNEL func
;
1063 if (search
!= ctx
->fs
->map
.end()) {
1064 func
= search
->second
->shader
;
1066 func
= swr_compile_fs(ctx
, key
);
1068 SWR_PS_STATE psState
= {0};
1069 psState
.pfnPixelShader
= func
;
1070 psState
.killsPixel
= ctx
->fs
->info
.base
.uses_kill
;
1071 psState
.inputCoverage
= SWR_INPUT_COVERAGE_NORMAL
;
1072 psState
.writesODepth
= ctx
->fs
->info
.base
.writes_z
;
1073 psState
.usesSourceDepth
= ctx
->fs
->info
.base
.reads_z
;
1074 psState
.shadingRate
= SWR_SHADING_RATE_PIXEL
; // XXX
1075 psState
.numRenderTargets
= ctx
->framebuffer
.nr_cbufs
;
1076 psState
.posOffset
= SWR_PS_POSITION_SAMPLE_NONE
; // XXX msaa
1077 uint32_t barycentricsMask
= 0;
1079 // when we switch to mesa-master
1080 if (ctx
->fs
->info
.base
.uses_persp_center
||
1081 ctx
->fs
->info
.base
.uses_linear_center
)
1082 barycentricsMask
|= SWR_BARYCENTRIC_PER_PIXEL_MASK
;
1083 if (ctx
->fs
->info
.base
.uses_persp_centroid
||
1084 ctx
->fs
->info
.base
.uses_linear_centroid
)
1085 barycentricsMask
|= SWR_BARYCENTRIC_CENTROID_MASK
;
1086 if (ctx
->fs
->info
.base
.uses_persp_sample
||
1087 ctx
->fs
->info
.base
.uses_linear_sample
)
1088 barycentricsMask
|= SWR_BARYCENTRIC_PER_SAMPLE_MASK
;
1090 for (unsigned i
= 0; i
< ctx
->fs
->info
.base
.num_inputs
; i
++) {
1091 switch (ctx
->fs
->info
.base
.input_interpolate_loc
[i
]) {
1092 case TGSI_INTERPOLATE_LOC_CENTER
:
1093 barycentricsMask
|= SWR_BARYCENTRIC_PER_PIXEL_MASK
;
1095 case TGSI_INTERPOLATE_LOC_CENTROID
:
1096 barycentricsMask
|= SWR_BARYCENTRIC_CENTROID_MASK
;
1098 case TGSI_INTERPOLATE_LOC_SAMPLE
:
1099 barycentricsMask
|= SWR_BARYCENTRIC_PER_SAMPLE_MASK
;
1104 psState
.barycentricsMask
= barycentricsMask
;
1105 psState
.usesUAV
= false; // XXX
1106 psState
.forceEarlyZ
= false;
1107 SwrSetPixelShaderState(ctx
->swrContext
, &psState
);
1109 /* JIT sampler state */
1110 if (ctx
->dirty
& SWR_NEW_SAMPLER
) {
1111 swr_update_sampler_state(ctx
,
1112 PIPE_SHADER_FRAGMENT
,
1114 ctx
->swrDC
.samplersFS
);
1117 /* JIT sampler view state */
1118 if (ctx
->dirty
& (SWR_NEW_SAMPLER_VIEW
| SWR_NEW_FRAMEBUFFER
)) {
1119 swr_update_texture_state(ctx
,
1120 PIPE_SHADER_FRAGMENT
,
1121 key
.nr_sampler_views
,
1122 ctx
->swrDC
.texturesFS
);
1127 /* VertexShader Constants */
1128 if (ctx
->dirty
& SWR_NEW_VSCONSTANTS
) {
1129 swr_draw_context
*pDC
= &ctx
->swrDC
;
1131 for (UINT i
= 0; i
< PIPE_MAX_CONSTANT_BUFFERS
; i
++) {
1132 const pipe_constant_buffer
*cb
=
1133 &ctx
->constants
[PIPE_SHADER_VERTEX
][i
];
1134 pDC
->num_constantsVS
[i
] = cb
->buffer_size
;
1136 pDC
->constantVS
[i
] =
1137 (const float *)(swr_resource_data(cb
->buffer
) +
1140 /* Need to copy these constants to scratch space */
1141 if (cb
->user_buffer
&& cb
->buffer_size
) {
1143 ((const uint8_t *)cb
->user_buffer
+ cb
->buffer_offset
);
1144 uint32_t size
= AlignUp(cb
->buffer_size
, 4);
1145 ptr
= swr_copy_to_scratch_space(
1146 ctx
, &ctx
->scratch
->vs_constants
, ptr
, size
);
1147 pDC
->constantVS
[i
] = (const float *)ptr
;
1153 /* FragmentShader Constants */
1154 if (ctx
->dirty
& SWR_NEW_FSCONSTANTS
) {
1155 swr_draw_context
*pDC
= &ctx
->swrDC
;
1157 for (UINT i
= 0; i
< PIPE_MAX_CONSTANT_BUFFERS
; i
++) {
1158 const pipe_constant_buffer
*cb
=
1159 &ctx
->constants
[PIPE_SHADER_FRAGMENT
][i
];
1160 pDC
->num_constantsFS
[i
] = cb
->buffer_size
;
1162 pDC
->constantFS
[i
] =
1163 (const float *)(swr_resource_data(cb
->buffer
) +
1166 /* Need to copy these constants to scratch space */
1167 if (cb
->user_buffer
&& cb
->buffer_size
) {
1169 ((const uint8_t *)cb
->user_buffer
+ cb
->buffer_offset
);
1170 uint32_t size
= AlignUp(cb
->buffer_size
, 4);
1171 ptr
= swr_copy_to_scratch_space(
1172 ctx
, &ctx
->scratch
->fs_constants
, ptr
, size
);
1173 pDC
->constantFS
[i
] = (const float *)ptr
;
1179 /* Depth/stencil state */
1180 if (ctx
->dirty
& (SWR_NEW_DEPTH_STENCIL_ALPHA
| SWR_NEW_FRAMEBUFFER
)) {
1181 struct pipe_depth_state
*depth
= &(ctx
->depth_stencil
->depth
);
1182 struct pipe_stencil_state
*stencil
= ctx
->depth_stencil
->stencil
;
1183 SWR_DEPTH_STENCIL_STATE depthStencilState
= {{0}};
1185 /* XXX, incomplete. Need to flesh out stencil & alpha test state
1186 struct pipe_stencil_state *front_stencil =
1187 ctx->depth_stencil.stencil[0];
1188 struct pipe_stencil_state *back_stencil = ctx->depth_stencil.stencil[1];
1189 struct pipe_alpha_state alpha;
1191 if (stencil
[0].enabled
) {
1192 depthStencilState
.stencilWriteEnable
= 1;
1193 depthStencilState
.stencilTestEnable
= 1;
1194 depthStencilState
.stencilTestFunc
=
1195 swr_convert_depth_func(stencil
[0].func
);
1197 depthStencilState
.stencilPassDepthPassOp
=
1198 swr_convert_stencil_op(stencil
[0].zpass_op
);
1199 depthStencilState
.stencilPassDepthFailOp
=
1200 swr_convert_stencil_op(stencil
[0].zfail_op
);
1201 depthStencilState
.stencilFailOp
=
1202 swr_convert_stencil_op(stencil
[0].fail_op
);
1203 depthStencilState
.stencilWriteMask
= stencil
[0].writemask
;
1204 depthStencilState
.stencilTestMask
= stencil
[0].valuemask
;
1205 depthStencilState
.stencilRefValue
= ctx
->stencil_ref
.ref_value
[0];
1207 if (stencil
[1].enabled
) {
1208 depthStencilState
.doubleSidedStencilTestEnable
= 1;
1210 depthStencilState
.backfaceStencilTestFunc
=
1211 swr_convert_depth_func(stencil
[1].func
);
1213 depthStencilState
.backfaceStencilPassDepthPassOp
=
1214 swr_convert_stencil_op(stencil
[1].zpass_op
);
1215 depthStencilState
.backfaceStencilPassDepthFailOp
=
1216 swr_convert_stencil_op(stencil
[1].zfail_op
);
1217 depthStencilState
.backfaceStencilFailOp
=
1218 swr_convert_stencil_op(stencil
[1].fail_op
);
1219 depthStencilState
.backfaceStencilWriteMask
= stencil
[1].writemask
;
1220 depthStencilState
.backfaceStencilTestMask
= stencil
[1].valuemask
;
1222 depthStencilState
.backfaceStencilRefValue
=
1223 ctx
->stencil_ref
.ref_value
[1];
1226 depthStencilState
.depthTestEnable
= depth
->enabled
;
1227 depthStencilState
.depthTestFunc
= swr_convert_depth_func(depth
->func
);
1228 depthStencilState
.depthWriteEnable
= depth
->writemask
;
1229 SwrSetDepthStencilState(ctx
->swrContext
, &depthStencilState
);
1233 if (ctx
->dirty
& (SWR_NEW_BLEND
|
1234 SWR_NEW_FRAMEBUFFER
|
1235 SWR_NEW_DEPTH_STENCIL_ALPHA
)) {
1236 struct pipe_framebuffer_state
*fb
= &ctx
->framebuffer
;
1238 SWR_BLEND_STATE blendState
;
1239 memcpy(&blendState
, &ctx
->blend
->blendState
, sizeof(blendState
));
1240 blendState
.constantColor
[0] = ctx
->blend_color
.color
[0];
1241 blendState
.constantColor
[1] = ctx
->blend_color
.color
[1];
1242 blendState
.constantColor
[2] = ctx
->blend_color
.color
[2];
1243 blendState
.constantColor
[3] = ctx
->blend_color
.color
[3];
1244 blendState
.alphaTestReference
=
1245 *((uint32_t*)&ctx
->depth_stencil
->alpha
.ref_value
);
1248 blendState
.sampleMask
= 0;
1249 blendState
.sampleCount
= SWR_MULTISAMPLE_1X
;
1251 /* If there are no color buffers bound, disable writes on RT0
1253 if (fb
->nr_cbufs
== 0) {
1254 blendState
.renderTarget
[0].writeDisableRed
= 1;
1255 blendState
.renderTarget
[0].writeDisableGreen
= 1;
1256 blendState
.renderTarget
[0].writeDisableBlue
= 1;
1257 blendState
.renderTarget
[0].writeDisableAlpha
= 1;
1258 SwrSetBlendFunc(ctx
->swrContext
, 0, NULL
);
1261 for (int target
= 0;
1262 target
< std::min(SWR_NUM_RENDERTARGETS
,
1263 PIPE_MAX_COLOR_BUFS
);
1265 if (!fb
->cbufs
[target
])
1268 struct swr_resource
*colorBuffer
=
1269 swr_resource(fb
->cbufs
[target
]->texture
);
1271 BLEND_COMPILE_STATE compileState
;
1272 memset(&compileState
, 0, sizeof(compileState
));
1273 compileState
.format
= colorBuffer
->swr
.format
;
1274 memcpy(&compileState
.blendState
,
1275 &ctx
->blend
->compileState
[target
],
1276 sizeof(compileState
.blendState
));
1278 if (compileState
.blendState
.blendEnable
== false &&
1279 compileState
.blendState
.logicOpEnable
== false) {
1280 SwrSetBlendFunc(ctx
->swrContext
, target
, NULL
);
1284 compileState
.desc
.alphaTestEnable
=
1285 ctx
->depth_stencil
->alpha
.enabled
;
1286 compileState
.desc
.independentAlphaBlendEnable
=
1287 ctx
->blend
->pipe
.independent_blend_enable
;
1288 compileState
.desc
.alphaToCoverageEnable
=
1289 ctx
->blend
->pipe
.alpha_to_coverage
;
1290 compileState
.desc
.sampleMaskEnable
= 0; // XXX
1291 compileState
.desc
.numSamples
= 1; // XXX
1293 compileState
.alphaTestFunction
=
1294 swr_convert_depth_func(ctx
->depth_stencil
->alpha
.func
);
1295 compileState
.alphaTestFormat
= ALPHA_TEST_FLOAT32
; // xxx
1297 PFN_BLEND_JIT_FUNC func
= NULL
;
1298 auto search
= ctx
->blendJIT
->find(compileState
);
1299 if (search
!= ctx
->blendJIT
->end()) {
1300 func
= search
->second
;
1302 HANDLE hJitMgr
= screen
->hJitMgr
;
1303 func
= JitCompileBlend(hJitMgr
, compileState
);
1304 debug_printf("BLEND shader %p\n", func
);
1305 assert(func
&& "Error: BlendShader = NULL");
1307 ctx
->blendJIT
->insert(std::make_pair(compileState
, func
));
1309 SwrSetBlendFunc(ctx
->swrContext
, target
, func
);
1312 SwrSetBlendState(ctx
->swrContext
, &blendState
);
1315 if (ctx
->dirty
& SWR_NEW_STIPPLE
) {
1316 /* XXX What to do with this one??? SWR doesn't stipple */
1319 if (ctx
->dirty
& (SWR_NEW_VS
| SWR_NEW_SO
| SWR_NEW_RASTERIZER
)) {
1320 ctx
->vs
->soState
.rasterizerDisable
=
1321 ctx
->rasterizer
->rasterizer_discard
;
1322 SwrSetSoState(ctx
->swrContext
, &ctx
->vs
->soState
);
1324 pipe_stream_output_info
*stream_output
= &ctx
->vs
->pipe
.stream_output
;
1326 for (uint32_t i
= 0; i
< ctx
->num_so_targets
; i
++) {
1327 SWR_STREAMOUT_BUFFER buffer
= {0};
1328 if (!ctx
->so_targets
[i
])
1330 buffer
.enable
= true;
1332 (uint32_t *)swr_resource_data(ctx
->so_targets
[i
]->buffer
);
1333 buffer
.bufferSize
= ctx
->so_targets
[i
]->buffer_size
>> 2;
1334 buffer
.pitch
= stream_output
->stride
[i
];
1335 buffer
.streamOffset
= ctx
->so_targets
[i
]->buffer_offset
>> 2;
1337 SwrSetSoBuffers(ctx
->swrContext
, &buffer
, i
);
1341 uint32_t linkage
= ctx
->vs
->linkageMask
;
1342 if (ctx
->rasterizer
->sprite_coord_enable
)
1343 linkage
|= (1 << ctx
->vs
->info
.base
.num_outputs
);
1345 SwrSetLinkage(ctx
->swrContext
, linkage
, NULL
);
1347 // set up frontend state
1348 SWR_FRONTEND_STATE feState
= {0};
1349 SwrSetFrontendState(ctx
->swrContext
, &feState
);
1351 // set up backend state
1352 SWR_BACKEND_STATE backendState
= {0};
1353 backendState
.numAttributes
= 1;
1354 backendState
.numComponents
[0] = 4;
1355 backendState
.constantInterpolationMask
= ctx
->fs
->constantMask
;
1356 backendState
.pointSpriteTexCoordMask
= ctx
->fs
->pointSpriteMask
;
1358 SwrSetBackendState(ctx
->swrContext
, &backendState
);
1360 /* Ensure that any in-progress attachment change StoreTiles finish */
1361 if (swr_is_fence_pending(screen
->flush_fence
))
1362 swr_fence_finish(pipe
->screen
, screen
->flush_fence
, 0);
1364 /* Finally, update the in-use status of all resources involved in draw */
1365 swr_update_resource_status(pipe
, p_draw_info
);
1367 ctx
->dirty
= post_update_dirty_flags
;
1371 static struct pipe_stream_output_target
*
1372 swr_create_so_target(struct pipe_context
*pipe
,
1373 struct pipe_resource
*buffer
,
1374 unsigned buffer_offset
,
1375 unsigned buffer_size
)
1377 struct pipe_stream_output_target
*target
;
1379 target
= CALLOC_STRUCT(pipe_stream_output_target
);
1383 target
->context
= pipe
;
1384 target
->reference
.count
= 1;
1385 pipe_resource_reference(&target
->buffer
, buffer
);
1386 target
->buffer_offset
= buffer_offset
;
1387 target
->buffer_size
= buffer_size
;
1392 swr_destroy_so_target(struct pipe_context
*pipe
,
1393 struct pipe_stream_output_target
*target
)
1395 pipe_resource_reference(&target
->buffer
, NULL
);
1400 swr_set_so_targets(struct pipe_context
*pipe
,
1401 unsigned num_targets
,
1402 struct pipe_stream_output_target
**targets
,
1403 const unsigned *offsets
)
1405 struct swr_context
*swr
= swr_context(pipe
);
1408 assert(num_targets
< MAX_SO_STREAMS
);
1410 for (i
= 0; i
< num_targets
; i
++) {
1411 pipe_so_target_reference(
1412 (struct pipe_stream_output_target
**)&swr
->so_targets
[i
],
1416 for (/* fall-through */; i
< swr
->num_so_targets
; i
++) {
1417 pipe_so_target_reference(
1418 (struct pipe_stream_output_target
**)&swr
->so_targets
[i
], NULL
);
1421 swr
->num_so_targets
= num_targets
;
1423 swr
->dirty
= SWR_NEW_SO
;
1428 swr_state_init(struct pipe_context
*pipe
)
1430 pipe
->create_blend_state
= swr_create_blend_state
;
1431 pipe
->bind_blend_state
= swr_bind_blend_state
;
1432 pipe
->delete_blend_state
= swr_delete_blend_state
;
1434 pipe
->create_depth_stencil_alpha_state
= swr_create_depth_stencil_state
;
1435 pipe
->bind_depth_stencil_alpha_state
= swr_bind_depth_stencil_state
;
1436 pipe
->delete_depth_stencil_alpha_state
= swr_delete_depth_stencil_state
;
1438 pipe
->create_rasterizer_state
= swr_create_rasterizer_state
;
1439 pipe
->bind_rasterizer_state
= swr_bind_rasterizer_state
;
1440 pipe
->delete_rasterizer_state
= swr_delete_rasterizer_state
;
1442 pipe
->create_sampler_state
= swr_create_sampler_state
;
1443 pipe
->bind_sampler_states
= swr_bind_sampler_states
;
1444 pipe
->delete_sampler_state
= swr_delete_sampler_state
;
1446 pipe
->create_sampler_view
= swr_create_sampler_view
;
1447 pipe
->set_sampler_views
= swr_set_sampler_views
;
1448 pipe
->sampler_view_destroy
= swr_sampler_view_destroy
;
1450 pipe
->create_vs_state
= swr_create_vs_state
;
1451 pipe
->bind_vs_state
= swr_bind_vs_state
;
1452 pipe
->delete_vs_state
= swr_delete_vs_state
;
1454 pipe
->create_fs_state
= swr_create_fs_state
;
1455 pipe
->bind_fs_state
= swr_bind_fs_state
;
1456 pipe
->delete_fs_state
= swr_delete_fs_state
;
1458 pipe
->set_constant_buffer
= swr_set_constant_buffer
;
1460 pipe
->create_vertex_elements_state
= swr_create_vertex_elements_state
;
1461 pipe
->bind_vertex_elements_state
= swr_bind_vertex_elements_state
;
1462 pipe
->delete_vertex_elements_state
= swr_delete_vertex_elements_state
;
1464 pipe
->set_vertex_buffers
= swr_set_vertex_buffers
;
1465 pipe
->set_index_buffer
= swr_set_index_buffer
;
1467 pipe
->set_polygon_stipple
= swr_set_polygon_stipple
;
1468 pipe
->set_clip_state
= swr_set_clip_state
;
1469 pipe
->set_scissor_states
= swr_set_scissor_states
;
1470 pipe
->set_viewport_states
= swr_set_viewport_states
;
1472 pipe
->set_framebuffer_state
= swr_set_framebuffer_state
;
1474 pipe
->set_blend_color
= swr_set_blend_color
;
1475 pipe
->set_stencil_ref
= swr_set_stencil_ref
;
1477 pipe
->set_sample_mask
= swr_set_sample_mask
;
1479 pipe
->create_stream_output_target
= swr_create_so_target
;
1480 pipe
->stream_output_target_destroy
= swr_destroy_so_target
;
1481 pipe
->set_stream_output_targets
= swr_set_so_targets
;