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
;
55 /* number of samples per sampler, 2 bits per sampler: */
59 struct fd_program_stateobj
{
62 /* rest only used by fd2.. split out: */
64 /* Indexed by semantic name or TGSI_SEMANTIC_COUNT + semantic index
65 * for TGSI_SEMANTIC_GENERIC. Special vs exports (position and point-
66 * size) are not included in this
68 uint8_t export_linkage
[63];
71 struct fd_constbuf_stateobj
{
72 struct pipe_constant_buffer cb
[PIPE_MAX_CONSTANT_BUFFERS
];
73 uint32_t enabled_mask
;
76 struct fd_shaderbuf_stateobj
{
77 struct pipe_shader_buffer sb
[PIPE_MAX_SHADER_BUFFERS
];
78 uint32_t enabled_mask
;
81 struct fd_shaderimg_stateobj
{
82 struct pipe_image_view si
[PIPE_MAX_SHADER_IMAGES
];
83 uint32_t enabled_mask
;
86 struct fd_vertexbuf_stateobj
{
87 struct pipe_vertex_buffer vb
[PIPE_MAX_ATTRIBS
];
89 uint32_t enabled_mask
;
92 struct fd_vertex_stateobj
{
93 struct pipe_vertex_element pipe
[PIPE_MAX_ATTRIBS
];
94 unsigned num_elements
;
97 struct fd_streamout_stateobj
{
98 struct pipe_stream_output_target
*targets
[PIPE_MAX_SO_BUFFERS
];
100 /* Track offset from vtxcnt for streamout data. This counter
101 * is just incremented by # of vertices on each draw until
102 * reset or new streamout buffer bound.
104 * When we eventually have GS, the CPU won't actually know the
105 * number of vertices per draw, so I think we'll have to do
106 * something more clever.
108 unsigned offsets
[PIPE_MAX_SO_BUFFERS
];
111 #define MAX_GLOBAL_BUFFERS 16
112 struct fd_global_bindings_stateobj
{
113 struct pipe_resource
*buf
[MAX_GLOBAL_BUFFERS
];
114 uint32_t enabled_mask
;
117 /* group together the vertex and vertexbuf state.. for ease of passing
118 * around, and because various internal operations (gmem<->mem, etc)
119 * need their own vertex state:
121 struct fd_vertex_state
{
122 struct fd_vertex_stateobj
*vtx
;
123 struct fd_vertexbuf_stateobj vertexbuf
;
126 /* global 3d pipeline dirty state: */
127 enum fd_dirty_3d_state
{
128 FD_DIRTY_BLEND
= BIT(0),
129 FD_DIRTY_RASTERIZER
= BIT(1),
130 FD_DIRTY_ZSA
= BIT(2),
131 FD_DIRTY_BLEND_COLOR
= BIT(3),
132 FD_DIRTY_STENCIL_REF
= BIT(4),
133 FD_DIRTY_SAMPLE_MASK
= BIT(5),
134 FD_DIRTY_FRAMEBUFFER
= BIT(6),
135 FD_DIRTY_STIPPLE
= BIT(7),
136 FD_DIRTY_VIEWPORT
= BIT(8),
137 FD_DIRTY_VTXSTATE
= BIT(9),
138 FD_DIRTY_VTXBUF
= BIT(10),
140 FD_DIRTY_SCISSOR
= BIT(12),
141 FD_DIRTY_STREAMOUT
= BIT(13),
142 FD_DIRTY_UCP
= BIT(14),
143 FD_DIRTY_BLEND_DUAL
= BIT(15),
145 /* These are a bit redundent with fd_dirty_shader_state, and possibly
146 * should be removed. (But OTOH kinda convenient in some places)
148 FD_DIRTY_PROG
= BIT(16),
149 FD_DIRTY_CONST
= BIT(17),
150 FD_DIRTY_TEX
= BIT(18),
152 /* only used by a2xx.. possibly can be removed.. */
153 FD_DIRTY_TEXSTATE
= BIT(19),
156 /* per shader-stage dirty state: */
157 enum fd_dirty_shader_state
{
158 FD_DIRTY_SHADER_PROG
= BIT(0),
159 FD_DIRTY_SHADER_CONST
= BIT(1),
160 FD_DIRTY_SHADER_TEX
= BIT(2),
161 FD_DIRTY_SHADER_SSBO
= BIT(3),
162 FD_DIRTY_SHADER_IMAGE
= BIT(4),
166 struct pipe_context base
;
168 struct fd_device
*dev
;
169 struct fd_screen
*screen
;
170 struct fd_pipe
*pipe
;
172 struct util_queue flush_queue
;
174 struct blitter_context
*blitter
;
175 void *clear_rs_state
;
176 struct primconvert_context
*primconvert
;
178 /* slab for pipe_transfer allocations: */
179 struct slab_child_pool transfer_pool
;
182 * query related state:
185 /* slabs for fd_hw_sample and fd_hw_sample_period allocations: */
186 struct slab_mempool sample_pool
;
187 struct slab_mempool sample_period_pool
;
189 /* sample-providers for hw queries: */
190 const struct fd_hw_sample_provider
*hw_sample_providers
[MAX_HW_SAMPLE_PROVIDERS
];
192 /* list of active queries: */
193 struct list_head hw_active_queries
;
195 /* sample-providers for accumulating hw queries: */
196 const struct fd_acc_sample_provider
*acc_sample_providers
[MAX_HW_SAMPLE_PROVIDERS
];
198 /* list of active accumulating queries: */
199 struct list_head acc_active_queries
;
202 /* table with PIPE_PRIM_MAX entries mapping PIPE_PRIM_x to
203 * DI_PT_x value to use for draw initiator. There are some
204 * slight differences between generation:
206 const uint8_t *primtypes
;
207 uint32_t primtype_mask
;
209 /* shaders used by clear, and gmem->mem blits: */
210 struct fd_program_stateobj solid_prog
; // TODO move to screen?
212 /* shaders used by mem->gmem blits: */
213 struct fd_program_stateobj blit_prog
[MAX_RENDER_TARGETS
]; // TODO move to screen?
214 struct fd_program_stateobj blit_z
, blit_zs
;
219 uint64_t prims_emitted
;
220 uint64_t prims_generated
;
222 uint64_t batch_total
, batch_sysmem
, batch_gmem
, batch_nondraw
, batch_restore
;
223 uint64_t staging_uploads
, shadow_uploads
;
224 uint64_t vs_regs
, fs_regs
;
227 /* Current batch.. the rule here is that you can deref ctx->batch
228 * in codepaths from pipe_context entrypoints. But not in code-
229 * paths from fd_batch_flush() (basically, the stuff that gets
230 * called from GMEM code), since in those code-paths the batch
231 * you care about is not necessarily the same as ctx->batch.
233 struct fd_batch
*batch
;
235 /* Are we in process of shadowing a resource? Used to detect recursion
236 * in transfer_map, and skip unneeded synchronization.
240 /* Ie. in blit situation where we no longer care about previous framebuffer
241 * contents. Main point is to eliminate blits from fd_try_shadow_resource().
242 * For example, in case of texture upload + gen-mipmaps.
246 struct pipe_scissor_state scissor
;
248 /* we don't have a disable/enable bit for scissor, so instead we keep
249 * a disabled-scissor state which matches the entire bound framebuffer
250 * and use that when scissor is not enabled.
252 struct pipe_scissor_state disabled_scissor
;
254 /* Current gmem/tiling configuration.. gets updated on render_tiles()
255 * if out of date with current maximal-scissor/cpp:
257 * (NOTE: this is kind of related to the batch, but moving it there
258 * means we'd always have to recalc tiles ever batch)
260 struct fd_gmem_stateobj gmem
;
261 struct fd_vsc_pipe vsc_pipe
[32];
262 struct fd_tile tile
[512];
264 /* which state objects need to be re-emit'd: */
265 enum fd_dirty_3d_state dirty
;
267 /* per shader-stage dirty status: */
268 enum fd_dirty_shader_state dirty_shader
[PIPE_SHADER_TYPES
];
271 struct pipe_blend_state
*blend
;
272 struct pipe_rasterizer_state
*rasterizer
;
273 struct pipe_depth_stencil_alpha_state
*zsa
;
275 struct fd_texture_stateobj tex
[PIPE_SHADER_TYPES
];
277 struct fd_program_stateobj prog
;
279 struct fd_vertex_state vtx
;
281 struct pipe_blend_color blend_color
;
282 struct pipe_stencil_ref stencil_ref
;
283 unsigned sample_mask
;
284 /* local context fb state, for when ctx->batch is null: */
285 struct pipe_framebuffer_state framebuffer
;
286 struct pipe_poly_stipple stipple
;
287 struct pipe_viewport_state viewport
;
288 struct fd_constbuf_stateobj constbuf
[PIPE_SHADER_TYPES
];
289 struct fd_shaderbuf_stateobj shaderbuf
[PIPE_SHADER_TYPES
];
290 struct fd_shaderimg_stateobj shaderimg
[PIPE_SHADER_TYPES
];
291 struct fd_streamout_stateobj streamout
;
292 struct fd_global_bindings_stateobj global_bindings
;
293 struct pipe_clip_state ucp
;
295 struct pipe_query
*cond_query
;
296 bool cond_cond
; /* inverted rendering condition */
299 struct pipe_debug_callback debug
;
301 /* GMEM/tile handling fxns: */
302 void (*emit_tile_init
)(struct fd_batch
*batch
);
303 void (*emit_tile_prep
)(struct fd_batch
*batch
, struct fd_tile
*tile
);
304 void (*emit_tile_mem2gmem
)(struct fd_batch
*batch
, struct fd_tile
*tile
);
305 void (*emit_tile_renderprep
)(struct fd_batch
*batch
, struct fd_tile
*tile
);
306 void (*emit_tile_gmem2mem
)(struct fd_batch
*batch
, struct fd_tile
*tile
);
307 void (*emit_tile_fini
)(struct fd_batch
*batch
); /* optional */
309 /* optional, for GMEM bypass: */
310 void (*emit_sysmem_prep
)(struct fd_batch
*batch
);
311 void (*emit_sysmem_fini
)(struct fd_batch
*batch
);
314 bool (*draw_vbo
)(struct fd_context
*ctx
, const struct pipe_draw_info
*info
,
315 unsigned index_offset
);
316 bool (*clear
)(struct fd_context
*ctx
, unsigned buffers
,
317 const union pipe_color_union
*color
, double depth
, unsigned stencil
);
320 void (*launch_grid
)(struct fd_context
*ctx
, const struct pipe_grid_info
*info
);
322 /* constant emit: (note currently not used/needed for a2xx) */
323 void (*emit_const
)(struct fd_ringbuffer
*ring
, enum shader_t type
,
324 uint32_t regid
, uint32_t offset
, uint32_t sizedwords
,
325 const uint32_t *dwords
, struct pipe_resource
*prsc
);
326 /* emit bo addresses as constant: */
327 void (*emit_const_bo
)(struct fd_ringbuffer
*ring
, enum shader_t type
, boolean write
,
328 uint32_t regid
, uint32_t num
, struct pipe_resource
**prscs
, uint32_t *offsets
);
330 /* indirect-branch emit: */
331 void (*emit_ib
)(struct fd_ringbuffer
*ring
, struct fd_ringbuffer
*target
);
334 struct fd_query
* (*create_query
)(struct fd_context
*ctx
, unsigned query_type
);
335 void (*query_prepare
)(struct fd_batch
*batch
, uint32_t num_tiles
);
336 void (*query_prepare_tile
)(struct fd_batch
*batch
, uint32_t n
,
337 struct fd_ringbuffer
*ring
);
338 void (*query_set_stage
)(struct fd_batch
*batch
, enum fd_render_stage stage
);
341 void (*blit
)(struct fd_context
*ctx
, const struct pipe_blit_info
*info
);
343 /* simple gpu "memcpy": */
344 void (*mem_to_mem
)(struct fd_ringbuffer
*ring
, struct pipe_resource
*dst
,
345 unsigned dst_off
, struct pipe_resource
*src
, unsigned src_off
,
346 unsigned sizedwords
);
349 * Common pre-cooked VBO state (used for a3xx and later):
352 /* for clear/gmem->mem vertices, and mem->gmem */
353 struct pipe_resource
*solid_vbuf
;
355 /* for mem->gmem tex coords: */
356 struct pipe_resource
*blit_texcoord_vbuf
;
358 /* vertex state for solid_vbuf:
359 * - solid_vbuf / 12 / R32G32B32_FLOAT
361 struct fd_vertex_state solid_vbuf_state
;
363 /* vertex state for blit_prog:
364 * - blit_texcoord_vbuf / 8 / R32G32_FLOAT
365 * - solid_vbuf / 12 / R32G32B32_FLOAT
367 struct fd_vertex_state blit_vbuf_state
;
370 static inline struct fd_context
*
371 fd_context(struct pipe_context
*pctx
)
373 return (struct fd_context
*)pctx
;
377 fd_context_assert_locked(struct fd_context
*ctx
)
379 pipe_mutex_assert_locked(ctx
->screen
->lock
);
383 fd_context_lock(struct fd_context
*ctx
)
385 mtx_lock(&ctx
->screen
->lock
);
389 fd_context_unlock(struct fd_context
*ctx
)
391 mtx_unlock(&ctx
->screen
->lock
);
394 /* mark all state dirty: */
396 fd_context_all_dirty(struct fd_context
*ctx
)
399 for (unsigned i
= 0; i
< PIPE_SHADER_TYPES
; i
++)
400 ctx
->dirty_shader
[i
] = ~0;
404 fd_context_all_clean(struct fd_context
*ctx
)
407 for (unsigned i
= 0; i
< PIPE_SHADER_TYPES
; i
++) {
408 /* don't mark compute state as clean, since it is not emitted
409 * during normal draw call. The places that call _all_dirty(),
410 * it is safe to mark compute state dirty as well, but the
411 * inverse is not true.
413 if (i
== PIPE_SHADER_COMPUTE
)
415 ctx
->dirty_shader
[i
] = 0;
419 static inline struct pipe_scissor_state
*
420 fd_context_get_scissor(struct fd_context
*ctx
)
422 if (ctx
->rasterizer
&& ctx
->rasterizer
->scissor
)
423 return &ctx
->scissor
;
424 return &ctx
->disabled_scissor
;
428 fd_supported_prim(struct fd_context
*ctx
, unsigned prim
)
430 return (1 << prim
) & ctx
->primtype_mask
;
433 static inline struct fd_batch
*
434 fd_context_batch(struct fd_context
*ctx
)
436 if (unlikely(!ctx
->batch
)) {
437 struct fd_batch
*batch
=
438 fd_batch_from_fb(&ctx
->screen
->batch_cache
, ctx
, &ctx
->framebuffer
);
439 util_copy_framebuffer_state(&batch
->framebuffer
, &ctx
->framebuffer
);
441 fd_context_all_dirty(ctx
);
447 fd_batch_set_stage(struct fd_batch
*batch
, enum fd_render_stage stage
)
449 struct fd_context
*ctx
= batch
->ctx
;
451 /* special case: internal blits (like mipmap level generation)
452 * go through normal draw path (via util_blitter_blit()).. but
453 * we need to ignore the FD_STAGE_DRAW which will be set, so we
454 * don't enable queries which should be paused during internal
457 if ((batch
->stage
== FD_STAGE_BLIT
) &&
458 (stage
!= FD_STAGE_NULL
))
461 if (ctx
->query_set_stage
)
462 ctx
->query_set_stage(batch
, stage
);
464 batch
->stage
= stage
;
467 void fd_context_setup_common_vbos(struct fd_context
*ctx
);
468 void fd_context_cleanup_common_vbos(struct fd_context
*ctx
);
470 struct pipe_context
* fd_context_init(struct fd_context
*ctx
,
471 struct pipe_screen
*pscreen
, const uint8_t *primtypes
,
472 void *priv
, unsigned flags
);
474 void fd_context_destroy(struct pipe_context
*pctx
);
476 #endif /* FREEDRENO_CONTEXT_H_ */