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 #ifndef FREEDRENO_CONTEXT_H_
30 #define FREEDRENO_CONTEXT_H_
32 #include "pipe/p_context.h"
33 #include "indices/u_primconvert.h"
34 #include "util/u_blitter.h"
35 #include "util/list.h"
36 #include "util/slab.h"
37 #include "util/u_string.h"
39 #include "freedreno_batch.h"
40 #include "freedreno_screen.h"
41 #include "freedreno_gmem.h"
42 #include "freedreno_util.h"
44 #define BORDER_COLOR_UPLOAD_SIZE (2 * PIPE_MAX_SAMPLERS * BORDERCOLOR_SIZE)
46 struct fd_vertex_stateobj
;
48 struct fd_texture_stateobj
{
49 struct pipe_sampler_view
*textures
[PIPE_MAX_SAMPLERS
];
50 unsigned num_textures
;
51 unsigned valid_textures
;
52 struct pipe_sampler_state
*samplers
[PIPE_MAX_SAMPLERS
];
53 unsigned num_samplers
;
54 unsigned valid_samplers
;
57 struct fd_program_stateobj
{
60 /* rest only used by fd2.. split out: */
62 /* Indexed by semantic name or TGSI_SEMANTIC_COUNT + semantic index
63 * for TGSI_SEMANTIC_GENERIC. Special vs exports (position and point-
64 * size) are not included in this
66 uint8_t export_linkage
[63];
69 struct fd_constbuf_stateobj
{
70 struct pipe_constant_buffer cb
[PIPE_MAX_CONSTANT_BUFFERS
];
71 uint32_t enabled_mask
;
75 struct fd_vertexbuf_stateobj
{
76 struct pipe_vertex_buffer vb
[PIPE_MAX_ATTRIBS
];
78 uint32_t enabled_mask
;
82 struct fd_vertex_stateobj
{
83 struct pipe_vertex_element pipe
[PIPE_MAX_ATTRIBS
];
84 unsigned num_elements
;
87 struct fd_streamout_stateobj
{
88 struct pipe_stream_output_target
*targets
[PIPE_MAX_SO_BUFFERS
];
90 /* Track offset from vtxcnt for streamout data. This counter
91 * is just incremented by # of vertices on each draw until
92 * reset or new streamout buffer bound.
94 * When we eventually have GS, the CPU won't actually know the
95 * number of vertices per draw, so I think we'll have to do
96 * something more clever.
98 unsigned offsets
[PIPE_MAX_SO_BUFFERS
];
101 /* group together the vertex and vertexbuf state.. for ease of passing
102 * around, and because various internal operations (gmem<->mem, etc)
103 * need their own vertex state:
105 struct fd_vertex_state
{
106 struct fd_vertex_stateobj
*vtx
;
107 struct fd_vertexbuf_stateobj vertexbuf
;
110 /* global 3d pipeline dirty state: */
111 enum fd_dirty_3d_state
{
112 FD_DIRTY_BLEND
= BIT(0),
113 FD_DIRTY_RASTERIZER
= BIT(1),
114 FD_DIRTY_ZSA
= BIT(2),
115 FD_DIRTY_BLEND_COLOR
= BIT(3),
116 FD_DIRTY_STENCIL_REF
= BIT(4),
117 FD_DIRTY_SAMPLE_MASK
= BIT(5),
118 FD_DIRTY_FRAMEBUFFER
= BIT(6),
119 FD_DIRTY_STIPPLE
= BIT(7),
120 FD_DIRTY_VIEWPORT
= BIT(8),
121 FD_DIRTY_VTXSTATE
= BIT(9),
122 FD_DIRTY_VTXBUF
= BIT(10),
123 FD_DIRTY_INDEXBUF
= BIT(11),
124 FD_DIRTY_SCISSOR
= BIT(12),
125 FD_DIRTY_STREAMOUT
= BIT(13),
126 FD_DIRTY_UCP
= BIT(14),
127 FD_DIRTY_BLEND_DUAL
= BIT(15),
129 /* These are a bit redundent with fd_dirty_shader_state, and possibly
130 * should be removed. (But OTOH kinda convenient in some places)
132 FD_DIRTY_PROG
= BIT(16),
133 FD_DIRTY_CONST
= BIT(17),
134 FD_DIRTY_TEX
= BIT(18),
136 /* only used by a2xx.. possibly can be removed.. */
137 FD_DIRTY_TEXSTATE
= BIT(19),
140 /* per shader-stage dirty state: */
141 enum fd_dirty_shader_state
{
142 FD_DIRTY_SHADER_PROG
= BIT(0),
143 FD_DIRTY_SHADER_CONST
= BIT(1),
144 FD_DIRTY_SHADER_TEX
= BIT(2),
148 struct pipe_context base
;
150 struct fd_device
*dev
;
151 struct fd_screen
*screen
;
153 struct util_queue flush_queue
;
155 struct blitter_context
*blitter
;
156 void *clear_rs_state
;
157 struct primconvert_context
*primconvert
;
159 /* slab for pipe_transfer allocations: */
160 struct slab_child_pool transfer_pool
;
163 * query related state:
166 /* slabs for fd_hw_sample and fd_hw_sample_period allocations: */
167 struct slab_mempool sample_pool
;
168 struct slab_mempool sample_period_pool
;
170 /* sample-providers for hw queries: */
171 const struct fd_hw_sample_provider
*hw_sample_providers
[MAX_HW_SAMPLE_PROVIDERS
];
173 /* list of active queries: */
174 struct list_head hw_active_queries
;
176 /* sample-providers for accumulating hw queries: */
177 const struct fd_acc_sample_provider
*acc_sample_providers
[MAX_HW_SAMPLE_PROVIDERS
];
179 /* list of active accumulating queries: */
180 struct list_head acc_active_queries
;
183 /* table with PIPE_PRIM_MAX entries mapping PIPE_PRIM_x to
184 * DI_PT_x value to use for draw initiator. There are some
185 * slight differences between generation:
187 const uint8_t *primtypes
;
188 uint32_t primtype_mask
;
190 /* shaders used by clear, and gmem->mem blits: */
191 struct fd_program_stateobj solid_prog
; // TODO move to screen?
193 /* shaders used by mem->gmem blits: */
194 struct fd_program_stateobj blit_prog
[MAX_RENDER_TARGETS
]; // TODO move to screen?
195 struct fd_program_stateobj blit_z
, blit_zs
;
200 uint64_t prims_emitted
;
201 uint64_t prims_generated
;
203 uint64_t batch_total
, batch_sysmem
, batch_gmem
, batch_restore
;
206 /* Current batch.. the rule here is that you can deref ctx->batch
207 * in codepaths from pipe_context entrypoints. But not in code-
208 * paths from fd_batch_flush() (basically, the stuff that gets
209 * called from GMEM code), since in those code-paths the batch
210 * you care about is not necessarily the same as ctx->batch.
212 struct fd_batch
*batch
;
214 struct pipe_fence_handle
*last_fence
;
216 /* Are we in process of shadowing a resource? Used to detect recursion
217 * in transfer_map, and skip unneeded synchronization.
221 /* Ie. in blit situation where we no longer care about previous framebuffer
222 * contents. Main point is to eliminate blits from fd_try_shadow_resource().
223 * For example, in case of texture upload + gen-mipmaps.
227 struct pipe_scissor_state scissor
;
229 /* we don't have a disable/enable bit for scissor, so instead we keep
230 * a disabled-scissor state which matches the entire bound framebuffer
231 * and use that when scissor is not enabled.
233 struct pipe_scissor_state disabled_scissor
;
235 /* Current gmem/tiling configuration.. gets updated on render_tiles()
236 * if out of date with current maximal-scissor/cpp:
238 * (NOTE: this is kind of related to the batch, but moving it there
239 * means we'd always have to recalc tiles ever batch)
241 struct fd_gmem_stateobj gmem
;
242 struct fd_vsc_pipe pipe
[8];
243 struct fd_tile tile
[512];
245 /* which state objects need to be re-emit'd: */
246 enum fd_dirty_3d_state dirty
;
248 /* per shader-stage dirty status: */
249 enum fd_dirty_shader_state dirty_shader
[PIPE_SHADER_TYPES
];
251 struct pipe_blend_state
*blend
;
252 struct pipe_rasterizer_state
*rasterizer
;
253 struct pipe_depth_stencil_alpha_state
*zsa
;
255 struct fd_texture_stateobj tex
[PIPE_SHADER_TYPES
];
257 struct fd_program_stateobj prog
;
259 struct fd_vertex_state vtx
;
261 struct pipe_blend_color blend_color
;
262 struct pipe_stencil_ref stencil_ref
;
263 unsigned sample_mask
;
264 struct pipe_poly_stipple stipple
;
265 struct pipe_viewport_state viewport
;
266 struct fd_constbuf_stateobj constbuf
[PIPE_SHADER_TYPES
];
267 struct pipe_index_buffer indexbuf
;
268 struct fd_streamout_stateobj streamout
;
269 struct pipe_clip_state ucp
;
271 struct pipe_query
*cond_query
;
272 bool cond_cond
; /* inverted rendering condition */
275 struct pipe_debug_callback debug
;
277 /* GMEM/tile handling fxns: */
278 void (*emit_tile_init
)(struct fd_batch
*batch
);
279 void (*emit_tile_prep
)(struct fd_batch
*batch
, struct fd_tile
*tile
);
280 void (*emit_tile_mem2gmem
)(struct fd_batch
*batch
, struct fd_tile
*tile
);
281 void (*emit_tile_renderprep
)(struct fd_batch
*batch
, struct fd_tile
*tile
);
282 void (*emit_tile_gmem2mem
)(struct fd_batch
*batch
, struct fd_tile
*tile
);
283 void (*emit_tile_fini
)(struct fd_batch
*batch
); /* optional */
285 /* optional, for GMEM bypass: */
286 void (*emit_sysmem_prep
)(struct fd_batch
*batch
);
287 void (*emit_sysmem_fini
)(struct fd_batch
*batch
);
290 bool (*draw_vbo
)(struct fd_context
*ctx
, const struct pipe_draw_info
*info
);
291 void (*clear
)(struct fd_context
*ctx
, unsigned buffers
,
292 const union pipe_color_union
*color
, double depth
, unsigned stencil
);
294 /* constant emit: (note currently not used/needed for a2xx) */
295 void (*emit_const
)(struct fd_ringbuffer
*ring
, enum shader_t type
,
296 uint32_t regid
, uint32_t offset
, uint32_t sizedwords
,
297 const uint32_t *dwords
, struct pipe_resource
*prsc
);
298 /* emit bo addresses as constant: */
299 void (*emit_const_bo
)(struct fd_ringbuffer
*ring
, enum shader_t type
, boolean write
,
300 uint32_t regid
, uint32_t num
, struct pipe_resource
**prscs
, uint32_t *offsets
);
302 /* indirect-branch emit: */
303 void (*emit_ib
)(struct fd_ringbuffer
*ring
, struct fd_ringbuffer
*target
);
306 struct fd_query
* (*create_query
)(struct fd_context
*ctx
, unsigned query_type
);
307 void (*query_prepare
)(struct fd_batch
*batch
, uint32_t num_tiles
);
308 void (*query_prepare_tile
)(struct fd_batch
*batch
, uint32_t n
,
309 struct fd_ringbuffer
*ring
);
310 void (*query_set_stage
)(struct fd_batch
*batch
,
311 struct fd_ringbuffer
*ring
, enum fd_render_stage stage
);
314 * Common pre-cooked VBO state (used for a3xx and later):
317 /* for clear/gmem->mem vertices, and mem->gmem */
318 struct pipe_resource
*solid_vbuf
;
320 /* for mem->gmem tex coords: */
321 struct pipe_resource
*blit_texcoord_vbuf
;
323 /* vertex state for solid_vbuf:
324 * - solid_vbuf / 12 / R32G32B32_FLOAT
326 struct fd_vertex_state solid_vbuf_state
;
328 /* vertex state for blit_prog:
329 * - blit_texcoord_vbuf / 8 / R32G32_FLOAT
330 * - solid_vbuf / 12 / R32G32B32_FLOAT
332 struct fd_vertex_state blit_vbuf_state
;
335 static inline struct fd_context
*
336 fd_context(struct pipe_context
*pctx
)
338 return (struct fd_context
*)pctx
;
342 fd_context_assert_locked(struct fd_context
*ctx
)
344 pipe_mutex_assert_locked(ctx
->screen
->lock
);
348 fd_context_lock(struct fd_context
*ctx
)
350 mtx_lock(&ctx
->screen
->lock
);
354 fd_context_unlock(struct fd_context
*ctx
)
356 mtx_unlock(&ctx
->screen
->lock
);
359 /* mark all state dirty: */
361 fd_context_all_dirty(struct fd_context
*ctx
)
364 for (unsigned i
= 0; i
< PIPE_SHADER_TYPES
; i
++)
365 ctx
->dirty_shader
[i
] = ~0;
369 fd_context_all_clean(struct fd_context
*ctx
)
372 for (unsigned i
= 0; i
< PIPE_SHADER_TYPES
; i
++)
373 ctx
->dirty_shader
[i
] = 0;
376 static inline struct pipe_scissor_state
*
377 fd_context_get_scissor(struct fd_context
*ctx
)
379 if (ctx
->rasterizer
&& ctx
->rasterizer
->scissor
)
380 return &ctx
->scissor
;
381 return &ctx
->disabled_scissor
;
385 fd_supported_prim(struct fd_context
*ctx
, unsigned prim
)
387 return (1 << prim
) & ctx
->primtype_mask
;
391 fd_batch_set_stage(struct fd_batch
*batch
,
392 struct fd_ringbuffer
*ring
, enum fd_render_stage stage
)
394 struct fd_context
*ctx
= batch
->ctx
;
396 /* special case: internal blits (like mipmap level generation)
397 * go through normal draw path (via util_blitter_blit()).. but
398 * we need to ignore the FD_STAGE_DRAW which will be set, so we
399 * don't enable queries which should be paused during internal
402 if ((batch
->stage
== FD_STAGE_BLIT
) &&
403 (stage
!= FD_STAGE_NULL
))
406 if (ctx
->query_set_stage
)
407 ctx
->query_set_stage(batch
, ring
, stage
);
409 batch
->stage
= stage
;
412 void fd_context_setup_common_vbos(struct fd_context
*ctx
);
413 void fd_context_cleanup_common_vbos(struct fd_context
*ctx
);
415 struct pipe_context
* fd_context_init(struct fd_context
*ctx
,
416 struct pipe_screen
*pscreen
, const uint8_t *primtypes
,
419 void fd_context_destroy(struct pipe_context
*pctx
);
421 #endif /* FREEDRENO_CONTEXT_H_ */