1/*
2 * Copyright (C) 2019 Alyssa Rosenzweig
3 * Copyright (C) 2014-2017 Broadcom
4 *
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:
11 *
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
14 * Software.
15 *
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 FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 *
24 */
25
26#ifndef __PAN_JOB_H__
27#define __PAN_JOB_H__
28
29#include "util/u_dynarray.h"
30#include "pipe/p_state.h"
31#include "pan_cs.h"
32#include "pan_mempool.h"
33#include "pan_resource.h"
34#include "pan_scoreboard.h"
35
36/* A panfrost_batch corresponds to a bound FBO we're rendering to,
37 * collecting over multiple draws. */
38
39struct panfrost_batch {
40        struct panfrost_context *ctx;
41        struct pipe_framebuffer_state key;
42
43        /* Sequence number used to implement LRU eviction when all batch slots are used */
44        uint64_t seqnum;
45
46        /* Buffers cleared (PIPE_CLEAR_* bitmask) */
47        unsigned clear;
48
49        /* Buffers drawn */
50        unsigned draws;
51
52        /* Buffers read */
53        unsigned read;
54
55        /* Buffers needing resolve to memory */
56        unsigned resolve;
57
58        /* Packed clear values, indexed by both render target as well as word.
59         * Essentially, a single pixel is packed, with some padding to bring it
60         * up to a 32-bit interval; that pixel is then duplicated over to fill
61         * all 16-bytes */
62
63        uint32_t clear_color[PIPE_MAX_COLOR_BUFS][4];
64        float clear_depth;
65        unsigned clear_stencil;
66
67        /* Amount of thread local storage required per thread */
68        unsigned stack_size;
69
70        /* Amount of shared memory needed per workgroup (for compute) */
71        unsigned shared_size;
72
73        /* The bounding box covered by this job, taking scissors into account.
74         * Basically, the bounding box we have to run fragment shaders for */
75
76        unsigned minx, miny;
77        unsigned maxx, maxy;
78
79        /* Acts as a rasterizer discard */
80        bool scissor_culls_everything;
81
82        /* BOs referenced not in the pool */
83        int first_bo, last_bo;
84        unsigned num_bos;
85        struct util_sparse_array bos;
86
87        /* Pool owned by this batch (released when the batch is released) used for temporary descriptors */
88        struct panfrost_pool pool;
89
90        /* Pool also owned by this batch that is not CPU mapped (created as
91         * INVISIBLE) used for private GPU-internal structures, particularly
92         * varyings */
93        struct panfrost_pool invisible_pool;
94
95        /* Job scoreboarding state */
96        struct pan_scoreboard scoreboard;
97
98        /* Polygon list bound to the batch, or NULL if none bound yet */
99        struct panfrost_bo *polygon_list;
100
101        /* Scratchpad BO bound to the batch, or NULL if none bound yet */
102        struct panfrost_bo *scratchpad;
103
104        /* Shared memory BO bound to the batch, or NULL if none bound yet */
105        struct panfrost_bo *shared_memory;
106
107        /* Framebuffer descriptor. */
108        struct panfrost_ptr framebuffer;
109
110        /* Thread local storage descriptor. */
111        struct panfrost_ptr tls;
112
113        /* Tiler context */
114        struct pan_tiler_context tiler_ctx;
115
116        /* Indirect draw data */
117        struct panfrost_ptr indirect_draw_ctx;
118        unsigned indirect_draw_job_id;
119
120        /* Keep the num_work_groups sysval around for indirect dispatch */
121        mali_ptr num_wg_sysval[3];
122
123        /* Cached descriptors */
124        mali_ptr viewport;
125        mali_ptr rsd[PIPE_SHADER_TYPES];
126        mali_ptr textures[PIPE_SHADER_TYPES];
127        mali_ptr samplers[PIPE_SHADER_TYPES];
128        mali_ptr attribs[PIPE_SHADER_TYPES];
129        mali_ptr attrib_bufs[PIPE_SHADER_TYPES];
130        mali_ptr uniform_buffers[PIPE_SHADER_TYPES];
131        mali_ptr push_uniforms[PIPE_SHADER_TYPES];
132
133        /* Referenced resources */
134        struct set *resources;
135};
136
137/* Functions for managing the above */
138
139struct panfrost_batch *
140panfrost_get_batch_for_fbo(struct panfrost_context *ctx);
141
142struct panfrost_batch *
143panfrost_get_fresh_batch_for_fbo(struct panfrost_context *ctx, const char *reason);
144
145void
146panfrost_batch_add_bo(struct panfrost_batch *batch,
147                      struct panfrost_bo *bo,
148                      enum pipe_shader_type stage);
149
150void
151panfrost_batch_read_rsrc(struct panfrost_batch *batch,
152                         struct panfrost_resource *rsrc,
153                         enum pipe_shader_type stage);
154
155void
156panfrost_batch_write_rsrc(struct panfrost_batch *batch,
157                          struct panfrost_resource *rsrc,
158                          enum pipe_shader_type stage);
159
160struct panfrost_bo *
161panfrost_batch_create_bo(struct panfrost_batch *batch, size_t size,
162                         uint32_t create_flags, uint32_t access_flags,
163                         const char *label);
164
165void
166panfrost_flush_all_batches(struct panfrost_context *ctx, const char *reason);
167
168void
169panfrost_flush_batches_accessing_rsrc(struct panfrost_context *ctx,
170                                      struct panfrost_resource *rsrc,
171                                      const char *reason);
172
173void
174panfrost_flush_writer(struct panfrost_context *ctx,
175                      struct panfrost_resource *rsrc,
176                      const char *reason);
177
178void
179panfrost_batch_adjust_stack_size(struct panfrost_batch *batch);
180
181struct panfrost_bo *
182panfrost_batch_get_scratchpad(struct panfrost_batch *batch, unsigned size, unsigned thread_tls_alloc, unsigned core_count);
183
184struct panfrost_bo *
185panfrost_batch_get_shared_memory(struct panfrost_batch *batch, unsigned size, unsigned workgroup_count);
186
187void
188panfrost_batch_clear(struct panfrost_batch *batch,
189                     unsigned buffers,
190                     const union pipe_color_union *color,
191                     double depth, unsigned stencil);
192
193void
194panfrost_batch_union_scissor(struct panfrost_batch *batch,
195                             unsigned minx, unsigned miny,
196                             unsigned maxx, unsigned maxy);
197
198#endif
199