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
30 #include "pipe/p_context.h"
31 #include "pipe/p_state.h"
32 #include "util/bitset.h"
33 #include "util/slab.h"
36 #include "vc5_screen.h"
40 void vc5_job_add_bo(struct vc5_job
*job
, struct vc5_bo
*bo
);
44 #include "vc5_bufmgr.h"
45 #include "vc5_resource.h"
48 #ifdef USE_VC5_SIMULATOR
49 #define using_vc5_simulator true
51 #define using_vc5_simulator false
54 #define VC5_DIRTY_BLEND (1 << 0)
55 #define VC5_DIRTY_RASTERIZER (1 << 1)
56 #define VC5_DIRTY_ZSA (1 << 2)
57 #define VC5_DIRTY_FRAGTEX (1 << 3)
58 #define VC5_DIRTY_VERTTEX (1 << 4)
60 #define VC5_DIRTY_BLEND_COLOR (1 << 7)
61 #define VC5_DIRTY_STENCIL_REF (1 << 8)
62 #define VC5_DIRTY_SAMPLE_MASK (1 << 9)
63 #define VC5_DIRTY_FRAMEBUFFER (1 << 10)
64 #define VC5_DIRTY_STIPPLE (1 << 11)
65 #define VC5_DIRTY_VIEWPORT (1 << 12)
66 #define VC5_DIRTY_CONSTBUF (1 << 13)
67 #define VC5_DIRTY_VTXSTATE (1 << 14)
68 #define VC5_DIRTY_VTXBUF (1 << 15)
69 #define VC5_DIRTY_SCISSOR (1 << 17)
70 #define VC5_DIRTY_FLAT_SHADE_FLAGS (1 << 18)
71 #define VC5_DIRTY_PRIM_MODE (1 << 19)
72 #define VC5_DIRTY_CLIP (1 << 20)
73 #define VC5_DIRTY_UNCOMPILED_VS (1 << 21)
74 #define VC5_DIRTY_UNCOMPILED_FS (1 << 22)
75 #define VC5_DIRTY_COMPILED_CS (1 << 23)
76 #define VC5_DIRTY_COMPILED_VS (1 << 24)
77 #define VC5_DIRTY_COMPILED_FS (1 << 25)
78 #define VC5_DIRTY_FS_INPUTS (1 << 26)
79 #define VC5_DIRTY_STREAMOUT (1 << 27)
80 #define VC5_DIRTY_OQ (1 << 28)
82 #define VC5_MAX_FS_INPUTS 64
84 struct vc5_sampler_view
{
85 struct pipe_sampler_view base
;
88 /* Precomputed swizzles to pass in to the shader key. */
91 uint8_t texture_shader_state
[32];
94 struct vc5_sampler_state
{
95 struct pipe_sampler_state base
;
99 uint8_t texture_shader_state
[32];
102 struct vc5_texture_stateobj
{
103 struct pipe_sampler_view
*textures
[PIPE_MAX_SAMPLERS
];
104 unsigned num_textures
;
105 struct pipe_sampler_state
*samplers
[PIPE_MAX_SAMPLERS
];
106 unsigned num_samplers
;
107 struct vc5_cl_reloc texture_state
[PIPE_MAX_SAMPLERS
];
110 struct vc5_shader_uniform_info
{
111 enum quniform_contents
*contents
;
116 struct vc5_uncompiled_shader
{
117 /** A name for this program, so you can track it in shader-db output. */
119 /** How many variants of this program were compiled, for shader-db. */
120 uint32_t compiled_variant_count
;
121 struct pipe_shader_state base
;
122 uint32_t num_tf_outputs
;
123 struct v3d_varying_slot
*tf_outputs
;
124 uint16_t tf_specs
[PIPE_MAX_SO_BUFFERS
];
125 uint32_t num_tf_specs
;
128 struct vc5_compiled_shader
{
132 struct v3d_prog_data
*base
;
133 struct v3d_vs_prog_data
*vs
;
134 struct v3d_fs_prog_data
*fs
;
138 * VC5_DIRTY_* flags that, when set in vc5->dirty, mean that the
139 * uniforms have to be rewritten (and therefore the shader state
142 uint32_t uniform_dirty_bits
;
145 struct vc5_program_stateobj
{
146 struct vc5_uncompiled_shader
*bind_vs
, *bind_fs
;
147 struct vc5_compiled_shader
*cs
, *vs
, *fs
;
150 struct vc5_constbuf_stateobj
{
151 struct pipe_constant_buffer cb
[PIPE_MAX_CONSTANT_BUFFERS
];
152 uint32_t enabled_mask
;
156 struct vc5_vertexbuf_stateobj
{
157 struct pipe_vertex_buffer vb
[PIPE_MAX_ATTRIBS
];
159 uint32_t enabled_mask
;
163 struct vc5_vertex_stateobj
{
164 struct pipe_vertex_element pipe
[VC5_MAX_ATTRIBUTES
];
165 unsigned num_elements
;
167 uint8_t attrs
[12 * VC5_MAX_ATTRIBUTES
];
168 struct vc5_bo
*default_attribute_values
;
171 struct vc5_streamout_stateobj
{
172 struct pipe_stream_output_target
*targets
[PIPE_MAX_SO_BUFFERS
];
173 unsigned num_targets
;
176 /* Hash table key for vc5->jobs */
178 struct pipe_surface
*cbufs
[4];
179 struct pipe_surface
*zsbuf
;
183 * A complete bin/render job.
185 * This is all of the state necessary to submit a bin/render to the kernel.
186 * We want to be able to have multiple in progress at a time, so that we don't
187 * need to flush an existing CL just to switch to rendering to a new render
188 * target (which would mean reading back from the old render target when
189 * starting to render to it again).
192 struct vc5_context
*vc5
;
195 struct vc5_cl indirect
;
196 struct vc5_bo
*tile_alloc
;
197 uint32_t shader_rec_count
;
199 struct drm_vc5_submit_cl submit
;
202 * Set of all BOs referenced by the job. This will be used for making
203 * the list of BOs that the kernel will need to have paged in to
208 struct set
*write_prscs
;
210 /* Size of the submit.bo_handles array. */
211 uint32_t bo_handles_size
;
213 /** @{ Surfaces to submit rendering for. */
214 struct pipe_surface
*cbufs
[4];
215 struct pipe_surface
*zsbuf
;
218 * Bounding box of the scissor across all queued drawing.
220 * Note that the max values are exclusive.
228 * Width/height of the color framebuffer being rendered to,
229 * for VC5_TILE_RENDERING_MODE_CONFIG.
232 uint32_t draw_height
;
234 /** @{ Tile information, depending on MSAA and float color buffer. */
235 uint32_t draw_tiles_x
; /** @< Number of tiles wide for framebuffer. */
236 uint32_t draw_tiles_y
; /** @< Number of tiles high for framebuffer. */
238 uint32_t tile_width
; /** @< Width of a tile. */
239 uint32_t tile_height
; /** @< Height of a tile. */
240 /** maximum internal_bpp of all color render targets. */
241 uint32_t internal_bpp
;
243 /** Whether the current rendering is in a 4X MSAA tile buffer. */
247 /* Bitmask of PIPE_CLEAR_* of buffers that were cleared before the
251 /* Bitmask of PIPE_CLEAR_* of buffers that have been rendered to
252 * (either clears or draws).
255 uint32_t clear_color
[4][4];
260 * Set if some drawing (triangles, blits, or just a glClear()) has
261 * been done to the FBO, meaning that we need to
262 * DRM_IOCTL_VC5_SUBMIT_CL.
267 * Set if there is a nonzero address for OCCLUSION_QUERY_COUNTER. If
268 * so, we need to disable it and flush before ending the CL, to keep
269 * the next tile from starting with it enabled.
276 * Number of draw calls (not counting full buffer clears) queued in
279 uint32_t draw_calls_queued
;
281 struct vc5_job_key key
;
285 struct pipe_context base
;
288 struct vc5_screen
*screen
;
290 /** The 3D rendering job for the currently bound FBO. */
293 /* Map from struct vc5_job_key to the job for that FBO.
295 struct hash_table
*jobs
;
298 * Map from vc5_resource to a job writing to that resource.
300 * Primarily for flushing jobs rendering to textures that are now
303 struct hash_table
*write_jobs
;
305 struct slab_child_pool transfer_pool
;
306 struct blitter_context
*blitter
;
308 /** bitfield of VC5_DIRTY_* */
311 struct primconvert_context
*primconvert
;
313 struct hash_table
*fs_cache
, *vs_cache
;
314 uint32_t next_uncompiled_program_id
;
315 uint64_t next_compiled_program_id
;
317 struct vc5_compiler_state
*compiler_state
;
321 /** Maximum index buffer valid for the current shader_rec. */
324 /** Seqno of the last CL flush's job. */
325 uint64_t last_emit_seqno
;
327 struct u_upload_mgr
*uploader
;
329 /** @{ Current pipeline state objects */
330 struct pipe_scissor_state scissor
;
331 struct pipe_blend_state
*blend
;
332 struct vc5_rasterizer_state
*rasterizer
;
333 struct vc5_depth_stencil_alpha_state
*zsa
;
335 struct vc5_texture_stateobj verttex
, fragtex
;
337 struct vc5_program_stateobj prog
;
339 struct vc5_vertex_stateobj
*vtx
;
342 struct pipe_blend_color f
;
345 struct pipe_stencil_ref stencil_ref
;
346 unsigned sample_mask
;
347 struct pipe_framebuffer_state framebuffer
;
349 /* Per render target, whether we should swap the R and B fields in the
350 * shader's color output and in blending. If render targets disagree
351 * on the R/B swap and use the constant color, then we would need to
352 * fall back to in-shader blending.
354 uint8_t swap_color_rb
;
356 /* Per render target, whether we should treat the dst alpha values as
359 * For RGBX formats, the tile buffer's alpha channel will be
362 uint8_t blend_dst_alpha_one
;
366 uint32_t tf_prims_generated
;
367 uint32_t prims_generated
;
369 struct pipe_poly_stipple stipple
;
370 struct pipe_clip_state clip
;
371 struct pipe_viewport_state viewport
;
372 struct vc5_constbuf_stateobj constbuf
[PIPE_SHADER_TYPES
];
373 struct vc5_vertexbuf_stateobj vertexbuf
;
374 struct vc5_streamout_stateobj streamout
;
375 struct vc5_bo
*current_oq
;
379 struct vc5_rasterizer_state
{
380 struct pipe_rasterizer_state base
;
382 /* VC5_CONFIGURATION_BITS */
383 uint8_t config_bits
[3];
388 * Half-float (1/8/7 bits) value of polygon offset units for
389 * VC5_PACKET_DEPTH_OFFSET
391 uint16_t offset_units
;
393 * Half-float (1/8/7 bits) value of polygon offset scale for
394 * VC5_PACKET_DEPTH_OFFSET
396 uint16_t offset_factor
;
399 struct vc5_depth_stencil_alpha_state
{
400 struct pipe_depth_stencil_alpha_state base
;
404 /** Uniforms for stencil state.
406 * Index 0 is either the front config, or the front-and-back config.
407 * Index 1 is the back config if doing separate back stencil.
408 * Index 2 is the writemask config if it's not a common mask value.
410 uint32_t stencil_uniforms
[3];
412 uint8_t stencil_front
[6];
413 uint8_t stencil_back
[6];
416 #define perf_debug(...) do { \
417 if (unlikely(V3D_DEBUG & V3D_DEBUG_PERF)) \
418 fprintf(stderr, __VA_ARGS__); \
421 static inline struct vc5_context
*
422 vc5_context(struct pipe_context
*pcontext
)
424 return (struct vc5_context
*)pcontext
;
427 static inline struct vc5_sampler_view
*
428 vc5_sampler_view(struct pipe_sampler_view
*psview
)
430 return (struct vc5_sampler_view
*)psview
;
433 static inline struct vc5_sampler_state
*
434 vc5_sampler_state(struct pipe_sampler_state
*psampler
)
436 return (struct vc5_sampler_state
*)psampler
;
439 struct pipe_context
*vc5_context_create(struct pipe_screen
*pscreen
,
440 void *priv
, unsigned flags
);
441 void vc5_draw_init(struct pipe_context
*pctx
);
442 void vc5_state_init(struct pipe_context
*pctx
);
443 void vc5_program_init(struct pipe_context
*pctx
);
444 void vc5_program_fini(struct pipe_context
*pctx
);
445 void vc5_query_init(struct pipe_context
*pctx
);
447 void vc5_simulator_init(struct vc5_screen
*screen
);
448 void vc5_simulator_init(struct vc5_screen
*screen
);
449 void vc5_simulator_destroy(struct vc5_screen
*screen
);
450 void vc5_simulator_destroy(struct vc5_screen
*screen
);
451 int vc5_simulator_flush(struct vc5_context
*vc5
,
452 struct drm_vc5_submit_cl
*args
,
453 struct vc5_job
*job
);
454 int vc5_simulator_ioctl(int fd
, unsigned long request
, void *arg
);
455 void vc5_simulator_open_from_handle(int fd
, uint32_t winsys_stride
,
456 int handle
, uint32_t size
);
459 vc5_ioctl(int fd
, unsigned long request
, void *arg
)
461 if (using_vc5_simulator
)
462 return vc5_simulator_ioctl(fd
, request
, arg
);
464 return drmIoctl(fd
, request
, arg
);
467 void vc5_set_shader_uniform_dirty_flags(struct vc5_compiled_shader
*shader
);
468 struct vc5_cl_reloc
vc5_write_uniforms(struct vc5_context
*vc5
,
469 struct vc5_compiled_shader
*shader
,
470 struct vc5_constbuf_stateobj
*cb
,
471 struct vc5_texture_stateobj
*texstate
);
473 void vc5_flush(struct pipe_context
*pctx
);
474 void vc5_job_init(struct vc5_context
*vc5
);
475 struct vc5_job
*vc5_get_job(struct vc5_context
*vc5
,
476 struct pipe_surface
**cbufs
,
477 struct pipe_surface
*zsbuf
);
478 struct vc5_job
*vc5_get_job_for_fbo(struct vc5_context
*vc5
);
479 void vc5_job_add_bo(struct vc5_job
*job
, struct vc5_bo
*bo
);
480 void vc5_job_add_write_resource(struct vc5_job
*job
, struct pipe_resource
*prsc
);
481 void vc5_job_submit(struct vc5_context
*vc5
, struct vc5_job
*job
);
482 void vc5_flush_jobs_writing_resource(struct vc5_context
*vc5
,
483 struct pipe_resource
*prsc
);
484 void vc5_flush_jobs_reading_resource(struct vc5_context
*vc5
,
485 struct pipe_resource
*prsc
);
486 void vc5_emit_state(struct pipe_context
*pctx
);
487 void vc5_update_compiled_shaders(struct vc5_context
*vc5
, uint8_t prim_mode
);
489 bool vc5_rt_format_supported(enum pipe_format f
);
490 bool vc5_tex_format_supported(enum pipe_format f
);
491 uint8_t vc5_get_rt_format(enum pipe_format f
);
492 uint8_t vc5_get_tex_format(enum pipe_format f
);
493 uint8_t vc5_get_tex_return_size(enum pipe_format f
);
494 uint8_t vc5_get_tex_return_channels(enum pipe_format f
);
495 const uint8_t *vc5_get_format_swizzle(enum pipe_format f
);
496 void vc5_get_internal_type_bpp_for_output_format(uint32_t format
,
500 void vc5_init_query_functions(struct vc5_context
*vc5
);
501 void vc5_blit(struct pipe_context
*pctx
, const struct pipe_blit_info
*blit_info
);
502 void vc5_blitter_save(struct vc5_context
*vc5
);
503 void vc5_emit_rcl(struct vc5_job
*job
);
506 #endif /* VC5_CONTEXT_H */