1 /* -*- mode: C; c-file-style: "k&r"; tab-width 4; indent-tabs-mode: t; -*- */
4 * Copyright (C) 2012 Rob Clark <robclark@freedesktop.org>
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice (including the next
14 * paragraph) shall be included in all copies or substantial portions of the
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26 * Rob Clark <robclark@freedesktop.org>
29 #include "pipe/p_state.h"
30 #include "util/u_dual_blend.h"
31 #include "util/u_string.h"
32 #include "util/u_memory.h"
33 #include "util/u_helpers.h"
35 #include "freedreno_state.h"
36 #include "freedreno_context.h"
37 #include "freedreno_resource.h"
38 #include "freedreno_texture.h"
39 #include "freedreno_gmem.h"
40 #include "freedreno_query_hw.h"
41 #include "freedreno_util.h"
43 /* All the generic state handling.. In case of CSO's that are specific
44 * to the GPU version, when the bind and the delete are common they can
49 fd_set_blend_color(struct pipe_context
*pctx
,
50 const struct pipe_blend_color
*blend_color
)
52 struct fd_context
*ctx
= fd_context(pctx
);
53 ctx
->blend_color
= *blend_color
;
54 ctx
->dirty
|= FD_DIRTY_BLEND_COLOR
;
58 fd_set_stencil_ref(struct pipe_context
*pctx
,
59 const struct pipe_stencil_ref
*stencil_ref
)
61 struct fd_context
*ctx
= fd_context(pctx
);
62 ctx
->stencil_ref
=* stencil_ref
;
63 ctx
->dirty
|= FD_DIRTY_STENCIL_REF
;
67 fd_set_clip_state(struct pipe_context
*pctx
,
68 const struct pipe_clip_state
*clip
)
70 struct fd_context
*ctx
= fd_context(pctx
);
72 ctx
->dirty
|= FD_DIRTY_UCP
;
76 fd_set_sample_mask(struct pipe_context
*pctx
, unsigned sample_mask
)
78 struct fd_context
*ctx
= fd_context(pctx
);
79 ctx
->sample_mask
= (uint16_t)sample_mask
;
80 ctx
->dirty
|= FD_DIRTY_SAMPLE_MASK
;
83 /* notes from calim on #dri-devel:
84 * index==0 will be non-UBO (ie. glUniformXYZ()) all packed together padded
86 * I should be able to consider that I own the user_ptr until the next
87 * set_constant_buffer() call, at which point I don't really care about the
89 * index>0 will be UBO's.. well, I'll worry about that later
92 fd_set_constant_buffer(struct pipe_context
*pctx
,
93 enum pipe_shader_type shader
, uint index
,
94 const struct pipe_constant_buffer
*cb
)
96 struct fd_context
*ctx
= fd_context(pctx
);
97 struct fd_constbuf_stateobj
*so
= &ctx
->constbuf
[shader
];
99 util_copy_constant_buffer(&so
->cb
[index
], cb
);
101 /* Note that the state tracker can unbind constant buffers by
105 so
->enabled_mask
&= ~(1 << index
);
106 so
->dirty_mask
&= ~(1 << index
);
110 so
->enabled_mask
|= 1 << index
;
111 so
->dirty_mask
|= 1 << index
;
112 ctx
->dirty
|= FD_DIRTY_CONSTBUF
;
116 fd_set_framebuffer_state(struct pipe_context
*pctx
,
117 const struct pipe_framebuffer_state
*framebuffer
)
119 struct fd_context
*ctx
= fd_context(pctx
);
120 struct pipe_framebuffer_state
*cso
;
122 if (ctx
->screen
->reorder
) {
123 struct fd_batch
*batch
, *old_batch
= NULL
;
125 fd_batch_reference(&old_batch
, ctx
->batch
);
127 if (likely(old_batch
))
128 fd_hw_query_set_stage(old_batch
, old_batch
->draw
, FD_STAGE_NULL
);
130 batch
= fd_batch_from_fb(&ctx
->screen
->batch_cache
, ctx
, framebuffer
);
131 fd_batch_reference(&ctx
->batch
, NULL
);
136 if (old_batch
&& old_batch
->blit
&& !old_batch
->back_blit
) {
137 /* for blits, there is not really much point in hanging on
138 * to the uncommitted batch (ie. you probably don't blit
139 * multiple times to the same surface), so we might as
140 * well go ahead and flush this one:
142 fd_batch_flush(old_batch
, false);
145 fd_batch_reference(&old_batch
, NULL
);
147 DBG("%d: cbufs[0]=%p, zsbuf=%p", ctx
->batch
->needs_flush
,
148 framebuffer
->cbufs
[0], framebuffer
->zsbuf
);
149 fd_batch_flush(ctx
->batch
, false);
152 cso
= &ctx
->batch
->framebuffer
;
154 util_copy_framebuffer_state(cso
, framebuffer
);
156 ctx
->dirty
|= FD_DIRTY_FRAMEBUFFER
;
158 ctx
->disabled_scissor
.minx
= 0;
159 ctx
->disabled_scissor
.miny
= 0;
160 ctx
->disabled_scissor
.maxx
= cso
->width
;
161 ctx
->disabled_scissor
.maxy
= cso
->height
;
163 ctx
->dirty
|= FD_DIRTY_SCISSOR
;
167 fd_set_polygon_stipple(struct pipe_context
*pctx
,
168 const struct pipe_poly_stipple
*stipple
)
170 struct fd_context
*ctx
= fd_context(pctx
);
171 ctx
->stipple
= *stipple
;
172 ctx
->dirty
|= FD_DIRTY_STIPPLE
;
176 fd_set_scissor_states(struct pipe_context
*pctx
,
178 unsigned num_scissors
,
179 const struct pipe_scissor_state
*scissor
)
181 struct fd_context
*ctx
= fd_context(pctx
);
183 ctx
->scissor
= *scissor
;
184 ctx
->dirty
|= FD_DIRTY_SCISSOR
;
188 fd_set_viewport_states(struct pipe_context
*pctx
,
190 unsigned num_viewports
,
191 const struct pipe_viewport_state
*viewport
)
193 struct fd_context
*ctx
= fd_context(pctx
);
194 ctx
->viewport
= *viewport
;
195 ctx
->dirty
|= FD_DIRTY_VIEWPORT
;
199 fd_set_vertex_buffers(struct pipe_context
*pctx
,
200 unsigned start_slot
, unsigned count
,
201 const struct pipe_vertex_buffer
*vb
)
203 struct fd_context
*ctx
= fd_context(pctx
);
204 struct fd_vertexbuf_stateobj
*so
= &ctx
->vtx
.vertexbuf
;
207 /* on a2xx, pitch is encoded in the vtx fetch instruction, so
208 * we need to mark VTXSTATE as dirty as well to trigger patching
209 * and re-emitting the vtx shader:
211 if (ctx
->screen
->gpu_id
< 300) {
212 for (i
= 0; i
< count
; i
++) {
213 bool new_enabled
= vb
&& (vb
[i
].buffer
|| vb
[i
].user_buffer
);
214 bool old_enabled
= so
->vb
[i
].buffer
|| so
->vb
[i
].user_buffer
;
215 uint32_t new_stride
= vb
? vb
[i
].stride
: 0;
216 uint32_t old_stride
= so
->vb
[i
].stride
;
217 if ((new_enabled
!= old_enabled
) || (new_stride
!= old_stride
)) {
218 ctx
->dirty
|= FD_DIRTY_VTXSTATE
;
224 util_set_vertex_buffers_mask(so
->vb
, &so
->enabled_mask
, vb
, start_slot
, count
);
225 so
->count
= util_last_bit(so
->enabled_mask
);
227 ctx
->dirty
|= FD_DIRTY_VTXBUF
;
231 fd_set_index_buffer(struct pipe_context
*pctx
,
232 const struct pipe_index_buffer
*ib
)
234 struct fd_context
*ctx
= fd_context(pctx
);
237 pipe_resource_reference(&ctx
->indexbuf
.buffer
, ib
->buffer
);
238 ctx
->indexbuf
.index_size
= ib
->index_size
;
239 ctx
->indexbuf
.offset
= ib
->offset
;
240 ctx
->indexbuf
.user_buffer
= ib
->user_buffer
;
242 pipe_resource_reference(&ctx
->indexbuf
.buffer
, NULL
);
245 ctx
->dirty
|= FD_DIRTY_INDEXBUF
;
249 fd_blend_state_bind(struct pipe_context
*pctx
, void *hwcso
)
251 struct fd_context
*ctx
= fd_context(pctx
);
252 struct pipe_blend_state
*cso
= hwcso
;
253 bool old_is_dual
= ctx
->blend
?
254 ctx
->blend
->rt
[0].blend_enable
&& util_blend_state_is_dual(ctx
->blend
, 0) :
256 bool new_is_dual
= cso
?
257 cso
->rt
[0].blend_enable
&& util_blend_state_is_dual(cso
, 0) :
260 ctx
->dirty
|= FD_DIRTY_BLEND
;
261 if (old_is_dual
!= new_is_dual
)
262 ctx
->dirty
|= FD_DIRTY_BLEND_DUAL
;
266 fd_blend_state_delete(struct pipe_context
*pctx
, void *hwcso
)
272 fd_rasterizer_state_bind(struct pipe_context
*pctx
, void *hwcso
)
274 struct fd_context
*ctx
= fd_context(pctx
);
275 struct pipe_scissor_state
*old_scissor
= fd_context_get_scissor(ctx
);
277 ctx
->rasterizer
= hwcso
;
278 ctx
->dirty
|= FD_DIRTY_RASTERIZER
;
280 /* if scissor enable bit changed we need to mark scissor
281 * state as dirty as well:
282 * NOTE: we can do a shallow compare, since we only care
283 * if it changed to/from &ctx->disable_scissor
285 if (old_scissor
!= fd_context_get_scissor(ctx
))
286 ctx
->dirty
|= FD_DIRTY_SCISSOR
;
290 fd_rasterizer_state_delete(struct pipe_context
*pctx
, void *hwcso
)
296 fd_zsa_state_bind(struct pipe_context
*pctx
, void *hwcso
)
298 struct fd_context
*ctx
= fd_context(pctx
);
300 ctx
->dirty
|= FD_DIRTY_ZSA
;
304 fd_zsa_state_delete(struct pipe_context
*pctx
, void *hwcso
)
310 fd_vertex_state_create(struct pipe_context
*pctx
, unsigned num_elements
,
311 const struct pipe_vertex_element
*elements
)
313 struct fd_vertex_stateobj
*so
= CALLOC_STRUCT(fd_vertex_stateobj
);
318 memcpy(so
->pipe
, elements
, sizeof(*elements
) * num_elements
);
319 so
->num_elements
= num_elements
;
325 fd_vertex_state_delete(struct pipe_context
*pctx
, void *hwcso
)
331 fd_vertex_state_bind(struct pipe_context
*pctx
, void *hwcso
)
333 struct fd_context
*ctx
= fd_context(pctx
);
334 ctx
->vtx
.vtx
= hwcso
;
335 ctx
->dirty
|= FD_DIRTY_VTXSTATE
;
338 static struct pipe_stream_output_target
*
339 fd_create_stream_output_target(struct pipe_context
*pctx
,
340 struct pipe_resource
*prsc
, unsigned buffer_offset
,
341 unsigned buffer_size
)
343 struct pipe_stream_output_target
*target
;
344 struct fd_resource
*rsc
= fd_resource(prsc
);
346 target
= CALLOC_STRUCT(pipe_stream_output_target
);
350 pipe_reference_init(&target
->reference
, 1);
351 pipe_resource_reference(&target
->buffer
, prsc
);
353 target
->context
= pctx
;
354 target
->buffer_offset
= buffer_offset
;
355 target
->buffer_size
= buffer_size
;
357 assert(rsc
->base
.b
.target
== PIPE_BUFFER
);
358 util_range_add(&rsc
->valid_buffer_range
,
359 buffer_offset
, buffer_offset
+ buffer_size
);
365 fd_stream_output_target_destroy(struct pipe_context
*pctx
,
366 struct pipe_stream_output_target
*target
)
368 pipe_resource_reference(&target
->buffer
, NULL
);
373 fd_set_stream_output_targets(struct pipe_context
*pctx
,
374 unsigned num_targets
, struct pipe_stream_output_target
**targets
,
375 const unsigned *offsets
)
377 struct fd_context
*ctx
= fd_context(pctx
);
378 struct fd_streamout_stateobj
*so
= &ctx
->streamout
;
381 debug_assert(num_targets
<= ARRAY_SIZE(so
->targets
));
383 for (i
= 0; i
< num_targets
; i
++) {
384 boolean changed
= targets
[i
] != so
->targets
[i
];
385 boolean append
= (offsets
[i
] == (unsigned)-1);
387 if (!changed
&& append
)
391 so
->offsets
[i
] = offsets
[i
];
393 pipe_so_target_reference(&so
->targets
[i
], targets
[i
]);
396 for (; i
< so
->num_targets
; i
++) {
397 pipe_so_target_reference(&so
->targets
[i
], NULL
);
400 so
->num_targets
= num_targets
;
402 ctx
->dirty
|= FD_DIRTY_STREAMOUT
;
406 fd_state_init(struct pipe_context
*pctx
)
408 pctx
->set_blend_color
= fd_set_blend_color
;
409 pctx
->set_stencil_ref
= fd_set_stencil_ref
;
410 pctx
->set_clip_state
= fd_set_clip_state
;
411 pctx
->set_sample_mask
= fd_set_sample_mask
;
412 pctx
->set_constant_buffer
= fd_set_constant_buffer
;
413 pctx
->set_framebuffer_state
= fd_set_framebuffer_state
;
414 pctx
->set_polygon_stipple
= fd_set_polygon_stipple
;
415 pctx
->set_scissor_states
= fd_set_scissor_states
;
416 pctx
->set_viewport_states
= fd_set_viewport_states
;
418 pctx
->set_vertex_buffers
= fd_set_vertex_buffers
;
419 pctx
->set_index_buffer
= fd_set_index_buffer
;
421 pctx
->bind_blend_state
= fd_blend_state_bind
;
422 pctx
->delete_blend_state
= fd_blend_state_delete
;
424 pctx
->bind_rasterizer_state
= fd_rasterizer_state_bind
;
425 pctx
->delete_rasterizer_state
= fd_rasterizer_state_delete
;
427 pctx
->bind_depth_stencil_alpha_state
= fd_zsa_state_bind
;
428 pctx
->delete_depth_stencil_alpha_state
= fd_zsa_state_delete
;
430 pctx
->create_vertex_elements_state
= fd_vertex_state_create
;
431 pctx
->delete_vertex_elements_state
= fd_vertex_state_delete
;
432 pctx
->bind_vertex_elements_state
= fd_vertex_state_bind
;
434 pctx
->create_stream_output_target
= fd_create_stream_output_target
;
435 pctx
->stream_output_target_destroy
= fd_stream_output_target_destroy
;
436 pctx
->set_stream_output_targets
= fd_set_stream_output_targets
;