2 * Copyright © 2014-2017 Broadcom
3 * Copyright (C) 2012 Rob Clark <robclark@freedesktop.org>
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
29 #include "broadcom/common/v3d_macros.h"
34 #include "pipe/p_context.h"
35 #include "pipe/p_state.h"
36 #include "util/bitset.h"
37 #include "util/slab.h"
40 #include "v3d_screen.h"
44 void v3d_job_add_bo(struct v3d_job
*job
, struct v3d_bo
*bo
);
46 #include "v3d_bufmgr.h"
47 #include "v3d_resource.h"
50 #ifdef USE_V3D_SIMULATOR
51 #define using_v3d_simulator true
53 #define using_v3d_simulator false
56 #define VC5_DIRTY_BLEND (1 << 0)
57 #define VC5_DIRTY_RASTERIZER (1 << 1)
58 #define VC5_DIRTY_ZSA (1 << 2)
59 #define VC5_DIRTY_FRAGTEX (1 << 3)
60 #define VC5_DIRTY_VERTTEX (1 << 4)
62 #define VC5_DIRTY_BLEND_COLOR (1 << 7)
63 #define VC5_DIRTY_STENCIL_REF (1 << 8)
64 #define VC5_DIRTY_SAMPLE_STATE (1 << 9)
65 #define VC5_DIRTY_FRAMEBUFFER (1 << 10)
66 #define VC5_DIRTY_STIPPLE (1 << 11)
67 #define VC5_DIRTY_VIEWPORT (1 << 12)
68 #define VC5_DIRTY_CONSTBUF (1 << 13)
69 #define VC5_DIRTY_VTXSTATE (1 << 14)
70 #define VC5_DIRTY_VTXBUF (1 << 15)
71 #define VC5_DIRTY_SCISSOR (1 << 17)
72 #define VC5_DIRTY_FLAT_SHADE_FLAGS (1 << 18)
73 #define VC5_DIRTY_PRIM_MODE (1 << 19)
74 #define VC5_DIRTY_CLIP (1 << 20)
75 #define VC5_DIRTY_UNCOMPILED_VS (1 << 21)
76 #define VC5_DIRTY_UNCOMPILED_FS (1 << 22)
77 #define VC5_DIRTY_COMPILED_CS (1 << 23)
78 #define VC5_DIRTY_COMPILED_VS (1 << 24)
79 #define VC5_DIRTY_COMPILED_FS (1 << 25)
80 #define VC5_DIRTY_FS_INPUTS (1 << 26)
81 #define VC5_DIRTY_STREAMOUT (1 << 27)
82 #define VC5_DIRTY_OQ (1 << 28)
83 #define VC5_DIRTY_CENTROID_FLAGS (1 << 29)
84 #define VC5_DIRTY_NOPERSPECTIVE_FLAGS (1 << 30)
85 #define VC5_DIRTY_SSBO (1 << 31)
87 #define VC5_MAX_FS_INPUTS 64
89 struct v3d_sampler_view
{
90 struct pipe_sampler_view base
;
93 /* Precomputed swizzles to pass in to the shader key. */
96 uint8_t texture_shader_state
[32];
97 /* V3D 4.x: Texture state struct. */
100 /* Actual texture to be read by this sampler view. May be different
101 * from base.texture in the case of having a shadow tiled copy of a
104 struct pipe_resource
*texture
;
107 struct v3d_sampler_state
{
108 struct pipe_sampler_state base
;
112 /* V3D 3.x: Packed texture state. */
113 uint8_t texture_shader_state
[32];
114 /* V3D 4.x: Sampler state struct. */
118 struct v3d_texture_stateobj
{
119 struct pipe_sampler_view
*textures
[PIPE_MAX_SAMPLERS
];
120 unsigned num_textures
;
121 struct pipe_sampler_state
*samplers
[PIPE_MAX_SAMPLERS
];
122 unsigned num_samplers
;
123 struct v3d_cl_reloc texture_state
[PIPE_MAX_SAMPLERS
];
126 struct v3d_shader_uniform_info
{
127 enum quniform_contents
*contents
;
132 struct v3d_uncompiled_shader
{
133 /** A name for this program, so you can track it in shader-db output. */
135 /** How many variants of this program were compiled, for shader-db. */
136 uint32_t compiled_variant_count
;
137 struct pipe_shader_state base
;
138 uint32_t num_tf_outputs
;
139 struct v3d_varying_slot
*tf_outputs
;
140 uint16_t tf_specs
[16];
141 uint16_t tf_specs_psiz
[16];
142 uint32_t num_tf_specs
;
145 * Flag for if the NIR in this shader originally came from TGSI. If
146 * so, we need to do some fixups at compile time, due to missing
147 * information in TGSI that exists in NIR.
152 struct v3d_compiled_shader
{
153 struct pipe_resource
*resource
;
157 struct v3d_prog_data
*base
;
158 struct v3d_vs_prog_data
*vs
;
159 struct v3d_fs_prog_data
*fs
;
163 * VC5_DIRTY_* flags that, when set in v3d->dirty, mean that the
164 * uniforms have to be rewritten (and therefore the shader state
167 uint32_t uniform_dirty_bits
;
170 struct v3d_program_stateobj
{
171 struct v3d_uncompiled_shader
*bind_vs
, *bind_fs
;
172 struct v3d_compiled_shader
*cs
, *vs
, *fs
;
174 struct v3d_bo
*spill_bo
;
175 int spill_size_per_thread
;
178 struct v3d_constbuf_stateobj
{
179 struct pipe_constant_buffer cb
[PIPE_MAX_CONSTANT_BUFFERS
];
180 uint32_t enabled_mask
;
184 struct v3d_vertexbuf_stateobj
{
185 struct pipe_vertex_buffer vb
[PIPE_MAX_ATTRIBS
];
187 uint32_t enabled_mask
;
191 struct v3d_vertex_stateobj
{
192 struct pipe_vertex_element pipe
[VC5_MAX_ATTRIBUTES
];
193 unsigned num_elements
;
195 uint8_t attrs
[16 * VC5_MAX_ATTRIBUTES
];
196 struct pipe_resource
*defaults
;
197 uint32_t defaults_offset
;
200 struct v3d_streamout_stateobj
{
201 struct pipe_stream_output_target
*targets
[PIPE_MAX_SO_BUFFERS
];
202 /* Number of vertices we've written into the buffer so far. */
203 uint32_t offsets
[PIPE_MAX_SO_BUFFERS
];
204 unsigned num_targets
;
207 struct v3d_ssbo_stateobj
{
208 struct pipe_shader_buffer sb
[PIPE_MAX_SHADER_BUFFERS
];
209 uint32_t enabled_mask
;
212 /* Hash table key for v3d->jobs */
214 struct pipe_surface
*cbufs
[4];
215 struct pipe_surface
*zsbuf
;
219 VC5_EZ_UNDECIDED
= 0,
226 * A complete bin/render job.
228 * This is all of the state necessary to submit a bin/render to the kernel.
229 * We want to be able to have multiple in progress at a time, so that we don't
230 * need to flush an existing CL just to switch to rendering to a new render
231 * target (which would mean reading back from the old render target when
232 * starting to render to it again).
235 struct v3d_context
*v3d
;
238 struct v3d_cl indirect
;
239 struct v3d_bo
*tile_alloc
;
240 struct v3d_bo
*tile_state
;
241 uint32_t shader_rec_count
;
243 struct drm_v3d_submit_cl submit
;
246 * Set of all BOs referenced by the job. This will be used for making
247 * the list of BOs that the kernel will need to have paged in to
252 /** Sum of the sizes of the BOs referenced by the job. */
253 uint32_t referenced_size
;
255 struct set
*write_prscs
;
257 /* Size of the submit.bo_handles array. */
258 uint32_t bo_handles_size
;
260 /** @{ Surfaces to submit rendering for. */
261 struct pipe_surface
*cbufs
[4];
262 struct pipe_surface
*zsbuf
;
265 * Bounding box of the scissor across all queued drawing.
267 * Note that the max values are exclusive.
275 * Width/height of the color framebuffer being rendered to,
276 * for VC5_TILE_RENDERING_MODE_CONFIG.
279 uint32_t draw_height
;
281 /** @{ Tile information, depending on MSAA and float color buffer. */
282 uint32_t draw_tiles_x
; /** @< Number of tiles wide for framebuffer. */
283 uint32_t draw_tiles_y
; /** @< Number of tiles high for framebuffer. */
285 uint32_t tile_width
; /** @< Width of a tile. */
286 uint32_t tile_height
; /** @< Height of a tile. */
287 /** maximum internal_bpp of all color render targets. */
288 uint32_t internal_bpp
;
290 /** Whether the current rendering is in a 4X MSAA tile buffer. */
294 /* Bitmask of PIPE_CLEAR_* of buffers that were cleared before the
298 /* Bitmask of PIPE_CLEAR_* of buffers that have been read by a draw
299 * call without having been cleared first.
302 /* Bitmask of PIPE_CLEAR_* of buffers that have been rendered to
303 * (either clears or draws) and should be stored.
306 uint32_t clear_color
[4][4];
311 * Set if some drawing (triangles, blits, or just a glClear()) has
312 * been done to the FBO, meaning that we need to
313 * DRM_IOCTL_VC5_SUBMIT_CL.
317 /* Set if any shader has dirtied cachelines in the TMU that need to be
318 * flushed before job end.
323 * Set if a packet enabling TF has been emitted in the job (V3D 4.x).
328 * Current EZ state for drawing. Updated at the start of draw after
329 * we've decided on the shader being rendered.
331 enum v3d_ez_state ez_state
;
333 * The first EZ state that was used for drawing with a decided EZ
334 * direction (so either UNDECIDED, GT, or LT).
336 enum v3d_ez_state first_ez_state
;
339 * Number of draw calls (not counting full buffer clears) queued in
342 uint32_t draw_calls_queued
;
344 struct v3d_job_key key
;
348 struct pipe_context base
;
351 struct v3d_screen
*screen
;
353 /** The 3D rendering job for the currently bound FBO. */
356 /* Map from struct v3d_job_key to the job for that FBO.
358 struct hash_table
*jobs
;
361 * Map from v3d_resource to a job writing to that resource.
363 * Primarily for flushing jobs rendering to textures that are now
366 struct hash_table
*write_jobs
;
368 struct slab_child_pool transfer_pool
;
369 struct blitter_context
*blitter
;
371 /** bitfield of VC5_DIRTY_* */
374 struct primconvert_context
*primconvert
;
376 struct hash_table
*fs_cache
, *vs_cache
;
377 uint32_t next_uncompiled_program_id
;
378 uint64_t next_compiled_program_id
;
380 struct v3d_compiler_state
*compiler_state
;
384 /** Maximum index buffer valid for the current shader_rec. */
387 /** Sync object that our RCL or TFU job will update as its out_sync. */
390 /* Stream uploader used by gallium internals. This could also be used
391 * by driver internals, but we tend to use the v3d_cl.h interfaces
394 struct u_upload_mgr
*uploader
;
395 /* State uploader used inside the driver. This is for packing bits of
396 * long-term state inside buffers, since the kernel interfaces
397 * allocate a page at a time.
399 struct u_upload_mgr
*state_uploader
;
401 /** @{ Current pipeline state objects */
402 struct pipe_scissor_state scissor
;
403 struct v3d_blend_state
*blend
;
404 struct v3d_rasterizer_state
*rasterizer
;
405 struct v3d_depth_stencil_alpha_state
*zsa
;
407 struct v3d_program_stateobj prog
;
409 struct v3d_vertex_stateobj
*vtx
;
412 struct pipe_blend_color f
;
415 struct pipe_stencil_ref stencil_ref
;
416 unsigned sample_mask
;
417 struct pipe_framebuffer_state framebuffer
;
419 /* Per render target, whether we should swap the R and B fields in the
420 * shader's color output and in blending. If render targets disagree
421 * on the R/B swap and use the constant color, then we would need to
422 * fall back to in-shader blending.
424 uint8_t swap_color_rb
;
426 /* Per render target, whether we should treat the dst alpha values as
429 * For RGBX formats, the tile buffer's alpha channel will be
432 uint8_t blend_dst_alpha_one
;
436 uint32_t tf_prims_generated
;
437 uint32_t prims_generated
;
439 struct pipe_poly_stipple stipple
;
440 struct pipe_clip_state clip
;
441 struct pipe_viewport_state viewport
;
442 struct v3d_ssbo_stateobj ssbo
[PIPE_SHADER_TYPES
];
443 struct v3d_constbuf_stateobj constbuf
[PIPE_SHADER_TYPES
];
444 struct v3d_texture_stateobj tex
[PIPE_SHADER_TYPES
];
445 struct v3d_vertexbuf_stateobj vertexbuf
;
446 struct v3d_streamout_stateobj streamout
;
447 struct v3d_bo
*current_oq
;
448 struct pipe_debug_callback debug
;
452 struct v3d_rasterizer_state
{
453 struct pipe_rasterizer_state base
;
457 uint8_t depth_offset
[9];
458 uint8_t depth_offset_z16
[9];
461 struct v3d_depth_stencil_alpha_state
{
462 struct pipe_depth_stencil_alpha_state base
;
464 enum v3d_ez_state ez_state
;
466 uint8_t stencil_front
[6];
467 uint8_t stencil_back
[6];
470 struct v3d_blend_state
{
471 struct pipe_blend_state base
;
473 /* Per-RT mask of whether blending is enabled. */
474 uint8_t blend_enables
;
477 #define perf_debug(...) do { \
478 if (unlikely(V3D_DEBUG & V3D_DEBUG_PERF)) \
479 fprintf(stderr, __VA_ARGS__); \
480 if (unlikely(v3d->debug.debug_message)) \
481 pipe_debug_message(&v3d->debug, PERF_INFO, __VA_ARGS__); \
484 #define foreach_bit(b, mask) \
485 for (uint32_t _m = (mask), b; _m && ({(b) = u_bit_scan(&_m); 1;});)
487 static inline struct v3d_context
*
488 v3d_context(struct pipe_context
*pcontext
)
490 return (struct v3d_context
*)pcontext
;
493 static inline struct v3d_sampler_view
*
494 v3d_sampler_view(struct pipe_sampler_view
*psview
)
496 return (struct v3d_sampler_view
*)psview
;
499 static inline struct v3d_sampler_state
*
500 v3d_sampler_state(struct pipe_sampler_state
*psampler
)
502 return (struct v3d_sampler_state
*)psampler
;
505 struct pipe_context
*v3d_context_create(struct pipe_screen
*pscreen
,
506 void *priv
, unsigned flags
);
507 void v3d_program_init(struct pipe_context
*pctx
);
508 void v3d_program_fini(struct pipe_context
*pctx
);
509 void v3d_query_init(struct pipe_context
*pctx
);
511 void v3d_simulator_init(struct v3d_screen
*screen
);
512 void v3d_simulator_destroy(struct v3d_screen
*screen
);
513 int v3d_simulator_ioctl(int fd
, unsigned long request
, void *arg
);
514 void v3d_simulator_open_from_handle(int fd
, int handle
, uint32_t size
);
517 v3d_ioctl(int fd
, unsigned long request
, void *arg
)
519 if (using_v3d_simulator
)
520 return v3d_simulator_ioctl(fd
, request
, arg
);
522 return drmIoctl(fd
, request
, arg
);
525 void v3d_set_shader_uniform_dirty_flags(struct v3d_compiled_shader
*shader
);
526 struct v3d_cl_reloc
v3d_write_uniforms(struct v3d_context
*v3d
,
527 struct v3d_compiled_shader
*shader
,
528 enum pipe_shader_type stage
);
530 void v3d_flush(struct pipe_context
*pctx
);
531 void v3d_job_init(struct v3d_context
*v3d
);
532 struct v3d_job
*v3d_get_job(struct v3d_context
*v3d
,
533 struct pipe_surface
**cbufs
,
534 struct pipe_surface
*zsbuf
);
535 struct v3d_job
*v3d_get_job_for_fbo(struct v3d_context
*v3d
);
536 void v3d_job_add_bo(struct v3d_job
*job
, struct v3d_bo
*bo
);
537 void v3d_job_add_write_resource(struct v3d_job
*job
, struct pipe_resource
*prsc
);
538 void v3d_job_submit(struct v3d_context
*v3d
, struct v3d_job
*job
);
539 void v3d_flush_jobs_writing_resource(struct v3d_context
*v3d
,
540 struct pipe_resource
*prsc
);
541 void v3d_flush_jobs_reading_resource(struct v3d_context
*v3d
,
542 struct pipe_resource
*prsc
);
543 void v3d_update_compiled_shaders(struct v3d_context
*v3d
, uint8_t prim_mode
);
545 bool v3d_rt_format_supported(const struct v3d_device_info
*devinfo
,
547 bool v3d_tex_format_supported(const struct v3d_device_info
*devinfo
,
549 uint8_t v3d_get_rt_format(const struct v3d_device_info
*devinfo
, enum pipe_format f
);
550 uint8_t v3d_get_tex_format(const struct v3d_device_info
*devinfo
, enum pipe_format f
);
551 uint8_t v3d_get_tex_return_size(const struct v3d_device_info
*devinfo
,
553 enum pipe_tex_compare compare
);
554 uint8_t v3d_get_tex_return_channels(const struct v3d_device_info
*devinfo
,
556 const uint8_t *v3d_get_format_swizzle(const struct v3d_device_info
*devinfo
,
558 void v3d_get_internal_type_bpp_for_output_format(const struct v3d_device_info
*devinfo
,
562 bool v3d_tfu_supports_tex_format(const struct v3d_device_info
*devinfo
,
563 uint32_t tex_format
);
565 void v3d_init_query_functions(struct v3d_context
*v3d
);
566 void v3d_blit(struct pipe_context
*pctx
, const struct pipe_blit_info
*blit_info
);
567 void v3d_blitter_save(struct v3d_context
*v3d
);
568 boolean
v3d_generate_mipmap(struct pipe_context
*pctx
,
569 struct pipe_resource
*prsc
,
570 enum pipe_format format
,
571 unsigned int base_level
,
572 unsigned int last_level
,
573 unsigned int first_layer
,
574 unsigned int last_layer
);
576 struct v3d_fence
*v3d_fence_create(struct v3d_context
*v3d
);
579 # include "v3dx_context.h"
581 # define v3dX(x) v3d33_##x
582 # include "v3dx_context.h"
585 # define v3dX(x) v3d41_##x
586 # include "v3dx_context.h"
590 #endif /* VC5_CONTEXT_H */