1bf215546Sopenharmony_ci/*
2bf215546Sopenharmony_ci * Copyright (c) 2011-2013 Luc Verhaegen <libv@skynet.be>
3bf215546Sopenharmony_ci * Copyright (c) 2017-2019 Lima Project
4bf215546Sopenharmony_ci *
5bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a
6bf215546Sopenharmony_ci * copy of this software and associated documentation files (the "Software"),
7bf215546Sopenharmony_ci * to deal in the Software without restriction, including without limitation
8bf215546Sopenharmony_ci * the rights to use, copy, modify, merge, publish, distribute, sub license,
9bf215546Sopenharmony_ci * and/or sell copies of the Software, and to permit persons to whom the
10bf215546Sopenharmony_ci * Software is furnished to do so, subject to the following conditions:
11bf215546Sopenharmony_ci *
12bf215546Sopenharmony_ci * The above copyright notice and this permission notice (including the
13bf215546Sopenharmony_ci * next paragraph) shall be included in all copies or substantial portions
14bf215546Sopenharmony_ci * of the Software.
15bf215546Sopenharmony_ci *
16bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17bf215546Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18bf215546Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
19bf215546Sopenharmony_ci * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20bf215546Sopenharmony_ci * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21bf215546Sopenharmony_ci * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22bf215546Sopenharmony_ci * DEALINGS IN THE SOFTWARE.
23bf215546Sopenharmony_ci *
24bf215546Sopenharmony_ci */
25bf215546Sopenharmony_ci
26bf215546Sopenharmony_ci#include "util/format/u_format.h"
27bf215546Sopenharmony_ci#include "util/u_debug.h"
28bf215546Sopenharmony_ci#include "util/u_draw.h"
29bf215546Sopenharmony_ci#include "util/half_float.h"
30bf215546Sopenharmony_ci#include "util/u_helpers.h"
31bf215546Sopenharmony_ci#include "util/u_inlines.h"
32bf215546Sopenharmony_ci#include "util/u_pack_color.h"
33bf215546Sopenharmony_ci#include "util/u_split_draw.h"
34bf215546Sopenharmony_ci#include "util/u_upload_mgr.h"
35bf215546Sopenharmony_ci#include "util/u_prim.h"
36bf215546Sopenharmony_ci#include "util/u_vbuf.h"
37bf215546Sopenharmony_ci#include "util/hash_table.h"
38bf215546Sopenharmony_ci
39bf215546Sopenharmony_ci#include "lima_context.h"
40bf215546Sopenharmony_ci#include "lima_screen.h"
41bf215546Sopenharmony_ci#include "lima_resource.h"
42bf215546Sopenharmony_ci#include "lima_program.h"
43bf215546Sopenharmony_ci#include "lima_bo.h"
44bf215546Sopenharmony_ci#include "lima_job.h"
45bf215546Sopenharmony_ci#include "lima_texture.h"
46bf215546Sopenharmony_ci#include "lima_util.h"
47bf215546Sopenharmony_ci#include "lima_gpu.h"
48bf215546Sopenharmony_ci
49bf215546Sopenharmony_ci#include "pan_minmax_cache.h"
50bf215546Sopenharmony_ci
51bf215546Sopenharmony_ci#include <drm-uapi/lima_drm.h>
52bf215546Sopenharmony_ci
53bf215546Sopenharmony_cistatic void
54bf215546Sopenharmony_cilima_clip_scissor_to_viewport(struct lima_context *ctx)
55bf215546Sopenharmony_ci{
56bf215546Sopenharmony_ci   struct lima_context_framebuffer *fb = &ctx->framebuffer;
57bf215546Sopenharmony_ci   struct pipe_scissor_state *cscissor = &ctx->clipped_scissor;
58bf215546Sopenharmony_ci   int viewport_left, viewport_right, viewport_bottom, viewport_top;
59bf215546Sopenharmony_ci
60bf215546Sopenharmony_ci   if (ctx->rasterizer && ctx->rasterizer->base.scissor) {
61bf215546Sopenharmony_ci      struct pipe_scissor_state *scissor = &ctx->scissor;
62bf215546Sopenharmony_ci      cscissor->minx = scissor->minx;
63bf215546Sopenharmony_ci      cscissor->maxx = scissor->maxx;
64bf215546Sopenharmony_ci      cscissor->miny = scissor->miny;
65bf215546Sopenharmony_ci      cscissor->maxy = scissor->maxy;
66bf215546Sopenharmony_ci   } else {
67bf215546Sopenharmony_ci      cscissor->minx = 0;
68bf215546Sopenharmony_ci      cscissor->maxx = fb->base.width;
69bf215546Sopenharmony_ci      cscissor->miny = 0;
70bf215546Sopenharmony_ci      cscissor->maxy = fb->base.height;
71bf215546Sopenharmony_ci   }
72bf215546Sopenharmony_ci
73bf215546Sopenharmony_ci   viewport_left = MAX2(ctx->viewport.left, 0);
74bf215546Sopenharmony_ci   cscissor->minx = MAX2(cscissor->minx, viewport_left);
75bf215546Sopenharmony_ci   viewport_right = MIN2(MAX2(ctx->viewport.right, 0), fb->base.width);
76bf215546Sopenharmony_ci   cscissor->maxx = MIN2(cscissor->maxx, viewport_right);
77bf215546Sopenharmony_ci   if (cscissor->minx > cscissor->maxx)
78bf215546Sopenharmony_ci      cscissor->minx = cscissor->maxx;
79bf215546Sopenharmony_ci
80bf215546Sopenharmony_ci   viewport_bottom = MAX2(ctx->viewport.bottom, 0);
81bf215546Sopenharmony_ci   cscissor->miny = MAX2(cscissor->miny, viewport_bottom);
82bf215546Sopenharmony_ci   viewport_top = MIN2(MAX2(ctx->viewport.top, 0), fb->base.height);
83bf215546Sopenharmony_ci   cscissor->maxy = MIN2(cscissor->maxy, viewport_top);
84bf215546Sopenharmony_ci   if (cscissor->miny > cscissor->maxy)
85bf215546Sopenharmony_ci      cscissor->miny = cscissor->maxy;
86bf215546Sopenharmony_ci}
87bf215546Sopenharmony_ci
88bf215546Sopenharmony_cistatic void
89bf215546Sopenharmony_cilima_extend_viewport(struct lima_context *ctx, const struct pipe_draw_info *info)
90bf215546Sopenharmony_ci{
91bf215546Sopenharmony_ci   /* restore the original values */
92bf215546Sopenharmony_ci   ctx->ext_viewport.left = ctx->viewport.left;
93bf215546Sopenharmony_ci   ctx->ext_viewport.right = ctx->viewport.right;
94bf215546Sopenharmony_ci   ctx->ext_viewport.bottom = ctx->viewport.bottom;
95bf215546Sopenharmony_ci   ctx->ext_viewport.top = ctx->viewport.top;
96bf215546Sopenharmony_ci
97bf215546Sopenharmony_ci   if (info->mode != PIPE_PRIM_LINES)
98bf215546Sopenharmony_ci      return;
99bf215546Sopenharmony_ci
100bf215546Sopenharmony_ci   if (!ctx->rasterizer)
101bf215546Sopenharmony_ci      return;
102bf215546Sopenharmony_ci
103bf215546Sopenharmony_ci   float line_width = ctx->rasterizer->base.line_width;
104bf215546Sopenharmony_ci
105bf215546Sopenharmony_ci   if (line_width == 1.0f)
106bf215546Sopenharmony_ci      return;
107bf215546Sopenharmony_ci
108bf215546Sopenharmony_ci   ctx->ext_viewport.left = ctx->viewport.left - line_width / 2;
109bf215546Sopenharmony_ci   ctx->ext_viewport.right = ctx->viewport.right + line_width / 2;
110bf215546Sopenharmony_ci   ctx->ext_viewport.bottom = ctx->viewport.bottom - line_width / 2;
111bf215546Sopenharmony_ci   ctx->ext_viewport.top = ctx->viewport.top + line_width / 2;
112bf215546Sopenharmony_ci}
113bf215546Sopenharmony_ci
114bf215546Sopenharmony_cistatic bool
115bf215546Sopenharmony_cilima_is_scissor_zero(struct lima_context *ctx)
116bf215546Sopenharmony_ci{
117bf215546Sopenharmony_ci   struct pipe_scissor_state *cscissor = &ctx->clipped_scissor;
118bf215546Sopenharmony_ci
119bf215546Sopenharmony_ci   return cscissor->minx == cscissor->maxx || cscissor->miny == cscissor->maxy;
120bf215546Sopenharmony_ci}
121bf215546Sopenharmony_ci
122bf215546Sopenharmony_cistatic void
123bf215546Sopenharmony_cilima_update_job_wb(struct lima_context *ctx, unsigned buffers)
124bf215546Sopenharmony_ci{
125bf215546Sopenharmony_ci   struct lima_job *job = lima_job_get(ctx);
126bf215546Sopenharmony_ci   struct lima_context_framebuffer *fb = &ctx->framebuffer;
127bf215546Sopenharmony_ci
128bf215546Sopenharmony_ci   /* add to job when the buffer is dirty and resolve is clear (not added before) */
129bf215546Sopenharmony_ci   if (fb->base.nr_cbufs && (buffers & PIPE_CLEAR_COLOR0) &&
130bf215546Sopenharmony_ci       !(job->resolve & PIPE_CLEAR_COLOR0)) {
131bf215546Sopenharmony_ci      struct lima_resource *res = lima_resource(fb->base.cbufs[0]->texture);
132bf215546Sopenharmony_ci      lima_flush_job_accessing_bo(ctx, res->bo, true);
133bf215546Sopenharmony_ci      _mesa_hash_table_insert(ctx->write_jobs, &res->base, job);
134bf215546Sopenharmony_ci      lima_job_add_bo(job, LIMA_PIPE_PP, res->bo, LIMA_SUBMIT_BO_WRITE);
135bf215546Sopenharmony_ci   }
136bf215546Sopenharmony_ci
137bf215546Sopenharmony_ci   /* add to job when the buffer is dirty and resolve is clear (not added before) */
138bf215546Sopenharmony_ci   if (fb->base.zsbuf && (buffers & (PIPE_CLEAR_DEPTH | PIPE_CLEAR_STENCIL)) &&
139bf215546Sopenharmony_ci       !(job->resolve & (PIPE_CLEAR_DEPTH | PIPE_CLEAR_STENCIL))) {
140bf215546Sopenharmony_ci      struct lima_resource *res = lima_resource(fb->base.zsbuf->texture);
141bf215546Sopenharmony_ci      lima_flush_job_accessing_bo(ctx, res->bo, true);
142bf215546Sopenharmony_ci      _mesa_hash_table_insert(ctx->write_jobs, &res->base, job);
143bf215546Sopenharmony_ci      lima_job_add_bo(job, LIMA_PIPE_PP, res->bo, LIMA_SUBMIT_BO_WRITE);
144bf215546Sopenharmony_ci   }
145bf215546Sopenharmony_ci
146bf215546Sopenharmony_ci   job->resolve |= buffers;
147bf215546Sopenharmony_ci}
148bf215546Sopenharmony_ci
149bf215546Sopenharmony_cistatic void
150bf215546Sopenharmony_cilima_clear(struct pipe_context *pctx, unsigned buffers, const struct pipe_scissor_state *scissor_state,
151bf215546Sopenharmony_ci           const union pipe_color_union *color, double depth, unsigned stencil)
152bf215546Sopenharmony_ci{
153bf215546Sopenharmony_ci   struct lima_context *ctx = lima_context(pctx);
154bf215546Sopenharmony_ci   struct lima_job *job = lima_job_get(ctx);
155bf215546Sopenharmony_ci
156bf215546Sopenharmony_ci   /* flush if this job already contains any draw, otherwise multi clear can be
157bf215546Sopenharmony_ci    * combined into a single job */
158bf215546Sopenharmony_ci   if (lima_job_has_draw_pending(job)) {
159bf215546Sopenharmony_ci      lima_do_job(job);
160bf215546Sopenharmony_ci      job = lima_job_get(ctx);
161bf215546Sopenharmony_ci   }
162bf215546Sopenharmony_ci
163bf215546Sopenharmony_ci   lima_update_job_wb(ctx, buffers);
164bf215546Sopenharmony_ci
165bf215546Sopenharmony_ci   /* no need to reload if cleared */
166bf215546Sopenharmony_ci   if (ctx->framebuffer.base.nr_cbufs && (buffers & PIPE_CLEAR_COLOR0)) {
167bf215546Sopenharmony_ci      struct lima_surface *surf = lima_surface(ctx->framebuffer.base.cbufs[0]);
168bf215546Sopenharmony_ci      surf->reload &= ~PIPE_CLEAR_COLOR0;
169bf215546Sopenharmony_ci   }
170bf215546Sopenharmony_ci
171bf215546Sopenharmony_ci   struct lima_job_clear *clear = &job->clear;
172bf215546Sopenharmony_ci   clear->buffers = buffers;
173bf215546Sopenharmony_ci
174bf215546Sopenharmony_ci   if (buffers & PIPE_CLEAR_COLOR0) {
175bf215546Sopenharmony_ci      clear->color_8pc =
176bf215546Sopenharmony_ci         ((uint32_t)float_to_ubyte(color->f[3]) << 24) |
177bf215546Sopenharmony_ci         ((uint32_t)float_to_ubyte(color->f[2]) << 16) |
178bf215546Sopenharmony_ci         ((uint32_t)float_to_ubyte(color->f[1]) << 8) |
179bf215546Sopenharmony_ci         float_to_ubyte(color->f[0]);
180bf215546Sopenharmony_ci
181bf215546Sopenharmony_ci      clear->color_16pc =
182bf215546Sopenharmony_ci         ((uint64_t)float_to_ushort(color->f[3]) << 48) |
183bf215546Sopenharmony_ci         ((uint64_t)float_to_ushort(color->f[2]) << 32) |
184bf215546Sopenharmony_ci         ((uint64_t)float_to_ushort(color->f[1]) << 16) |
185bf215546Sopenharmony_ci         float_to_ushort(color->f[0]);
186bf215546Sopenharmony_ci   }
187bf215546Sopenharmony_ci
188bf215546Sopenharmony_ci   struct lima_surface *zsbuf = lima_surface(ctx->framebuffer.base.zsbuf);
189bf215546Sopenharmony_ci
190bf215546Sopenharmony_ci   if (buffers & PIPE_CLEAR_DEPTH) {
191bf215546Sopenharmony_ci      clear->depth = util_pack_z(PIPE_FORMAT_Z24X8_UNORM, depth);
192bf215546Sopenharmony_ci      if (zsbuf)
193bf215546Sopenharmony_ci         zsbuf->reload &= ~PIPE_CLEAR_DEPTH;
194bf215546Sopenharmony_ci   }
195bf215546Sopenharmony_ci
196bf215546Sopenharmony_ci   if (buffers & PIPE_CLEAR_STENCIL) {
197bf215546Sopenharmony_ci      clear->stencil = stencil;
198bf215546Sopenharmony_ci      if (zsbuf)
199bf215546Sopenharmony_ci         zsbuf->reload &= ~PIPE_CLEAR_STENCIL;
200bf215546Sopenharmony_ci   }
201bf215546Sopenharmony_ci
202bf215546Sopenharmony_ci   ctx->dirty |= LIMA_CONTEXT_DIRTY_CLEAR;
203bf215546Sopenharmony_ci
204bf215546Sopenharmony_ci   lima_damage_rect_union(&job->damage_rect,
205bf215546Sopenharmony_ci                          0, ctx->framebuffer.base.width,
206bf215546Sopenharmony_ci                          0, ctx->framebuffer.base.height);
207bf215546Sopenharmony_ci}
208bf215546Sopenharmony_ci
209bf215546Sopenharmony_cienum lima_attrib_type {
210bf215546Sopenharmony_ci   LIMA_ATTRIB_FLOAT = 0x000,
211bf215546Sopenharmony_ci   LIMA_ATTRIB_I32   = 0x001,
212bf215546Sopenharmony_ci   LIMA_ATTRIB_U32   = 0x002,
213bf215546Sopenharmony_ci   LIMA_ATTRIB_FP16  = 0x003,
214bf215546Sopenharmony_ci   LIMA_ATTRIB_I16   = 0x004,
215bf215546Sopenharmony_ci   LIMA_ATTRIB_U16   = 0x005,
216bf215546Sopenharmony_ci   LIMA_ATTRIB_I8    = 0x006,
217bf215546Sopenharmony_ci   LIMA_ATTRIB_U8    = 0x007,
218bf215546Sopenharmony_ci   LIMA_ATTRIB_I8N   = 0x008,
219bf215546Sopenharmony_ci   LIMA_ATTRIB_U8N   = 0x009,
220bf215546Sopenharmony_ci   LIMA_ATTRIB_I16N  = 0x00A,
221bf215546Sopenharmony_ci   LIMA_ATTRIB_U16N  = 0x00B,
222bf215546Sopenharmony_ci   LIMA_ATTRIB_I32N  = 0x00D,
223bf215546Sopenharmony_ci   LIMA_ATTRIB_U32N  = 0x00E,
224bf215546Sopenharmony_ci   LIMA_ATTRIB_FIXED = 0x101
225bf215546Sopenharmony_ci};
226bf215546Sopenharmony_ci
227bf215546Sopenharmony_cistatic enum lima_attrib_type
228bf215546Sopenharmony_cilima_pipe_format_to_attrib_type(enum pipe_format format)
229bf215546Sopenharmony_ci{
230bf215546Sopenharmony_ci   const struct util_format_description *desc = util_format_description(format);
231bf215546Sopenharmony_ci   int i = util_format_get_first_non_void_channel(format);
232bf215546Sopenharmony_ci   const struct util_format_channel_description *c = desc->channel + i;
233bf215546Sopenharmony_ci
234bf215546Sopenharmony_ci   switch (c->type) {
235bf215546Sopenharmony_ci   case UTIL_FORMAT_TYPE_FLOAT:
236bf215546Sopenharmony_ci      if (c->size == 16)
237bf215546Sopenharmony_ci         return LIMA_ATTRIB_FP16;
238bf215546Sopenharmony_ci      else
239bf215546Sopenharmony_ci         return LIMA_ATTRIB_FLOAT;
240bf215546Sopenharmony_ci   case UTIL_FORMAT_TYPE_FIXED:
241bf215546Sopenharmony_ci      return LIMA_ATTRIB_FIXED;
242bf215546Sopenharmony_ci   case UTIL_FORMAT_TYPE_SIGNED:
243bf215546Sopenharmony_ci      if (c->size == 8) {
244bf215546Sopenharmony_ci         if (c->normalized)
245bf215546Sopenharmony_ci            return LIMA_ATTRIB_I8N;
246bf215546Sopenharmony_ci         else
247bf215546Sopenharmony_ci            return LIMA_ATTRIB_I8;
248bf215546Sopenharmony_ci      }
249bf215546Sopenharmony_ci      else if (c->size == 16) {
250bf215546Sopenharmony_ci         if (c->normalized)
251bf215546Sopenharmony_ci            return LIMA_ATTRIB_I16N;
252bf215546Sopenharmony_ci         else
253bf215546Sopenharmony_ci            return LIMA_ATTRIB_I16;
254bf215546Sopenharmony_ci      }
255bf215546Sopenharmony_ci      else if (c->size == 32) {
256bf215546Sopenharmony_ci         if (c->normalized)
257bf215546Sopenharmony_ci            return LIMA_ATTRIB_I32N;
258bf215546Sopenharmony_ci         else
259bf215546Sopenharmony_ci            return LIMA_ATTRIB_I32;
260bf215546Sopenharmony_ci      }
261bf215546Sopenharmony_ci      break;
262bf215546Sopenharmony_ci   case UTIL_FORMAT_TYPE_UNSIGNED:
263bf215546Sopenharmony_ci      if (c->size == 8) {
264bf215546Sopenharmony_ci         if (c->normalized)
265bf215546Sopenharmony_ci            return LIMA_ATTRIB_U8N;
266bf215546Sopenharmony_ci         else
267bf215546Sopenharmony_ci            return LIMA_ATTRIB_U8;
268bf215546Sopenharmony_ci      }
269bf215546Sopenharmony_ci      else if (c->size == 16) {
270bf215546Sopenharmony_ci         if (c->normalized)
271bf215546Sopenharmony_ci            return LIMA_ATTRIB_U16N;
272bf215546Sopenharmony_ci         else
273bf215546Sopenharmony_ci            return LIMA_ATTRIB_U16;
274bf215546Sopenharmony_ci      }
275bf215546Sopenharmony_ci      else if (c->size == 32) {
276bf215546Sopenharmony_ci         if (c->normalized)
277bf215546Sopenharmony_ci            return LIMA_ATTRIB_U32N;
278bf215546Sopenharmony_ci         else
279bf215546Sopenharmony_ci            return LIMA_ATTRIB_U32;
280bf215546Sopenharmony_ci      }
281bf215546Sopenharmony_ci      break;
282bf215546Sopenharmony_ci   }
283bf215546Sopenharmony_ci
284bf215546Sopenharmony_ci   return LIMA_ATTRIB_FLOAT;
285bf215546Sopenharmony_ci}
286bf215546Sopenharmony_ci
287bf215546Sopenharmony_cistatic void
288bf215546Sopenharmony_cilima_pack_vs_cmd(struct lima_context *ctx, const struct pipe_draw_info *info,
289bf215546Sopenharmony_ci                 const struct pipe_draw_start_count_bias *draw)
290bf215546Sopenharmony_ci{
291bf215546Sopenharmony_ci   struct lima_context_constant_buffer *ccb =
292bf215546Sopenharmony_ci      ctx->const_buffer + PIPE_SHADER_VERTEX;
293bf215546Sopenharmony_ci   struct lima_vs_compiled_shader *vs = ctx->vs;
294bf215546Sopenharmony_ci   struct lima_job *job = lima_job_get(ctx);
295bf215546Sopenharmony_ci
296bf215546Sopenharmony_ci   VS_CMD_BEGIN(&job->vs_cmd_array, 24);
297bf215546Sopenharmony_ci
298bf215546Sopenharmony_ci   if (!info->index_size) {
299bf215546Sopenharmony_ci      VS_CMD_ARRAYS_SEMAPHORE_BEGIN_1();
300bf215546Sopenharmony_ci      VS_CMD_ARRAYS_SEMAPHORE_BEGIN_2();
301bf215546Sopenharmony_ci   }
302bf215546Sopenharmony_ci   int uniform_size = MIN2(vs->state.uniform_size, ccb->size);
303bf215546Sopenharmony_ci
304bf215546Sopenharmony_ci   int size = uniform_size + vs->state.constant_size + 32;
305bf215546Sopenharmony_ci   VS_CMD_UNIFORMS_ADDRESS(
306bf215546Sopenharmony_ci      lima_ctx_buff_va(ctx, lima_ctx_buff_gp_uniform),
307bf215546Sopenharmony_ci      align(size, 16));
308bf215546Sopenharmony_ci
309bf215546Sopenharmony_ci   VS_CMD_SHADER_ADDRESS(ctx->vs->bo->va, ctx->vs->state.shader_size);
310bf215546Sopenharmony_ci   VS_CMD_SHADER_INFO(ctx->vs->state.prefetch, ctx->vs->state.shader_size);
311bf215546Sopenharmony_ci
312bf215546Sopenharmony_ci   int num_outputs = ctx->vs->state.num_outputs;
313bf215546Sopenharmony_ci   int num_attributes = ctx->vertex_elements->num_elements;
314bf215546Sopenharmony_ci   VS_CMD_VARYING_ATTRIBUTE_COUNT(num_outputs, MAX2(1, num_attributes));
315bf215546Sopenharmony_ci
316bf215546Sopenharmony_ci   VS_CMD_UNKNOWN1();
317bf215546Sopenharmony_ci
318bf215546Sopenharmony_ci   VS_CMD_ATTRIBUTES_ADDRESS(
319bf215546Sopenharmony_ci      lima_ctx_buff_va(ctx, lima_ctx_buff_gp_attribute_info),
320bf215546Sopenharmony_ci      MAX2(1, num_attributes));
321bf215546Sopenharmony_ci
322bf215546Sopenharmony_ci   VS_CMD_VARYINGS_ADDRESS(
323bf215546Sopenharmony_ci      lima_ctx_buff_va(ctx, lima_ctx_buff_gp_varying_info),
324bf215546Sopenharmony_ci      num_outputs);
325bf215546Sopenharmony_ci
326bf215546Sopenharmony_ci   unsigned num = info->index_size ? (ctx->max_index - ctx->min_index + 1) : draw->count;
327bf215546Sopenharmony_ci   VS_CMD_DRAW(num, info->index_size);
328bf215546Sopenharmony_ci
329bf215546Sopenharmony_ci   VS_CMD_UNKNOWN2();
330bf215546Sopenharmony_ci
331bf215546Sopenharmony_ci   VS_CMD_ARRAYS_SEMAPHORE_END(info->index_size);
332bf215546Sopenharmony_ci
333bf215546Sopenharmony_ci   VS_CMD_END();
334bf215546Sopenharmony_ci}
335bf215546Sopenharmony_ci
336bf215546Sopenharmony_cistatic void
337bf215546Sopenharmony_cilima_pack_plbu_cmd(struct lima_context *ctx, const struct pipe_draw_info *info,
338bf215546Sopenharmony_ci                   const struct pipe_draw_start_count_bias *draw)
339bf215546Sopenharmony_ci{
340bf215546Sopenharmony_ci   struct lima_vs_compiled_shader *vs = ctx->vs;
341bf215546Sopenharmony_ci   struct pipe_scissor_state *cscissor = &ctx->clipped_scissor;
342bf215546Sopenharmony_ci   struct lima_job *job = lima_job_get(ctx);
343bf215546Sopenharmony_ci   PLBU_CMD_BEGIN(&job->plbu_cmd_array, 32);
344bf215546Sopenharmony_ci
345bf215546Sopenharmony_ci   PLBU_CMD_VIEWPORT_LEFT(fui(ctx->ext_viewport.left));
346bf215546Sopenharmony_ci   PLBU_CMD_VIEWPORT_RIGHT(fui(ctx->ext_viewport.right));
347bf215546Sopenharmony_ci   PLBU_CMD_VIEWPORT_BOTTOM(fui(ctx->ext_viewport.bottom));
348bf215546Sopenharmony_ci   PLBU_CMD_VIEWPORT_TOP(fui(ctx->ext_viewport.top));
349bf215546Sopenharmony_ci
350bf215546Sopenharmony_ci   if (!info->index_size)
351bf215546Sopenharmony_ci      PLBU_CMD_ARRAYS_SEMAPHORE_BEGIN();
352bf215546Sopenharmony_ci
353bf215546Sopenharmony_ci   int cf = ctx->rasterizer->base.cull_face;
354bf215546Sopenharmony_ci   int ccw = ctx->rasterizer->base.front_ccw;
355bf215546Sopenharmony_ci   uint32_t cull = 0;
356bf215546Sopenharmony_ci   bool force_point_size = false;
357bf215546Sopenharmony_ci
358bf215546Sopenharmony_ci   if (cf != PIPE_FACE_NONE) {
359bf215546Sopenharmony_ci      if (cf & PIPE_FACE_FRONT)
360bf215546Sopenharmony_ci         cull |= ccw ? 0x00040000 : 0x00020000;
361bf215546Sopenharmony_ci      if (cf & PIPE_FACE_BACK)
362bf215546Sopenharmony_ci         cull |= ccw ? 0x00020000 : 0x00040000;
363bf215546Sopenharmony_ci   }
364bf215546Sopenharmony_ci
365bf215546Sopenharmony_ci   /* Specify point size with PLBU command if shader doesn't write */
366bf215546Sopenharmony_ci   if (info->mode == PIPE_PRIM_POINTS && ctx->vs->state.point_size_idx == -1)
367bf215546Sopenharmony_ci      force_point_size = true;
368bf215546Sopenharmony_ci
369bf215546Sopenharmony_ci   /* Specify line width with PLBU command for lines */
370bf215546Sopenharmony_ci   if (info->mode > PIPE_PRIM_POINTS && info->mode < PIPE_PRIM_TRIANGLES)
371bf215546Sopenharmony_ci      force_point_size = true;
372bf215546Sopenharmony_ci
373bf215546Sopenharmony_ci   PLBU_CMD_PRIMITIVE_SETUP(force_point_size, cull, info->index_size);
374bf215546Sopenharmony_ci
375bf215546Sopenharmony_ci   PLBU_CMD_RSW_VERTEX_ARRAY(
376bf215546Sopenharmony_ci      lima_ctx_buff_va(ctx, lima_ctx_buff_pp_plb_rsw),
377bf215546Sopenharmony_ci      ctx->gp_output->va);
378bf215546Sopenharmony_ci
379bf215546Sopenharmony_ci   /* TODO
380bf215546Sopenharmony_ci    * - we should set it only for the first draw that enabled the scissor and for
381bf215546Sopenharmony_ci    *   latter draw only if scissor is dirty
382bf215546Sopenharmony_ci    */
383bf215546Sopenharmony_ci
384bf215546Sopenharmony_ci   assert(cscissor->minx < cscissor->maxx && cscissor->miny < cscissor->maxy);
385bf215546Sopenharmony_ci   PLBU_CMD_SCISSORS(cscissor->minx, cscissor->maxx, cscissor->miny, cscissor->maxy);
386bf215546Sopenharmony_ci
387bf215546Sopenharmony_ci   lima_damage_rect_union(&job->damage_rect, cscissor->minx, cscissor->maxx,
388bf215546Sopenharmony_ci                          cscissor->miny, cscissor->maxy);
389bf215546Sopenharmony_ci
390bf215546Sopenharmony_ci   PLBU_CMD_UNKNOWN1();
391bf215546Sopenharmony_ci
392bf215546Sopenharmony_ci   PLBU_CMD_DEPTH_RANGE_NEAR(fui(ctx->viewport.near));
393bf215546Sopenharmony_ci   PLBU_CMD_DEPTH_RANGE_FAR(fui(ctx->viewport.far));
394bf215546Sopenharmony_ci
395bf215546Sopenharmony_ci   if ((info->mode == PIPE_PRIM_POINTS && ctx->vs->state.point_size_idx == -1) ||
396bf215546Sopenharmony_ci       ((info->mode >= PIPE_PRIM_LINES) && (info->mode < PIPE_PRIM_TRIANGLES)))
397bf215546Sopenharmony_ci   {
398bf215546Sopenharmony_ci      uint32_t v = info->mode == PIPE_PRIM_POINTS ?
399bf215546Sopenharmony_ci         fui(ctx->rasterizer->base.point_size) : fui(ctx->rasterizer->base.line_width);
400bf215546Sopenharmony_ci      PLBU_CMD_LOW_PRIM_SIZE(v);
401bf215546Sopenharmony_ci   }
402bf215546Sopenharmony_ci
403bf215546Sopenharmony_ci   if (info->index_size) {
404bf215546Sopenharmony_ci      PLBU_CMD_INDEXED_DEST(ctx->gp_output->va);
405bf215546Sopenharmony_ci      if (vs->state.point_size_idx != -1)
406bf215546Sopenharmony_ci         PLBU_CMD_INDEXED_PT_SIZE(ctx->gp_output->va + ctx->gp_output_point_size_offt);
407bf215546Sopenharmony_ci
408bf215546Sopenharmony_ci      PLBU_CMD_INDICES(ctx->index_res->bo->va + draw->start * info->index_size + ctx->index_offset);
409bf215546Sopenharmony_ci   }
410bf215546Sopenharmony_ci   else {
411bf215546Sopenharmony_ci      /* can this make the attribute info static? */
412bf215546Sopenharmony_ci      PLBU_CMD_DRAW_ARRAYS(info->mode, draw->start, draw->count);
413bf215546Sopenharmony_ci   }
414bf215546Sopenharmony_ci
415bf215546Sopenharmony_ci   PLBU_CMD_ARRAYS_SEMAPHORE_END();
416bf215546Sopenharmony_ci
417bf215546Sopenharmony_ci   if (info->index_size)
418bf215546Sopenharmony_ci      PLBU_CMD_DRAW_ELEMENTS(info->mode, ctx->min_index, draw->count);
419bf215546Sopenharmony_ci
420bf215546Sopenharmony_ci   PLBU_CMD_END();
421bf215546Sopenharmony_ci}
422bf215546Sopenharmony_ci
423bf215546Sopenharmony_cistatic int
424bf215546Sopenharmony_cilima_blend_func(enum pipe_blend_func pipe)
425bf215546Sopenharmony_ci{
426bf215546Sopenharmony_ci   switch (pipe) {
427bf215546Sopenharmony_ci   case PIPE_BLEND_ADD:
428bf215546Sopenharmony_ci      return 2;
429bf215546Sopenharmony_ci   case PIPE_BLEND_SUBTRACT:
430bf215546Sopenharmony_ci      return 0;
431bf215546Sopenharmony_ci   case PIPE_BLEND_REVERSE_SUBTRACT:
432bf215546Sopenharmony_ci      return 1;
433bf215546Sopenharmony_ci   case PIPE_BLEND_MIN:
434bf215546Sopenharmony_ci      return 4;
435bf215546Sopenharmony_ci   case PIPE_BLEND_MAX:
436bf215546Sopenharmony_ci      return 5;
437bf215546Sopenharmony_ci   }
438bf215546Sopenharmony_ci   return -1;
439bf215546Sopenharmony_ci}
440bf215546Sopenharmony_ci
441bf215546Sopenharmony_cistatic int
442bf215546Sopenharmony_cilima_blend_factor(enum pipe_blendfactor pipe)
443bf215546Sopenharmony_ci{
444bf215546Sopenharmony_ci   /* Bits 0-2 indicate the blendfactor type,
445bf215546Sopenharmony_ci    * Bit 3 is set if blendfactor is inverted
446bf215546Sopenharmony_ci    * Bit 4 is set if blendfactor has alpha */
447bf215546Sopenharmony_ci   switch (pipe) {
448bf215546Sopenharmony_ci   case PIPE_BLENDFACTOR_SRC_COLOR:
449bf215546Sopenharmony_ci      return 0 << 4 | 0 << 3 | 0;
450bf215546Sopenharmony_ci   case PIPE_BLENDFACTOR_SRC_ALPHA:
451bf215546Sopenharmony_ci      return 1 << 4 | 0 << 3 | 0;
452bf215546Sopenharmony_ci   case PIPE_BLENDFACTOR_INV_SRC_COLOR:
453bf215546Sopenharmony_ci      return 0 << 4 | 1 << 3 | 0;
454bf215546Sopenharmony_ci   case PIPE_BLENDFACTOR_INV_SRC_ALPHA:
455bf215546Sopenharmony_ci      return 1 << 4 | 1 << 3 | 0;
456bf215546Sopenharmony_ci
457bf215546Sopenharmony_ci   case PIPE_BLENDFACTOR_DST_COLOR:
458bf215546Sopenharmony_ci      return 0 << 4 | 0 << 3 | 1;
459bf215546Sopenharmony_ci   case PIPE_BLENDFACTOR_DST_ALPHA:
460bf215546Sopenharmony_ci      return 1 << 4 | 0 << 3 | 1;
461bf215546Sopenharmony_ci   case PIPE_BLENDFACTOR_INV_DST_COLOR:
462bf215546Sopenharmony_ci      return 0 << 4 | 1 << 3 | 1;
463bf215546Sopenharmony_ci   case PIPE_BLENDFACTOR_INV_DST_ALPHA:
464bf215546Sopenharmony_ci      return 1 << 4 | 1 << 3 | 1;
465bf215546Sopenharmony_ci
466bf215546Sopenharmony_ci   case PIPE_BLENDFACTOR_CONST_COLOR:
467bf215546Sopenharmony_ci      return 0 << 4 | 0 << 3 | 2;
468bf215546Sopenharmony_ci   case PIPE_BLENDFACTOR_CONST_ALPHA:
469bf215546Sopenharmony_ci      return 1 << 4 | 0 << 3 | 2;
470bf215546Sopenharmony_ci   case PIPE_BLENDFACTOR_INV_CONST_COLOR:
471bf215546Sopenharmony_ci      return 0 << 4 | 1 << 3 | 2;
472bf215546Sopenharmony_ci   case PIPE_BLENDFACTOR_INV_CONST_ALPHA:
473bf215546Sopenharmony_ci      return 1 << 4 | 1 << 3 | 2;
474bf215546Sopenharmony_ci
475bf215546Sopenharmony_ci   case PIPE_BLENDFACTOR_ZERO:
476bf215546Sopenharmony_ci      return 0 << 4 | 0 << 3 | 3;
477bf215546Sopenharmony_ci   case PIPE_BLENDFACTOR_ONE:
478bf215546Sopenharmony_ci      return 0 << 4 | 1 << 3 | 3;
479bf215546Sopenharmony_ci
480bf215546Sopenharmony_ci   case PIPE_BLENDFACTOR_SRC_ALPHA_SATURATE:
481bf215546Sopenharmony_ci      return 0 << 4 | 0 << 3 | 4;
482bf215546Sopenharmony_ci
483bf215546Sopenharmony_ci   case PIPE_BLENDFACTOR_SRC1_COLOR:
484bf215546Sopenharmony_ci      return 0 << 4 | 0 << 3 | 5;
485bf215546Sopenharmony_ci   case PIPE_BLENDFACTOR_SRC1_ALPHA:
486bf215546Sopenharmony_ci      return 1 << 4 | 0 << 3 | 5;
487bf215546Sopenharmony_ci   case PIPE_BLENDFACTOR_INV_SRC1_COLOR:
488bf215546Sopenharmony_ci      return 0 << 4 | 1 << 3 | 5;
489bf215546Sopenharmony_ci   case PIPE_BLENDFACTOR_INV_SRC1_ALPHA:
490bf215546Sopenharmony_ci      return 1 << 4 | 1 << 3 | 5;
491bf215546Sopenharmony_ci   }
492bf215546Sopenharmony_ci   return -1;
493bf215546Sopenharmony_ci}
494bf215546Sopenharmony_ci
495bf215546Sopenharmony_cistatic int
496bf215546Sopenharmony_cilima_calculate_alpha_blend(enum pipe_blend_func rgb_func, enum pipe_blend_func alpha_func,
497bf215546Sopenharmony_ci                           enum pipe_blendfactor rgb_src_factor, enum pipe_blendfactor rgb_dst_factor,
498bf215546Sopenharmony_ci                           enum pipe_blendfactor alpha_src_factor, enum pipe_blendfactor alpha_dst_factor)
499bf215546Sopenharmony_ci{
500bf215546Sopenharmony_ci   /* PIPE_BLENDFACTOR_SRC_ALPHA_SATURATE has to be changed to PIPE_BLENDFACTOR_ONE
501bf215546Sopenharmony_ci    * if it is set for alpha_src or alpha_dst.
502bf215546Sopenharmony_ci    */
503bf215546Sopenharmony_ci   if (alpha_src_factor == PIPE_BLENDFACTOR_SRC_ALPHA_SATURATE)
504bf215546Sopenharmony_ci      alpha_src_factor = PIPE_BLENDFACTOR_ONE;
505bf215546Sopenharmony_ci
506bf215546Sopenharmony_ci   if (alpha_dst_factor == PIPE_BLENDFACTOR_SRC_ALPHA_SATURATE)
507bf215546Sopenharmony_ci      alpha_dst_factor = PIPE_BLENDFACTOR_ONE;
508bf215546Sopenharmony_ci
509bf215546Sopenharmony_ci   /* MIN and MAX ops actually do OP(As * S + Ad * D, Ad), so
510bf215546Sopenharmony_ci    * we need to set S to 1 and D to 0 to get correct result */
511bf215546Sopenharmony_ci   if (alpha_func == PIPE_BLEND_MIN ||
512bf215546Sopenharmony_ci       alpha_func == PIPE_BLEND_MAX) {
513bf215546Sopenharmony_ci      alpha_src_factor = PIPE_BLENDFACTOR_ONE;
514bf215546Sopenharmony_ci      alpha_dst_factor = PIPE_BLENDFACTOR_ZERO;
515bf215546Sopenharmony_ci   }
516bf215546Sopenharmony_ci
517bf215546Sopenharmony_ci   /* MIN and MAX ops actually do OP(Cs * S + Cd * D, Cd), so
518bf215546Sopenharmony_ci    * we need to set S to 1 and D to 0 to get correct result */
519bf215546Sopenharmony_ci   if (rgb_func == PIPE_BLEND_MIN ||
520bf215546Sopenharmony_ci       rgb_func == PIPE_BLEND_MAX) {
521bf215546Sopenharmony_ci      rgb_src_factor = PIPE_BLENDFACTOR_ONE;
522bf215546Sopenharmony_ci      rgb_dst_factor = PIPE_BLENDFACTOR_ZERO;
523bf215546Sopenharmony_ci   }
524bf215546Sopenharmony_ci
525bf215546Sopenharmony_ci   return lima_blend_func(rgb_func) |
526bf215546Sopenharmony_ci      (lima_blend_func(alpha_func) << 3) |
527bf215546Sopenharmony_ci      (lima_blend_factor(rgb_src_factor) << 6) |
528bf215546Sopenharmony_ci      (lima_blend_factor(rgb_dst_factor) << 11) |
529bf215546Sopenharmony_ci      /* alpha_src and alpha_dst are 4 bit, so need to mask 5th bit */
530bf215546Sopenharmony_ci      ((lima_blend_factor(alpha_src_factor) & 0xf) << 16) |
531bf215546Sopenharmony_ci      ((lima_blend_factor(alpha_dst_factor) & 0xf) << 20) |
532bf215546Sopenharmony_ci      0x0C000000; /* need to check if this is GLESv1 glAlphaFunc */
533bf215546Sopenharmony_ci}
534bf215546Sopenharmony_ci
535bf215546Sopenharmony_cistatic int
536bf215546Sopenharmony_cilima_stencil_op(enum pipe_stencil_op pipe)
537bf215546Sopenharmony_ci{
538bf215546Sopenharmony_ci   switch (pipe) {
539bf215546Sopenharmony_ci   case PIPE_STENCIL_OP_KEEP:
540bf215546Sopenharmony_ci      return 0;
541bf215546Sopenharmony_ci   case PIPE_STENCIL_OP_ZERO:
542bf215546Sopenharmony_ci      return 2;
543bf215546Sopenharmony_ci   case PIPE_STENCIL_OP_REPLACE:
544bf215546Sopenharmony_ci      return 1;
545bf215546Sopenharmony_ci   case PIPE_STENCIL_OP_INCR:
546bf215546Sopenharmony_ci      return 6;
547bf215546Sopenharmony_ci   case PIPE_STENCIL_OP_DECR:
548bf215546Sopenharmony_ci      return 7;
549bf215546Sopenharmony_ci   case PIPE_STENCIL_OP_INCR_WRAP:
550bf215546Sopenharmony_ci      return 4;
551bf215546Sopenharmony_ci   case PIPE_STENCIL_OP_DECR_WRAP:
552bf215546Sopenharmony_ci      return 5;
553bf215546Sopenharmony_ci   case PIPE_STENCIL_OP_INVERT:
554bf215546Sopenharmony_ci      return 3;
555bf215546Sopenharmony_ci   }
556bf215546Sopenharmony_ci   return -1;
557bf215546Sopenharmony_ci}
558bf215546Sopenharmony_ci
559bf215546Sopenharmony_cistatic unsigned
560bf215546Sopenharmony_cilima_calculate_depth_test(struct pipe_depth_stencil_alpha_state *depth,
561bf215546Sopenharmony_ci                          struct pipe_rasterizer_state *rst)
562bf215546Sopenharmony_ci{
563bf215546Sopenharmony_ci   int offset_scale = 0, offset_units = 0;
564bf215546Sopenharmony_ci   enum pipe_compare_func func = (depth->depth_enabled ? depth->depth_func : PIPE_FUNC_ALWAYS);
565bf215546Sopenharmony_ci
566bf215546Sopenharmony_ci   offset_scale = CLAMP(rst->offset_scale * 4, -128, 127);
567bf215546Sopenharmony_ci   if (offset_scale < 0)
568bf215546Sopenharmony_ci      offset_scale += 0x100;
569bf215546Sopenharmony_ci
570bf215546Sopenharmony_ci   offset_units = CLAMP(rst->offset_units * 2, -128, 127);
571bf215546Sopenharmony_ci   if (offset_units < 0)
572bf215546Sopenharmony_ci      offset_units += 0x100;
573bf215546Sopenharmony_ci
574bf215546Sopenharmony_ci   return (depth->depth_enabled && depth->depth_writemask) |
575bf215546Sopenharmony_ci      ((int)func << 1) |
576bf215546Sopenharmony_ci      (offset_scale << 16) |
577bf215546Sopenharmony_ci      (offset_units << 24);
578bf215546Sopenharmony_ci}
579bf215546Sopenharmony_ci
580bf215546Sopenharmony_cistatic void
581bf215546Sopenharmony_cilima_pack_render_state(struct lima_context *ctx, const struct pipe_draw_info *info)
582bf215546Sopenharmony_ci{
583bf215546Sopenharmony_ci   struct lima_fs_compiled_shader *fs = ctx->fs;
584bf215546Sopenharmony_ci   struct lima_render_state *render =
585bf215546Sopenharmony_ci      lima_ctx_buff_alloc(ctx, lima_ctx_buff_pp_plb_rsw,
586bf215546Sopenharmony_ci                          sizeof(*render));
587bf215546Sopenharmony_ci   bool early_z = true;
588bf215546Sopenharmony_ci   bool pixel_kill = true;
589bf215546Sopenharmony_ci
590bf215546Sopenharmony_ci   /* do hw support RGBA independ blend?
591bf215546Sopenharmony_ci    * PIPE_CAP_INDEP_BLEND_ENABLE
592bf215546Sopenharmony_ci    *
593bf215546Sopenharmony_ci    * how to handle the no cbuf only zbuf case?
594bf215546Sopenharmony_ci    */
595bf215546Sopenharmony_ci   struct pipe_rt_blend_state *rt = ctx->blend->base.rt;
596bf215546Sopenharmony_ci   render->blend_color_bg = float_to_ubyte(ctx->blend_color.color[2]) |
597bf215546Sopenharmony_ci      (float_to_ubyte(ctx->blend_color.color[1]) << 16);
598bf215546Sopenharmony_ci   render->blend_color_ra = float_to_ubyte(ctx->blend_color.color[0]) |
599bf215546Sopenharmony_ci      (float_to_ubyte(ctx->blend_color.color[3]) << 16);
600bf215546Sopenharmony_ci
601bf215546Sopenharmony_ci   if (rt->blend_enable) {
602bf215546Sopenharmony_ci      render->alpha_blend = lima_calculate_alpha_blend(rt->rgb_func, rt->alpha_func,
603bf215546Sopenharmony_ci         rt->rgb_src_factor, rt->rgb_dst_factor,
604bf215546Sopenharmony_ci         rt->alpha_src_factor, rt->alpha_dst_factor);
605bf215546Sopenharmony_ci   }
606bf215546Sopenharmony_ci   else {
607bf215546Sopenharmony_ci      /*
608bf215546Sopenharmony_ci       * Special handling for blending disabled.
609bf215546Sopenharmony_ci       * Binary driver is generating the same alpha_value,
610bf215546Sopenharmony_ci       * as when we would just enable blending, without changing/setting any blend equation/params.
611bf215546Sopenharmony_ci       * Normaly in this case mesa would set all rt fields (func/factor) to zero.
612bf215546Sopenharmony_ci       */
613bf215546Sopenharmony_ci      render->alpha_blend = lima_calculate_alpha_blend(PIPE_BLEND_ADD, PIPE_BLEND_ADD,
614bf215546Sopenharmony_ci         PIPE_BLENDFACTOR_ONE, PIPE_BLENDFACTOR_ZERO,
615bf215546Sopenharmony_ci         PIPE_BLENDFACTOR_ONE, PIPE_BLENDFACTOR_ZERO);
616bf215546Sopenharmony_ci   }
617bf215546Sopenharmony_ci
618bf215546Sopenharmony_ci   render->alpha_blend |= (rt->colormask & PIPE_MASK_RGBA) << 28;
619bf215546Sopenharmony_ci
620bf215546Sopenharmony_ci   struct pipe_rasterizer_state *rst = &ctx->rasterizer->base;
621bf215546Sopenharmony_ci   render->depth_test = lima_calculate_depth_test(&ctx->zsa->base, rst);
622bf215546Sopenharmony_ci
623bf215546Sopenharmony_ci   if (!rst->depth_clip_near || ctx->viewport.near == 0.0f)
624bf215546Sopenharmony_ci      render->depth_test |= 0x10; /* don't clip depth near */
625bf215546Sopenharmony_ci   if (!rst->depth_clip_far || ctx->viewport.far == 1.0f)
626bf215546Sopenharmony_ci      render->depth_test |= 0x20; /* don't clip depth far */
627bf215546Sopenharmony_ci
628bf215546Sopenharmony_ci   if (fs->state.frag_depth_reg != -1) {
629bf215546Sopenharmony_ci      render->depth_test |= (fs->state.frag_depth_reg << 6);
630bf215546Sopenharmony_ci      /* Shader writes depth */
631bf215546Sopenharmony_ci      render->depth_test |= 0x801;
632bf215546Sopenharmony_ci   }
633bf215546Sopenharmony_ci
634bf215546Sopenharmony_ci   ushort far, near;
635bf215546Sopenharmony_ci
636bf215546Sopenharmony_ci   near = float_to_ushort(ctx->viewport.near);
637bf215546Sopenharmony_ci   far = float_to_ushort(ctx->viewport.far);
638bf215546Sopenharmony_ci
639bf215546Sopenharmony_ci   /* overlap with plbu? any place can remove one? */
640bf215546Sopenharmony_ci   render->depth_range = near | (far << 16);
641bf215546Sopenharmony_ci
642bf215546Sopenharmony_ci   struct pipe_stencil_state *stencil = ctx->zsa->base.stencil;
643bf215546Sopenharmony_ci   struct pipe_stencil_ref *ref = &ctx->stencil_ref;
644bf215546Sopenharmony_ci
645bf215546Sopenharmony_ci   if (stencil[0].enabled) { /* stencil is enabled */
646bf215546Sopenharmony_ci      render->stencil_front = stencil[0].func |
647bf215546Sopenharmony_ci         (lima_stencil_op(stencil[0].fail_op) << 3) |
648bf215546Sopenharmony_ci         (lima_stencil_op(stencil[0].zfail_op) << 6) |
649bf215546Sopenharmony_ci         (lima_stencil_op(stencil[0].zpass_op) << 9) |
650bf215546Sopenharmony_ci         (ref->ref_value[0] << 16) |
651bf215546Sopenharmony_ci         (stencil[0].valuemask << 24);
652bf215546Sopenharmony_ci      render->stencil_back = render->stencil_front;
653bf215546Sopenharmony_ci      render->stencil_test = (stencil[0].writemask & 0xff) | (stencil[0].writemask & 0xff) << 8;
654bf215546Sopenharmony_ci      if (stencil[1].enabled) { /* two-side is enabled */
655bf215546Sopenharmony_ci         render->stencil_back = stencil[1].func |
656bf215546Sopenharmony_ci            (lima_stencil_op(stencil[1].fail_op) << 3) |
657bf215546Sopenharmony_ci            (lima_stencil_op(stencil[1].zfail_op) << 6) |
658bf215546Sopenharmony_ci            (lima_stencil_op(stencil[1].zpass_op) << 9) |
659bf215546Sopenharmony_ci            (ref->ref_value[1] << 16) |
660bf215546Sopenharmony_ci            (stencil[1].valuemask << 24);
661bf215546Sopenharmony_ci         render->stencil_test = (stencil[0].writemask & 0xff) | (stencil[1].writemask & 0xff) << 8;
662bf215546Sopenharmony_ci      }
663bf215546Sopenharmony_ci      /* TODO: Find out, what (render->stecil_test & 0xff000000) is */
664bf215546Sopenharmony_ci   }
665bf215546Sopenharmony_ci   else {
666bf215546Sopenharmony_ci      /* Default values, when stencil is disabled:
667bf215546Sopenharmony_ci       * stencil[0|1].valuemask = 0xff
668bf215546Sopenharmony_ci       * stencil[0|1].func = PIPE_FUNC_ALWAYS
669bf215546Sopenharmony_ci       * stencil[0|1].writemask = 0xff
670bf215546Sopenharmony_ci       */
671bf215546Sopenharmony_ci      render->stencil_front = 0xff000007;
672bf215546Sopenharmony_ci      render->stencil_back = 0xff000007;
673bf215546Sopenharmony_ci      render->stencil_test = 0x0000ffff;
674bf215546Sopenharmony_ci   }
675bf215546Sopenharmony_ci
676bf215546Sopenharmony_ci   /* need more investigation */
677bf215546Sopenharmony_ci   if (info->mode == PIPE_PRIM_POINTS)
678bf215546Sopenharmony_ci      render->multi_sample = 0x00000000;
679bf215546Sopenharmony_ci   else if (info->mode < PIPE_PRIM_TRIANGLES)
680bf215546Sopenharmony_ci      render->multi_sample = 0x00000400;
681bf215546Sopenharmony_ci   else
682bf215546Sopenharmony_ci      render->multi_sample = 0x00000800;
683bf215546Sopenharmony_ci   if (ctx->framebuffer.base.samples)
684bf215546Sopenharmony_ci      render->multi_sample |= 0x68;
685bf215546Sopenharmony_ci   if (ctx->blend->base.alpha_to_coverage)
686bf215546Sopenharmony_ci      render->multi_sample |= (1 << 7);
687bf215546Sopenharmony_ci   if (ctx->blend->base.alpha_to_one)
688bf215546Sopenharmony_ci      render->multi_sample |= (1 << 8);
689bf215546Sopenharmony_ci   render->multi_sample |= (ctx->sample_mask << 12);
690bf215546Sopenharmony_ci
691bf215546Sopenharmony_ci   /* Set gl_FragColor register, need to specify it 4 times */
692bf215546Sopenharmony_ci   render->multi_sample |= (fs->state.frag_color0_reg << 28) |
693bf215546Sopenharmony_ci                           (fs->state.frag_color0_reg << 24) |
694bf215546Sopenharmony_ci                           (fs->state.frag_color0_reg << 20) |
695bf215546Sopenharmony_ci                           (fs->state.frag_color0_reg << 16);
696bf215546Sopenharmony_ci
697bf215546Sopenharmony_ci   /* alpha test */
698bf215546Sopenharmony_ci   if (ctx->zsa->base.alpha_enabled) {
699bf215546Sopenharmony_ci      render->multi_sample |= ctx->zsa->base.alpha_func;
700bf215546Sopenharmony_ci      render->stencil_test |= float_to_ubyte(ctx->zsa->base.alpha_ref_value) << 16;
701bf215546Sopenharmony_ci   } else {
702bf215546Sopenharmony_ci      /* func = PIPE_FUNC_ALWAYS */
703bf215546Sopenharmony_ci      render->multi_sample |= 0x7;
704bf215546Sopenharmony_ci   }
705bf215546Sopenharmony_ci
706bf215546Sopenharmony_ci   render->shader_address =
707bf215546Sopenharmony_ci      ctx->fs->bo->va | (((uint32_t *)ctx->fs->bo->map)[0] & 0x1F);
708bf215546Sopenharmony_ci
709bf215546Sopenharmony_ci   /* seems not needed */
710bf215546Sopenharmony_ci   render->uniforms_address = 0x00000000;
711bf215546Sopenharmony_ci
712bf215546Sopenharmony_ci   render->textures_address = 0x00000000;
713bf215546Sopenharmony_ci
714bf215546Sopenharmony_ci   render->aux0 = (ctx->vs->state.varying_stride >> 3);
715bf215546Sopenharmony_ci   render->aux1 = 0x00000000;
716bf215546Sopenharmony_ci   if (ctx->rasterizer->base.front_ccw)
717bf215546Sopenharmony_ci      render->aux1 = 0x00001000;
718bf215546Sopenharmony_ci
719bf215546Sopenharmony_ci   if (ctx->blend->base.dither)
720bf215546Sopenharmony_ci      render->aux1 |= 0x00002000;
721bf215546Sopenharmony_ci
722bf215546Sopenharmony_ci   if (fs->state.uses_discard ||
723bf215546Sopenharmony_ci       ctx->zsa->base.alpha_enabled ||
724bf215546Sopenharmony_ci       fs->state.frag_depth_reg != -1 ||
725bf215546Sopenharmony_ci       ctx->blend->base.alpha_to_coverage) {
726bf215546Sopenharmony_ci      early_z = false;
727bf215546Sopenharmony_ci      pixel_kill = false;
728bf215546Sopenharmony_ci   }
729bf215546Sopenharmony_ci
730bf215546Sopenharmony_ci   if (rt->blend_enable)
731bf215546Sopenharmony_ci      pixel_kill = false;
732bf215546Sopenharmony_ci
733bf215546Sopenharmony_ci   if ((rt->colormask & PIPE_MASK_RGBA) != PIPE_MASK_RGBA)
734bf215546Sopenharmony_ci      pixel_kill = false;
735bf215546Sopenharmony_ci
736bf215546Sopenharmony_ci   if (early_z)
737bf215546Sopenharmony_ci      render->aux0 |= 0x300;
738bf215546Sopenharmony_ci
739bf215546Sopenharmony_ci   if (pixel_kill)
740bf215546Sopenharmony_ci      render->aux0 |= 0x1000;
741bf215546Sopenharmony_ci
742bf215546Sopenharmony_ci   if (ctx->tex_stateobj.num_samplers) {
743bf215546Sopenharmony_ci      render->textures_address =
744bf215546Sopenharmony_ci         lima_ctx_buff_va(ctx, lima_ctx_buff_pp_tex_desc);
745bf215546Sopenharmony_ci      render->aux0 |= ctx->tex_stateobj.num_samplers << 14;
746bf215546Sopenharmony_ci      render->aux0 |= 0x20;
747bf215546Sopenharmony_ci   }
748bf215546Sopenharmony_ci
749bf215546Sopenharmony_ci   if (ctx->const_buffer[PIPE_SHADER_FRAGMENT].buffer) {
750bf215546Sopenharmony_ci      render->uniforms_address =
751bf215546Sopenharmony_ci         lima_ctx_buff_va(ctx, lima_ctx_buff_pp_uniform_array);
752bf215546Sopenharmony_ci      uint32_t size = ctx->buffer_state[lima_ctx_buff_pp_uniform].size;
753bf215546Sopenharmony_ci      uint32_t bits = 0;
754bf215546Sopenharmony_ci      if (size >= 8) {
755bf215546Sopenharmony_ci         bits = util_last_bit(size >> 3) - 1;
756bf215546Sopenharmony_ci         bits += size & u_bit_consecutive(0, bits + 3) ? 1 : 0;
757bf215546Sopenharmony_ci      }
758bf215546Sopenharmony_ci      render->uniforms_address |= bits > 0xf ? 0xf : bits;
759bf215546Sopenharmony_ci
760bf215546Sopenharmony_ci      render->aux0 |= 0x80;
761bf215546Sopenharmony_ci      render->aux1 |= 0x10000;
762bf215546Sopenharmony_ci   }
763bf215546Sopenharmony_ci
764bf215546Sopenharmony_ci   /* Set secondary output color */
765bf215546Sopenharmony_ci   if (fs->state.frag_color1_reg != -1)
766bf215546Sopenharmony_ci      render->aux0 |= (fs->state.frag_color1_reg << 28);
767bf215546Sopenharmony_ci
768bf215546Sopenharmony_ci   if (ctx->vs->state.num_varyings) {
769bf215546Sopenharmony_ci      render->varying_types = 0x00000000;
770bf215546Sopenharmony_ci      render->varyings_address = ctx->gp_output->va +
771bf215546Sopenharmony_ci                                 ctx->gp_output_varyings_offt;
772bf215546Sopenharmony_ci      for (int i = 0, index = 0; i < ctx->vs->state.num_outputs; i++) {
773bf215546Sopenharmony_ci         int val;
774bf215546Sopenharmony_ci
775bf215546Sopenharmony_ci         if (i == ctx->vs->state.gl_pos_idx ||
776bf215546Sopenharmony_ci             i == ctx->vs->state.point_size_idx)
777bf215546Sopenharmony_ci            continue;
778bf215546Sopenharmony_ci
779bf215546Sopenharmony_ci         struct lima_varying_info *v = ctx->vs->state.varying + i;
780bf215546Sopenharmony_ci         if (v->component_size == 4)
781bf215546Sopenharmony_ci            val = v->components > 2 ? 0 : 1;
782bf215546Sopenharmony_ci         else
783bf215546Sopenharmony_ci            val = v->components > 2 ? 2 : 3;
784bf215546Sopenharmony_ci
785bf215546Sopenharmony_ci         if (index < 10)
786bf215546Sopenharmony_ci            render->varying_types |= val << (3 * index);
787bf215546Sopenharmony_ci         else if (index == 10) {
788bf215546Sopenharmony_ci            render->varying_types |= val << 30;
789bf215546Sopenharmony_ci            render->varyings_address |= val >> 2;
790bf215546Sopenharmony_ci         }
791bf215546Sopenharmony_ci         else if (index == 11)
792bf215546Sopenharmony_ci            render->varyings_address |= val << 1;
793bf215546Sopenharmony_ci
794bf215546Sopenharmony_ci         index++;
795bf215546Sopenharmony_ci      }
796bf215546Sopenharmony_ci   }
797bf215546Sopenharmony_ci   else {
798bf215546Sopenharmony_ci      render->varying_types = 0x00000000;
799bf215546Sopenharmony_ci      render->varyings_address = 0x00000000;
800bf215546Sopenharmony_ci   }
801bf215546Sopenharmony_ci
802bf215546Sopenharmony_ci   struct lima_job *job = lima_job_get(ctx);
803bf215546Sopenharmony_ci
804bf215546Sopenharmony_ci   lima_dump_command_stream_print(
805bf215546Sopenharmony_ci      job->dump, render, sizeof(*render),
806bf215546Sopenharmony_ci      false, "add render state at va %x\n",
807bf215546Sopenharmony_ci      lima_ctx_buff_va(ctx, lima_ctx_buff_pp_plb_rsw));
808bf215546Sopenharmony_ci
809bf215546Sopenharmony_ci   lima_dump_rsw_command_stream_print(
810bf215546Sopenharmony_ci      job->dump, render, sizeof(*render),
811bf215546Sopenharmony_ci      lima_ctx_buff_va(ctx, lima_ctx_buff_pp_plb_rsw));
812bf215546Sopenharmony_ci}
813bf215546Sopenharmony_ci
814bf215546Sopenharmony_cistatic void
815bf215546Sopenharmony_cilima_update_gp_attribute_info(struct lima_context *ctx, const struct pipe_draw_info *info,
816bf215546Sopenharmony_ci                              const struct pipe_draw_start_count_bias *draw)
817bf215546Sopenharmony_ci{
818bf215546Sopenharmony_ci   struct lima_job *job = lima_job_get(ctx);
819bf215546Sopenharmony_ci   struct lima_vertex_element_state *ve = ctx->vertex_elements;
820bf215546Sopenharmony_ci   struct lima_context_vertex_buffer *vb = &ctx->vertex_buffers;
821bf215546Sopenharmony_ci
822bf215546Sopenharmony_ci   uint32_t *attribute =
823bf215546Sopenharmony_ci      lima_ctx_buff_alloc(ctx, lima_ctx_buff_gp_attribute_info,
824bf215546Sopenharmony_ci                          MAX2(1, ve->num_elements) * 8);
825bf215546Sopenharmony_ci
826bf215546Sopenharmony_ci   int n = 0;
827bf215546Sopenharmony_ci   for (int i = 0; i < ve->num_elements; i++) {
828bf215546Sopenharmony_ci      struct pipe_vertex_element *pve = ve->pipe + i;
829bf215546Sopenharmony_ci
830bf215546Sopenharmony_ci      assert(pve->vertex_buffer_index < vb->count);
831bf215546Sopenharmony_ci      assert(vb->enabled_mask & (1 << pve->vertex_buffer_index));
832bf215546Sopenharmony_ci
833bf215546Sopenharmony_ci      struct pipe_vertex_buffer *pvb = vb->vb + pve->vertex_buffer_index;
834bf215546Sopenharmony_ci      struct lima_resource *res = lima_resource(pvb->buffer.resource);
835bf215546Sopenharmony_ci
836bf215546Sopenharmony_ci      lima_job_add_bo(job, LIMA_PIPE_GP, res->bo, LIMA_SUBMIT_BO_READ);
837bf215546Sopenharmony_ci
838bf215546Sopenharmony_ci      unsigned start = info->index_size ? (ctx->min_index + draw->index_bias) : draw->start;
839bf215546Sopenharmony_ci      attribute[n++] = res->bo->va + pvb->buffer_offset + pve->src_offset
840bf215546Sopenharmony_ci         + start * pvb->stride;
841bf215546Sopenharmony_ci      attribute[n++] = (pvb->stride << 11) |
842bf215546Sopenharmony_ci         (lima_pipe_format_to_attrib_type(pve->src_format) << 2) |
843bf215546Sopenharmony_ci         (util_format_get_nr_components(pve->src_format) - 1);
844bf215546Sopenharmony_ci   }
845bf215546Sopenharmony_ci
846bf215546Sopenharmony_ci   lima_dump_command_stream_print(
847bf215546Sopenharmony_ci      job->dump, attribute, n * 4, false, "update attribute info at va %x\n",
848bf215546Sopenharmony_ci      lima_ctx_buff_va(ctx, lima_ctx_buff_gp_attribute_info));
849bf215546Sopenharmony_ci}
850bf215546Sopenharmony_ci
851bf215546Sopenharmony_cistatic void
852bf215546Sopenharmony_cilima_update_gp_uniform(struct lima_context *ctx)
853bf215546Sopenharmony_ci{
854bf215546Sopenharmony_ci   struct lima_context_constant_buffer *ccb =
855bf215546Sopenharmony_ci      ctx->const_buffer + PIPE_SHADER_VERTEX;
856bf215546Sopenharmony_ci   struct lima_vs_compiled_shader *vs = ctx->vs;
857bf215546Sopenharmony_ci   int uniform_size = MIN2(vs->state.uniform_size, ccb->size);
858bf215546Sopenharmony_ci
859bf215546Sopenharmony_ci   int size = uniform_size + vs->state.constant_size + 32;
860bf215546Sopenharmony_ci   void *vs_const_buff =
861bf215546Sopenharmony_ci      lima_ctx_buff_alloc(ctx, lima_ctx_buff_gp_uniform, size);
862bf215546Sopenharmony_ci
863bf215546Sopenharmony_ci   if (ccb->buffer)
864bf215546Sopenharmony_ci      memcpy(vs_const_buff, ccb->buffer, uniform_size);
865bf215546Sopenharmony_ci
866bf215546Sopenharmony_ci   memcpy(vs_const_buff + uniform_size,
867bf215546Sopenharmony_ci          ctx->viewport.transform.scale,
868bf215546Sopenharmony_ci          sizeof(ctx->viewport.transform.scale));
869bf215546Sopenharmony_ci   memcpy(vs_const_buff + uniform_size + 16,
870bf215546Sopenharmony_ci          ctx->viewport.transform.translate,
871bf215546Sopenharmony_ci          sizeof(ctx->viewport.transform.translate));
872bf215546Sopenharmony_ci
873bf215546Sopenharmony_ci   if (vs->constant)
874bf215546Sopenharmony_ci      memcpy(vs_const_buff + uniform_size + 32,
875bf215546Sopenharmony_ci             vs->constant, vs->state.constant_size);
876bf215546Sopenharmony_ci
877bf215546Sopenharmony_ci   struct lima_job *job = lima_job_get(ctx);
878bf215546Sopenharmony_ci
879bf215546Sopenharmony_ci   if (lima_debug & LIMA_DEBUG_GP) {
880bf215546Sopenharmony_ci      float *vs_const_buff_f = vs_const_buff;
881bf215546Sopenharmony_ci      printf("gp uniforms:\n");
882bf215546Sopenharmony_ci      for (int i = 0; i < (size / sizeof(float)); i++) {
883bf215546Sopenharmony_ci         if ((i % 4) == 0)
884bf215546Sopenharmony_ci            printf("%4d:", i / 4);
885bf215546Sopenharmony_ci         printf(" %8.4f", vs_const_buff_f[i]);
886bf215546Sopenharmony_ci         if ((i % 4) == 3)
887bf215546Sopenharmony_ci            printf("\n");
888bf215546Sopenharmony_ci      }
889bf215546Sopenharmony_ci      printf("\n");
890bf215546Sopenharmony_ci   }
891bf215546Sopenharmony_ci
892bf215546Sopenharmony_ci   lima_dump_command_stream_print(
893bf215546Sopenharmony_ci      job->dump, vs_const_buff, size, true,
894bf215546Sopenharmony_ci      "update gp uniform at va %x\n",
895bf215546Sopenharmony_ci      lima_ctx_buff_va(ctx, lima_ctx_buff_gp_uniform));
896bf215546Sopenharmony_ci}
897bf215546Sopenharmony_ci
898bf215546Sopenharmony_cistatic void
899bf215546Sopenharmony_cilima_update_pp_uniform(struct lima_context *ctx)
900bf215546Sopenharmony_ci{
901bf215546Sopenharmony_ci   const float *const_buff = ctx->const_buffer[PIPE_SHADER_FRAGMENT].buffer;
902bf215546Sopenharmony_ci   size_t const_buff_size = ctx->const_buffer[PIPE_SHADER_FRAGMENT].size / sizeof(float);
903bf215546Sopenharmony_ci
904bf215546Sopenharmony_ci   if (!const_buff)
905bf215546Sopenharmony_ci      return;
906bf215546Sopenharmony_ci
907bf215546Sopenharmony_ci   uint16_t *fp16_const_buff =
908bf215546Sopenharmony_ci      lima_ctx_buff_alloc(ctx, lima_ctx_buff_pp_uniform,
909bf215546Sopenharmony_ci                          const_buff_size * sizeof(uint16_t));
910bf215546Sopenharmony_ci
911bf215546Sopenharmony_ci   uint32_t *array =
912bf215546Sopenharmony_ci      lima_ctx_buff_alloc(ctx, lima_ctx_buff_pp_uniform_array, 4);
913bf215546Sopenharmony_ci
914bf215546Sopenharmony_ci   for (int i = 0; i < const_buff_size; i++)
915bf215546Sopenharmony_ci       fp16_const_buff[i] = _mesa_float_to_half(const_buff[i]);
916bf215546Sopenharmony_ci
917bf215546Sopenharmony_ci   *array = lima_ctx_buff_va(ctx, lima_ctx_buff_pp_uniform);
918bf215546Sopenharmony_ci
919bf215546Sopenharmony_ci   struct lima_job *job = lima_job_get(ctx);
920bf215546Sopenharmony_ci
921bf215546Sopenharmony_ci   lima_dump_command_stream_print(
922bf215546Sopenharmony_ci      job->dump, fp16_const_buff, const_buff_size * 2,
923bf215546Sopenharmony_ci      false, "add pp uniform data at va %x\n",
924bf215546Sopenharmony_ci      lima_ctx_buff_va(ctx, lima_ctx_buff_pp_uniform));
925bf215546Sopenharmony_ci   lima_dump_command_stream_print(
926bf215546Sopenharmony_ci      job->dump, array, 4, false, "add pp uniform info at va %x\n",
927bf215546Sopenharmony_ci      lima_ctx_buff_va(ctx, lima_ctx_buff_pp_uniform_array));
928bf215546Sopenharmony_ci}
929bf215546Sopenharmony_ci
930bf215546Sopenharmony_cistatic void
931bf215546Sopenharmony_cilima_update_varying(struct lima_context *ctx, const struct pipe_draw_info *info,
932bf215546Sopenharmony_ci                    const struct pipe_draw_start_count_bias *draw)
933bf215546Sopenharmony_ci{
934bf215546Sopenharmony_ci   struct lima_job *job = lima_job_get(ctx);
935bf215546Sopenharmony_ci   struct lima_screen *screen = lima_screen(ctx->base.screen);
936bf215546Sopenharmony_ci   struct lima_vs_compiled_shader *vs = ctx->vs;
937bf215546Sopenharmony_ci   uint32_t gp_output_size;
938bf215546Sopenharmony_ci   unsigned num = info->index_size ? (ctx->max_index - ctx->min_index + 1) : draw->count;
939bf215546Sopenharmony_ci
940bf215546Sopenharmony_ci   uint32_t *varying =
941bf215546Sopenharmony_ci      lima_ctx_buff_alloc(ctx, lima_ctx_buff_gp_varying_info,
942bf215546Sopenharmony_ci                          vs->state.num_outputs * 8);
943bf215546Sopenharmony_ci   int n = 0;
944bf215546Sopenharmony_ci
945bf215546Sopenharmony_ci   int offset = 0;
946bf215546Sopenharmony_ci
947bf215546Sopenharmony_ci   for (int i = 0; i < vs->state.num_outputs; i++) {
948bf215546Sopenharmony_ci      struct lima_varying_info *v = vs->state.varying + i;
949bf215546Sopenharmony_ci
950bf215546Sopenharmony_ci      if (i == vs->state.gl_pos_idx ||
951bf215546Sopenharmony_ci          i == vs->state.point_size_idx)
952bf215546Sopenharmony_ci         continue;
953bf215546Sopenharmony_ci
954bf215546Sopenharmony_ci      int size = v->component_size * 4;
955bf215546Sopenharmony_ci
956bf215546Sopenharmony_ci      /* does component_size == 2 need to be 16 aligned? */
957bf215546Sopenharmony_ci      if (v->component_size == 4)
958bf215546Sopenharmony_ci         offset = align(offset, 16);
959bf215546Sopenharmony_ci
960bf215546Sopenharmony_ci      v->offset = offset;
961bf215546Sopenharmony_ci      offset += size;
962bf215546Sopenharmony_ci   }
963bf215546Sopenharmony_ci
964bf215546Sopenharmony_ci   vs->state.varying_stride = align(offset, 16);
965bf215546Sopenharmony_ci
966bf215546Sopenharmony_ci   /* gl_Position is always present, allocate space for it */
967bf215546Sopenharmony_ci   gp_output_size = align(4 * 4 * num, 0x40);
968bf215546Sopenharmony_ci
969bf215546Sopenharmony_ci   /* Allocate space for varyings if there're any */
970bf215546Sopenharmony_ci   if (vs->state.num_varyings) {
971bf215546Sopenharmony_ci      ctx->gp_output_varyings_offt = gp_output_size;
972bf215546Sopenharmony_ci      gp_output_size += align(vs->state.varying_stride * num, 0x40);
973bf215546Sopenharmony_ci   }
974bf215546Sopenharmony_ci
975bf215546Sopenharmony_ci   /* Allocate space for gl_PointSize if it's there */
976bf215546Sopenharmony_ci   if (vs->state.point_size_idx != -1) {
977bf215546Sopenharmony_ci      ctx->gp_output_point_size_offt = gp_output_size;
978bf215546Sopenharmony_ci      gp_output_size += 4 * num;
979bf215546Sopenharmony_ci   }
980bf215546Sopenharmony_ci
981bf215546Sopenharmony_ci   /* gp_output can be too large for the suballocator, so create a
982bf215546Sopenharmony_ci    * separate bo for it. The bo cache should prevent performance hit.
983bf215546Sopenharmony_ci    */
984bf215546Sopenharmony_ci   ctx->gp_output = lima_bo_create(screen, gp_output_size, 0);
985bf215546Sopenharmony_ci   assert(ctx->gp_output);
986bf215546Sopenharmony_ci   lima_job_add_bo(job, LIMA_PIPE_GP, ctx->gp_output, LIMA_SUBMIT_BO_WRITE);
987bf215546Sopenharmony_ci   lima_job_add_bo(job, LIMA_PIPE_PP, ctx->gp_output, LIMA_SUBMIT_BO_READ);
988bf215546Sopenharmony_ci
989bf215546Sopenharmony_ci   for (int i = 0; i < vs->state.num_outputs; i++) {
990bf215546Sopenharmony_ci      struct lima_varying_info *v = vs->state.varying + i;
991bf215546Sopenharmony_ci
992bf215546Sopenharmony_ci      if (i == vs->state.gl_pos_idx) {
993bf215546Sopenharmony_ci         /* gl_Position */
994bf215546Sopenharmony_ci         varying[n++] = ctx->gp_output->va;
995bf215546Sopenharmony_ci         varying[n++] = 0x8020;
996bf215546Sopenharmony_ci      } else if (i == vs->state.point_size_idx) {
997bf215546Sopenharmony_ci         /* gl_PointSize */
998bf215546Sopenharmony_ci         varying[n++] = ctx->gp_output->va + ctx->gp_output_point_size_offt;
999bf215546Sopenharmony_ci         varying[n++] = 0x2021;
1000bf215546Sopenharmony_ci      } else {
1001bf215546Sopenharmony_ci         /* Varying */
1002bf215546Sopenharmony_ci         varying[n++] = ctx->gp_output->va + ctx->gp_output_varyings_offt +
1003bf215546Sopenharmony_ci                        v->offset;
1004bf215546Sopenharmony_ci         varying[n++] = (vs->state.varying_stride << 11) | (v->components - 1) |
1005bf215546Sopenharmony_ci            (v->component_size == 2 ? 0x0C : 0);
1006bf215546Sopenharmony_ci      }
1007bf215546Sopenharmony_ci   }
1008bf215546Sopenharmony_ci
1009bf215546Sopenharmony_ci   lima_dump_command_stream_print(
1010bf215546Sopenharmony_ci      job->dump, varying, n * 4, false, "update varying info at va %x\n",
1011bf215546Sopenharmony_ci      lima_ctx_buff_va(ctx, lima_ctx_buff_gp_varying_info));
1012bf215546Sopenharmony_ci}
1013bf215546Sopenharmony_ci
1014bf215546Sopenharmony_cistatic void
1015bf215546Sopenharmony_cilima_draw_vbo_update(struct pipe_context *pctx,
1016bf215546Sopenharmony_ci                     const struct pipe_draw_info *info,
1017bf215546Sopenharmony_ci                     const struct pipe_draw_start_count_bias *draw)
1018bf215546Sopenharmony_ci{
1019bf215546Sopenharmony_ci   struct lima_context *ctx = lima_context(pctx);
1020bf215546Sopenharmony_ci   struct lima_context_framebuffer *fb = &ctx->framebuffer;
1021bf215546Sopenharmony_ci   unsigned buffers = 0;
1022bf215546Sopenharmony_ci
1023bf215546Sopenharmony_ci   if (fb->base.zsbuf) {
1024bf215546Sopenharmony_ci      if (ctx->zsa->base.depth_enabled)
1025bf215546Sopenharmony_ci         buffers |= PIPE_CLEAR_DEPTH;
1026bf215546Sopenharmony_ci      if (ctx->zsa->base.stencil[0].enabled ||
1027bf215546Sopenharmony_ci          ctx->zsa->base.stencil[1].enabled)
1028bf215546Sopenharmony_ci         buffers |= PIPE_CLEAR_STENCIL;
1029bf215546Sopenharmony_ci   }
1030bf215546Sopenharmony_ci
1031bf215546Sopenharmony_ci   if (fb->base.nr_cbufs)
1032bf215546Sopenharmony_ci      buffers |= PIPE_CLEAR_COLOR0;
1033bf215546Sopenharmony_ci
1034bf215546Sopenharmony_ci   lima_update_job_wb(ctx, buffers);
1035bf215546Sopenharmony_ci
1036bf215546Sopenharmony_ci   lima_update_gp_attribute_info(ctx, info, draw);
1037bf215546Sopenharmony_ci
1038bf215546Sopenharmony_ci   if ((ctx->dirty & LIMA_CONTEXT_DIRTY_CONST_BUFF &&
1039bf215546Sopenharmony_ci        ctx->const_buffer[PIPE_SHADER_VERTEX].dirty) ||
1040bf215546Sopenharmony_ci       ctx->dirty & LIMA_CONTEXT_DIRTY_VIEWPORT ||
1041bf215546Sopenharmony_ci       ctx->dirty & LIMA_CONTEXT_DIRTY_COMPILED_VS) {
1042bf215546Sopenharmony_ci      lima_update_gp_uniform(ctx);
1043bf215546Sopenharmony_ci      ctx->const_buffer[PIPE_SHADER_VERTEX].dirty = false;
1044bf215546Sopenharmony_ci   }
1045bf215546Sopenharmony_ci
1046bf215546Sopenharmony_ci   lima_update_varying(ctx, info, draw);
1047bf215546Sopenharmony_ci
1048bf215546Sopenharmony_ci   lima_pack_vs_cmd(ctx, info, draw);
1049bf215546Sopenharmony_ci
1050bf215546Sopenharmony_ci   if (ctx->dirty & LIMA_CONTEXT_DIRTY_CONST_BUFF &&
1051bf215546Sopenharmony_ci       ctx->const_buffer[PIPE_SHADER_FRAGMENT].dirty) {
1052bf215546Sopenharmony_ci      lima_update_pp_uniform(ctx);
1053bf215546Sopenharmony_ci      ctx->const_buffer[PIPE_SHADER_FRAGMENT].dirty = false;
1054bf215546Sopenharmony_ci   }
1055bf215546Sopenharmony_ci
1056bf215546Sopenharmony_ci   lima_update_textures(ctx);
1057bf215546Sopenharmony_ci
1058bf215546Sopenharmony_ci   lima_pack_render_state(ctx, info);
1059bf215546Sopenharmony_ci   lima_pack_plbu_cmd(ctx, info, draw);
1060bf215546Sopenharmony_ci
1061bf215546Sopenharmony_ci   if (ctx->gp_output) {
1062bf215546Sopenharmony_ci      lima_bo_unreference(ctx->gp_output); /* held by job */
1063bf215546Sopenharmony_ci      ctx->gp_output = NULL;
1064bf215546Sopenharmony_ci   }
1065bf215546Sopenharmony_ci
1066bf215546Sopenharmony_ci   ctx->dirty = 0;
1067bf215546Sopenharmony_ci}
1068bf215546Sopenharmony_ci
1069bf215546Sopenharmony_cistatic void
1070bf215546Sopenharmony_cilima_draw_vbo_indexed(struct pipe_context *pctx,
1071bf215546Sopenharmony_ci                      const struct pipe_draw_info *info,
1072bf215546Sopenharmony_ci                      const struct pipe_draw_start_count_bias *draw)
1073bf215546Sopenharmony_ci{
1074bf215546Sopenharmony_ci   struct lima_context *ctx = lima_context(pctx);
1075bf215546Sopenharmony_ci   struct lima_job *job = lima_job_get(ctx);
1076bf215546Sopenharmony_ci   struct pipe_resource *indexbuf = NULL;
1077bf215546Sopenharmony_ci   bool needs_indices = true;
1078bf215546Sopenharmony_ci
1079bf215546Sopenharmony_ci   /* Mali Utgard GPU always need min/max index info for index draw,
1080bf215546Sopenharmony_ci    * compute it if upper layer does not do for us */
1081bf215546Sopenharmony_ci   if (info->index_bounds_valid) {
1082bf215546Sopenharmony_ci      ctx->min_index = info->min_index;
1083bf215546Sopenharmony_ci      ctx->max_index = info->max_index;
1084bf215546Sopenharmony_ci      needs_indices = false;
1085bf215546Sopenharmony_ci   }
1086bf215546Sopenharmony_ci
1087bf215546Sopenharmony_ci   if (info->has_user_indices) {
1088bf215546Sopenharmony_ci      util_upload_index_buffer(&ctx->base, info, draw, &indexbuf, &ctx->index_offset, 0x40);
1089bf215546Sopenharmony_ci      ctx->index_res = lima_resource(indexbuf);
1090bf215546Sopenharmony_ci   }
1091bf215546Sopenharmony_ci   else {
1092bf215546Sopenharmony_ci      ctx->index_res = lima_resource(info->index.resource);
1093bf215546Sopenharmony_ci      ctx->index_offset = 0;
1094bf215546Sopenharmony_ci      needs_indices = !panfrost_minmax_cache_get(ctx->index_res->index_cache, draw->start,
1095bf215546Sopenharmony_ci                                                 draw->count, &ctx->min_index, &ctx->max_index);
1096bf215546Sopenharmony_ci   }
1097bf215546Sopenharmony_ci
1098bf215546Sopenharmony_ci   if (needs_indices) {
1099bf215546Sopenharmony_ci      u_vbuf_get_minmax_index(pctx, info, draw, &ctx->min_index, &ctx->max_index);
1100bf215546Sopenharmony_ci      if (!info->has_user_indices)
1101bf215546Sopenharmony_ci         panfrost_minmax_cache_add(ctx->index_res->index_cache, draw->start, draw->count,
1102bf215546Sopenharmony_ci                                   ctx->min_index, ctx->max_index);
1103bf215546Sopenharmony_ci   }
1104bf215546Sopenharmony_ci
1105bf215546Sopenharmony_ci   lima_job_add_bo(job, LIMA_PIPE_GP, ctx->index_res->bo, LIMA_SUBMIT_BO_READ);
1106bf215546Sopenharmony_ci   lima_job_add_bo(job, LIMA_PIPE_PP, ctx->index_res->bo, LIMA_SUBMIT_BO_READ);
1107bf215546Sopenharmony_ci   lima_draw_vbo_update(pctx, info, draw);
1108bf215546Sopenharmony_ci
1109bf215546Sopenharmony_ci   if (indexbuf)
1110bf215546Sopenharmony_ci      pipe_resource_reference(&indexbuf, NULL);
1111bf215546Sopenharmony_ci}
1112bf215546Sopenharmony_ci
1113bf215546Sopenharmony_cistatic void
1114bf215546Sopenharmony_cilima_draw_vbo_count(struct pipe_context *pctx,
1115bf215546Sopenharmony_ci                    const struct pipe_draw_info *info,
1116bf215546Sopenharmony_ci                    const struct pipe_draw_start_count_bias *draw)
1117bf215546Sopenharmony_ci{
1118bf215546Sopenharmony_ci   static const uint32_t max_verts = 65535;
1119bf215546Sopenharmony_ci
1120bf215546Sopenharmony_ci   struct pipe_draw_start_count_bias local_draw = *draw;
1121bf215546Sopenharmony_ci   unsigned start = draw->start;
1122bf215546Sopenharmony_ci   unsigned count = draw->count;
1123bf215546Sopenharmony_ci
1124bf215546Sopenharmony_ci   while (count) {
1125bf215546Sopenharmony_ci      unsigned this_count = count;
1126bf215546Sopenharmony_ci      unsigned step;
1127bf215546Sopenharmony_ci
1128bf215546Sopenharmony_ci      u_split_draw(info, max_verts, &this_count, &step);
1129bf215546Sopenharmony_ci
1130bf215546Sopenharmony_ci      local_draw.start = start;
1131bf215546Sopenharmony_ci      local_draw.count = this_count;
1132bf215546Sopenharmony_ci
1133bf215546Sopenharmony_ci      lima_draw_vbo_update(pctx, info, &local_draw);
1134bf215546Sopenharmony_ci
1135bf215546Sopenharmony_ci      count -= step;
1136bf215546Sopenharmony_ci      start += step;
1137bf215546Sopenharmony_ci   }
1138bf215546Sopenharmony_ci}
1139bf215546Sopenharmony_ci
1140bf215546Sopenharmony_cistatic void
1141bf215546Sopenharmony_cilima_draw_vbo(struct pipe_context *pctx,
1142bf215546Sopenharmony_ci              const struct pipe_draw_info *info,
1143bf215546Sopenharmony_ci              unsigned drawid_offset,
1144bf215546Sopenharmony_ci              const struct pipe_draw_indirect_info *indirect,
1145bf215546Sopenharmony_ci              const struct pipe_draw_start_count_bias *draws,
1146bf215546Sopenharmony_ci              unsigned num_draws)
1147bf215546Sopenharmony_ci{
1148bf215546Sopenharmony_ci   if (num_draws > 1) {
1149bf215546Sopenharmony_ci      util_draw_multi(pctx, info, drawid_offset, indirect, draws, num_draws);
1150bf215546Sopenharmony_ci      return;
1151bf215546Sopenharmony_ci   }
1152bf215546Sopenharmony_ci
1153bf215546Sopenharmony_ci   /* check if draw mode and vertex/index count match,
1154bf215546Sopenharmony_ci    * otherwise gp will hang */
1155bf215546Sopenharmony_ci   if (!u_trim_pipe_prim(info->mode, (unsigned*)&draws[0].count)) {
1156bf215546Sopenharmony_ci      debug_printf("draw mode and vertex/index count mismatch\n");
1157bf215546Sopenharmony_ci      return;
1158bf215546Sopenharmony_ci   }
1159bf215546Sopenharmony_ci
1160bf215546Sopenharmony_ci   struct lima_context *ctx = lima_context(pctx);
1161bf215546Sopenharmony_ci
1162bf215546Sopenharmony_ci   if (!ctx->uncomp_fs || !ctx->uncomp_vs) {
1163bf215546Sopenharmony_ci      debug_warn_once("no shader, skip draw\n");
1164bf215546Sopenharmony_ci      return;
1165bf215546Sopenharmony_ci   }
1166bf215546Sopenharmony_ci
1167bf215546Sopenharmony_ci   lima_clip_scissor_to_viewport(ctx);
1168bf215546Sopenharmony_ci   if (lima_is_scissor_zero(ctx))
1169bf215546Sopenharmony_ci      return;
1170bf215546Sopenharmony_ci
1171bf215546Sopenharmony_ci   /* extend the viewport in case of line draws with a line_width > 1.0f,
1172bf215546Sopenharmony_ci    * otherwise use the original values */
1173bf215546Sopenharmony_ci   lima_extend_viewport(ctx, info);
1174bf215546Sopenharmony_ci
1175bf215546Sopenharmony_ci   if (!lima_update_fs_state(ctx) || !lima_update_vs_state(ctx))
1176bf215546Sopenharmony_ci      return;
1177bf215546Sopenharmony_ci
1178bf215546Sopenharmony_ci   struct lima_job *job = lima_job_get(ctx);
1179bf215546Sopenharmony_ci   job->pp_max_stack_size = MAX2(job->pp_max_stack_size, ctx->fs->state.stack_size);
1180bf215546Sopenharmony_ci
1181bf215546Sopenharmony_ci   lima_dump_command_stream_print(
1182bf215546Sopenharmony_ci      job->dump, ctx->vs->bo->map, ctx->vs->state.shader_size, false,
1183bf215546Sopenharmony_ci      "add vs at va %x\n", ctx->vs->bo->va);
1184bf215546Sopenharmony_ci   lima_dump_shader(job->dump, ctx->vs->bo->map, ctx->vs->state.shader_size, false);
1185bf215546Sopenharmony_ci
1186bf215546Sopenharmony_ci   lima_dump_command_stream_print(
1187bf215546Sopenharmony_ci      job->dump, ctx->fs->bo->map, ctx->fs->state.shader_size, false,
1188bf215546Sopenharmony_ci      "add fs at va %x\n", ctx->fs->bo->va);
1189bf215546Sopenharmony_ci   lima_dump_shader(job->dump, ctx->fs->bo->map, ctx->fs->state.shader_size, true);
1190bf215546Sopenharmony_ci
1191bf215546Sopenharmony_ci   lima_job_add_bo(job, LIMA_PIPE_GP, ctx->vs->bo, LIMA_SUBMIT_BO_READ);
1192bf215546Sopenharmony_ci   lima_job_add_bo(job, LIMA_PIPE_PP, ctx->fs->bo, LIMA_SUBMIT_BO_READ);
1193bf215546Sopenharmony_ci
1194bf215546Sopenharmony_ci   if (info->index_size)
1195bf215546Sopenharmony_ci      lima_draw_vbo_indexed(pctx, info, &draws[0]);
1196bf215546Sopenharmony_ci   else
1197bf215546Sopenharmony_ci      lima_draw_vbo_count(pctx, info, &draws[0]);
1198bf215546Sopenharmony_ci
1199bf215546Sopenharmony_ci   job->draws++;
1200bf215546Sopenharmony_ci   /* Flush job if we hit the limit of draws per job otherwise we may
1201bf215546Sopenharmony_ci    * hit tile heap size limit */
1202bf215546Sopenharmony_ci   if (job->draws > MAX_DRAWS_PER_JOB) {
1203bf215546Sopenharmony_ci      unsigned resolve = job->resolve;
1204bf215546Sopenharmony_ci      lima_do_job(job);
1205bf215546Sopenharmony_ci      /* Subsequent job will need to resolve the same buffers */
1206bf215546Sopenharmony_ci      lima_update_job_wb(ctx, resolve);
1207bf215546Sopenharmony_ci   }
1208bf215546Sopenharmony_ci}
1209bf215546Sopenharmony_ci
1210bf215546Sopenharmony_civoid
1211bf215546Sopenharmony_cilima_draw_init(struct lima_context *ctx)
1212bf215546Sopenharmony_ci{
1213bf215546Sopenharmony_ci   ctx->base.clear = lima_clear;
1214bf215546Sopenharmony_ci   ctx->base.draw_vbo = lima_draw_vbo;
1215bf215546Sopenharmony_ci}
1216