1bf215546Sopenharmony_ci#include "zink_compiler.h"
2bf215546Sopenharmony_ci#include "zink_context.h"
3bf215546Sopenharmony_ci#include "zink_program.h"
4bf215546Sopenharmony_ci#include "zink_query.h"
5bf215546Sopenharmony_ci#include "zink_resource.h"
6bf215546Sopenharmony_ci#include "zink_screen.h"
7bf215546Sopenharmony_ci#include "zink_state.h"
8bf215546Sopenharmony_ci#include "zink_surface.h"
9bf215546Sopenharmony_ci#include "zink_inlines.h"
10bf215546Sopenharmony_ci
11bf215546Sopenharmony_ci#include "tgsi/tgsi_from_mesa.h"
12bf215546Sopenharmony_ci#include "util/hash_table.h"
13bf215546Sopenharmony_ci#include "util/u_debug.h"
14bf215546Sopenharmony_ci#include "util/u_helpers.h"
15bf215546Sopenharmony_ci#include "util/u_inlines.h"
16bf215546Sopenharmony_ci#include "util/u_prim.h"
17bf215546Sopenharmony_ci#include "util/u_prim_restart.h"
18bf215546Sopenharmony_ci
19bf215546Sopenharmony_ci
20bf215546Sopenharmony_cistatic void
21bf215546Sopenharmony_cizink_emit_xfb_counter_barrier(struct zink_context *ctx)
22bf215546Sopenharmony_ci{
23bf215546Sopenharmony_ci   for (unsigned i = 0; i < ctx->num_so_targets; i++) {
24bf215546Sopenharmony_ci      struct zink_so_target *t = zink_so_target(ctx->so_targets[i]);
25bf215546Sopenharmony_ci      if (!t)
26bf215546Sopenharmony_ci         continue;
27bf215546Sopenharmony_ci      struct zink_resource *res = zink_resource(t->counter_buffer);
28bf215546Sopenharmony_ci      VkAccessFlags access = VK_ACCESS_TRANSFORM_FEEDBACK_COUNTER_WRITE_BIT_EXT;
29bf215546Sopenharmony_ci      VkPipelineStageFlags stage = VK_PIPELINE_STAGE_TRANSFORM_FEEDBACK_BIT_EXT;
30bf215546Sopenharmony_ci      if (t->counter_buffer_valid) {
31bf215546Sopenharmony_ci         /* Between the pause and resume there needs to be a memory barrier for the counter buffers
32bf215546Sopenharmony_ci          * with a source access of VK_ACCESS_TRANSFORM_FEEDBACK_COUNTER_WRITE_BIT_EXT
33bf215546Sopenharmony_ci          * at pipeline stage VK_PIPELINE_STAGE_TRANSFORM_FEEDBACK_BIT_EXT
34bf215546Sopenharmony_ci          * to a destination access of VK_ACCESS_TRANSFORM_FEEDBACK_COUNTER_READ_BIT_EXT
35bf215546Sopenharmony_ci          * at pipeline stage VK_PIPELINE_STAGE_DRAW_INDIRECT_BIT.
36bf215546Sopenharmony_ci          *
37bf215546Sopenharmony_ci          * - from VK_EXT_transform_feedback spec
38bf215546Sopenharmony_ci          */
39bf215546Sopenharmony_ci         access |= VK_ACCESS_TRANSFORM_FEEDBACK_COUNTER_READ_BIT_EXT;
40bf215546Sopenharmony_ci         stage |= VK_PIPELINE_STAGE_DRAW_INDIRECT_BIT;
41bf215546Sopenharmony_ci      }
42bf215546Sopenharmony_ci      zink_resource_buffer_barrier(ctx, res, access, stage);
43bf215546Sopenharmony_ci      res->obj->unordered_read = false;
44bf215546Sopenharmony_ci   }
45bf215546Sopenharmony_ci}
46bf215546Sopenharmony_ci
47bf215546Sopenharmony_cistatic void
48bf215546Sopenharmony_cizink_emit_stream_output_targets(struct pipe_context *pctx)
49bf215546Sopenharmony_ci{
50bf215546Sopenharmony_ci   struct zink_context *ctx = zink_context(pctx);
51bf215546Sopenharmony_ci   struct zink_batch *batch = &ctx->batch;
52bf215546Sopenharmony_ci   VkBuffer buffers[PIPE_MAX_SO_OUTPUTS] = {0};
53bf215546Sopenharmony_ci   VkDeviceSize buffer_offsets[PIPE_MAX_SO_OUTPUTS] = {0};
54bf215546Sopenharmony_ci   VkDeviceSize buffer_sizes[PIPE_MAX_SO_OUTPUTS] = {0};
55bf215546Sopenharmony_ci
56bf215546Sopenharmony_ci   for (unsigned i = 0; i < ctx->num_so_targets; i++) {
57bf215546Sopenharmony_ci      struct zink_so_target *t = (struct zink_so_target *)ctx->so_targets[i];
58bf215546Sopenharmony_ci      if (!t) {
59bf215546Sopenharmony_ci         /* no need to reference this or anything */
60bf215546Sopenharmony_ci         buffers[i] = zink_resource(ctx->dummy_xfb_buffer)->obj->buffer;
61bf215546Sopenharmony_ci         buffer_offsets[i] = 0;
62bf215546Sopenharmony_ci         buffer_sizes[i] = sizeof(uint8_t);
63bf215546Sopenharmony_ci         continue;
64bf215546Sopenharmony_ci      }
65bf215546Sopenharmony_ci      struct zink_resource *res = zink_resource(t->base.buffer);
66bf215546Sopenharmony_ci      if (!res->so_valid)
67bf215546Sopenharmony_ci         /* resource has been rebound */
68bf215546Sopenharmony_ci         t->counter_buffer_valid = false;
69bf215546Sopenharmony_ci      buffers[i] = res->obj->buffer;
70bf215546Sopenharmony_ci      zink_batch_reference_resource_rw(batch, res, true);
71bf215546Sopenharmony_ci      buffer_offsets[i] = t->base.buffer_offset;
72bf215546Sopenharmony_ci      buffer_sizes[i] = t->base.buffer_size;
73bf215546Sopenharmony_ci      res->so_valid = true;
74bf215546Sopenharmony_ci      util_range_add(t->base.buffer, &res->valid_buffer_range, t->base.buffer_offset,
75bf215546Sopenharmony_ci                     t->base.buffer_offset + t->base.buffer_size);
76bf215546Sopenharmony_ci   }
77bf215546Sopenharmony_ci
78bf215546Sopenharmony_ci   VKCTX(CmdBindTransformFeedbackBuffersEXT)(batch->state->cmdbuf, 0, ctx->num_so_targets,
79bf215546Sopenharmony_ci                                                 buffers, buffer_offsets,
80bf215546Sopenharmony_ci                                                 buffer_sizes);
81bf215546Sopenharmony_ci   ctx->dirty_so_targets = false;
82bf215546Sopenharmony_ci}
83bf215546Sopenharmony_ci
84bf215546Sopenharmony_ciALWAYS_INLINE static void
85bf215546Sopenharmony_cicheck_buffer_barrier(struct zink_context *ctx, struct pipe_resource *pres, VkAccessFlags flags, VkPipelineStageFlags pipeline)
86bf215546Sopenharmony_ci{
87bf215546Sopenharmony_ci   struct zink_resource *res = zink_resource(pres);
88bf215546Sopenharmony_ci   zink_resource_buffer_barrier(ctx, res, flags, pipeline);
89bf215546Sopenharmony_ci   res->obj->unordered_read = false;
90bf215546Sopenharmony_ci}
91bf215546Sopenharmony_ci
92bf215546Sopenharmony_ciALWAYS_INLINE static void
93bf215546Sopenharmony_cibarrier_draw_buffers(struct zink_context *ctx, const struct pipe_draw_info *dinfo,
94bf215546Sopenharmony_ci                     const struct pipe_draw_indirect_info *dindirect, struct pipe_resource *index_buffer)
95bf215546Sopenharmony_ci{
96bf215546Sopenharmony_ci   if (index_buffer)
97bf215546Sopenharmony_ci      check_buffer_barrier(ctx, index_buffer, VK_ACCESS_INDEX_READ_BIT, VK_PIPELINE_STAGE_VERTEX_INPUT_BIT);
98bf215546Sopenharmony_ci   if (dindirect && dindirect->buffer) {
99bf215546Sopenharmony_ci      check_buffer_barrier(ctx, dindirect->buffer,
100bf215546Sopenharmony_ci                           VK_ACCESS_INDIRECT_COMMAND_READ_BIT, VK_PIPELINE_STAGE_DRAW_INDIRECT_BIT);
101bf215546Sopenharmony_ci      if (dindirect->indirect_draw_count)
102bf215546Sopenharmony_ci         check_buffer_barrier(ctx, dindirect->indirect_draw_count,
103bf215546Sopenharmony_ci                              VK_ACCESS_INDIRECT_COMMAND_READ_BIT, VK_PIPELINE_STAGE_DRAW_INDIRECT_BIT);
104bf215546Sopenharmony_ci   }
105bf215546Sopenharmony_ci}
106bf215546Sopenharmony_ci
107bf215546Sopenharmony_citemplate <zink_dynamic_state DYNAMIC_STATE>
108bf215546Sopenharmony_cistatic void
109bf215546Sopenharmony_cizink_bind_vertex_buffers(struct zink_batch *batch, struct zink_context *ctx)
110bf215546Sopenharmony_ci{
111bf215546Sopenharmony_ci   VkBuffer buffers[PIPE_MAX_ATTRIBS];
112bf215546Sopenharmony_ci   VkDeviceSize buffer_offsets[PIPE_MAX_ATTRIBS];
113bf215546Sopenharmony_ci   VkDeviceSize buffer_strides[PIPE_MAX_ATTRIBS];
114bf215546Sopenharmony_ci   struct zink_vertex_elements_state *elems = ctx->element_state;
115bf215546Sopenharmony_ci   struct zink_screen *screen = zink_screen(ctx->base.screen);
116bf215546Sopenharmony_ci
117bf215546Sopenharmony_ci   if (!elems->hw_state.num_bindings)
118bf215546Sopenharmony_ci      return;
119bf215546Sopenharmony_ci
120bf215546Sopenharmony_ci   for (unsigned i = 0; i < elems->hw_state.num_bindings; i++) {
121bf215546Sopenharmony_ci      struct pipe_vertex_buffer *vb = ctx->vertex_buffers + ctx->element_state->binding_map[i];
122bf215546Sopenharmony_ci      assert(vb);
123bf215546Sopenharmony_ci      if (vb->buffer.resource) {
124bf215546Sopenharmony_ci         struct zink_resource *res = zink_resource(vb->buffer.resource);
125bf215546Sopenharmony_ci         assert(res->obj->buffer);
126bf215546Sopenharmony_ci         buffers[i] = res->obj->buffer;
127bf215546Sopenharmony_ci         buffer_offsets[i] = vb->buffer_offset;
128bf215546Sopenharmony_ci         buffer_strides[i] = vb->stride;
129bf215546Sopenharmony_ci         if (DYNAMIC_STATE == ZINK_DYNAMIC_VERTEX_INPUT)
130bf215546Sopenharmony_ci            elems->hw_state.dynbindings[i].stride = vb->stride;
131bf215546Sopenharmony_ci      } else {
132bf215546Sopenharmony_ci         buffers[i] = zink_resource(ctx->dummy_vertex_buffer)->obj->buffer;
133bf215546Sopenharmony_ci         buffer_offsets[i] = 0;
134bf215546Sopenharmony_ci         buffer_strides[i] = 0;
135bf215546Sopenharmony_ci         if (DYNAMIC_STATE == ZINK_DYNAMIC_VERTEX_INPUT)
136bf215546Sopenharmony_ci            elems->hw_state.dynbindings[i].stride = 0;
137bf215546Sopenharmony_ci      }
138bf215546Sopenharmony_ci   }
139bf215546Sopenharmony_ci
140bf215546Sopenharmony_ci   if (DYNAMIC_STATE != ZINK_NO_DYNAMIC_STATE && DYNAMIC_STATE != ZINK_DYNAMIC_VERTEX_INPUT)
141bf215546Sopenharmony_ci      VKCTX(CmdBindVertexBuffers2EXT)(batch->state->cmdbuf, 0,
142bf215546Sopenharmony_ci                                          elems->hw_state.num_bindings,
143bf215546Sopenharmony_ci                                          buffers, buffer_offsets, NULL, buffer_strides);
144bf215546Sopenharmony_ci   else
145bf215546Sopenharmony_ci      VKSCR(CmdBindVertexBuffers)(batch->state->cmdbuf, 0,
146bf215546Sopenharmony_ci                             elems->hw_state.num_bindings,
147bf215546Sopenharmony_ci                             buffers, buffer_offsets);
148bf215546Sopenharmony_ci
149bf215546Sopenharmony_ci   if (DYNAMIC_STATE == ZINK_DYNAMIC_VERTEX_INPUT)
150bf215546Sopenharmony_ci      VKCTX(CmdSetVertexInputEXT)(batch->state->cmdbuf,
151bf215546Sopenharmony_ci                                      elems->hw_state.num_bindings, elems->hw_state.dynbindings,
152bf215546Sopenharmony_ci                                      elems->hw_state.num_attribs, elems->hw_state.dynattribs);
153bf215546Sopenharmony_ci
154bf215546Sopenharmony_ci   ctx->vertex_buffers_dirty = false;
155bf215546Sopenharmony_ci}
156bf215546Sopenharmony_ci
157bf215546Sopenharmony_cistatic void
158bf215546Sopenharmony_cizink_bind_vertex_state(struct zink_batch *batch, struct zink_context *ctx,
159bf215546Sopenharmony_ci                       struct pipe_vertex_state *vstate, uint32_t partial_velem_mask)
160bf215546Sopenharmony_ci{
161bf215546Sopenharmony_ci   if (!vstate->input.vbuffer.buffer.resource)
162bf215546Sopenharmony_ci      return;
163bf215546Sopenharmony_ci
164bf215546Sopenharmony_ci   const struct zink_vertex_elements_hw_state *hw_state = zink_vertex_state_mask(vstate, partial_velem_mask, true);
165bf215546Sopenharmony_ci   assert(hw_state);
166bf215546Sopenharmony_ci
167bf215546Sopenharmony_ci   struct zink_resource *res = zink_resource(vstate->input.vbuffer.buffer.resource);
168bf215546Sopenharmony_ci   zink_batch_resource_usage_set(&ctx->batch, res, false);
169bf215546Sopenharmony_ci   VkDeviceSize offset = vstate->input.vbuffer.buffer_offset;
170bf215546Sopenharmony_ci   VKCTX(CmdBindVertexBuffers)(batch->state->cmdbuf, 0,
171bf215546Sopenharmony_ci                               hw_state->num_bindings,
172bf215546Sopenharmony_ci                               &res->obj->buffer, &offset);
173bf215546Sopenharmony_ci
174bf215546Sopenharmony_ci   VKCTX(CmdSetVertexInputEXT)(batch->state->cmdbuf,
175bf215546Sopenharmony_ci                               hw_state->num_bindings, hw_state->dynbindings,
176bf215546Sopenharmony_ci                               hw_state->num_attribs, hw_state->dynattribs);
177bf215546Sopenharmony_ci}
178bf215546Sopenharmony_ci
179bf215546Sopenharmony_cistatic void
180bf215546Sopenharmony_ciupdate_gfx_program(struct zink_context *ctx)
181bf215546Sopenharmony_ci{
182bf215546Sopenharmony_ci   if (ctx->last_vertex_stage_dirty) {
183bf215546Sopenharmony_ci      enum pipe_shader_type pstage = pipe_shader_type_from_mesa(ctx->last_vertex_stage->nir->info.stage);
184bf215546Sopenharmony_ci      ctx->dirty_shader_stages |= BITFIELD_BIT(pstage);
185bf215546Sopenharmony_ci      memcpy(&ctx->gfx_pipeline_state.shader_keys.key[pstage].key.vs_base,
186bf215546Sopenharmony_ci             &ctx->gfx_pipeline_state.shader_keys.last_vertex.key.vs_base,
187bf215546Sopenharmony_ci             sizeof(struct zink_vs_key_base));
188bf215546Sopenharmony_ci      ctx->last_vertex_stage_dirty = false;
189bf215546Sopenharmony_ci   }
190bf215546Sopenharmony_ci   unsigned bits = BITFIELD_MASK(PIPE_SHADER_COMPUTE);
191bf215546Sopenharmony_ci   if (ctx->gfx_dirty) {
192bf215546Sopenharmony_ci      struct zink_gfx_program *prog = NULL;
193bf215546Sopenharmony_ci
194bf215546Sopenharmony_ci      struct hash_table *ht = &ctx->program_cache[ctx->shader_stages >> 2];
195bf215546Sopenharmony_ci      const uint32_t hash = ctx->gfx_hash;
196bf215546Sopenharmony_ci      struct hash_entry *entry = _mesa_hash_table_search_pre_hashed(ht, hash, ctx->gfx_stages);
197bf215546Sopenharmony_ci      if (entry) {
198bf215546Sopenharmony_ci         prog = (struct zink_gfx_program*)entry->data;
199bf215546Sopenharmony_ci         u_foreach_bit(stage, prog->stages_present & ~ctx->dirty_shader_stages)
200bf215546Sopenharmony_ci            ctx->gfx_pipeline_state.modules[stage] = prog->modules[stage]->shader;
201bf215546Sopenharmony_ci         /* ensure variants are always updated if keys have changed since last use */
202bf215546Sopenharmony_ci         ctx->dirty_shader_stages |= prog->stages_present;
203bf215546Sopenharmony_ci      } else {
204bf215546Sopenharmony_ci         ctx->dirty_shader_stages |= bits;
205bf215546Sopenharmony_ci         prog = zink_create_gfx_program(ctx, ctx->gfx_stages, ctx->gfx_pipeline_state.dyn_state2.vertices_per_patch);
206bf215546Sopenharmony_ci         _mesa_hash_table_insert_pre_hashed(ht, hash, prog->shaders, prog);
207bf215546Sopenharmony_ci      }
208bf215546Sopenharmony_ci      zink_update_gfx_program(ctx, prog);
209bf215546Sopenharmony_ci      if (prog && prog != ctx->curr_program)
210bf215546Sopenharmony_ci         zink_batch_reference_program(&ctx->batch, &prog->base);
211bf215546Sopenharmony_ci      if (ctx->curr_program)
212bf215546Sopenharmony_ci         ctx->gfx_pipeline_state.final_hash ^= ctx->curr_program->last_variant_hash;
213bf215546Sopenharmony_ci      ctx->curr_program = prog;
214bf215546Sopenharmony_ci      ctx->gfx_pipeline_state.final_hash ^= ctx->curr_program->last_variant_hash;
215bf215546Sopenharmony_ci      ctx->gfx_dirty = false;
216bf215546Sopenharmony_ci   } else if (ctx->dirty_shader_stages & bits) {
217bf215546Sopenharmony_ci      /* remove old hash */
218bf215546Sopenharmony_ci      ctx->gfx_pipeline_state.final_hash ^= ctx->curr_program->last_variant_hash;
219bf215546Sopenharmony_ci      zink_update_gfx_program(ctx, ctx->curr_program);
220bf215546Sopenharmony_ci      /* apply new hash */
221bf215546Sopenharmony_ci      ctx->gfx_pipeline_state.final_hash ^= ctx->curr_program->last_variant_hash;
222bf215546Sopenharmony_ci   }
223bf215546Sopenharmony_ci   ctx->dirty_shader_stages &= ~bits;
224bf215546Sopenharmony_ci}
225bf215546Sopenharmony_ci
226bf215546Sopenharmony_ciALWAYS_INLINE static void
227bf215546Sopenharmony_ciupdate_drawid(struct zink_context *ctx, unsigned draw_id)
228bf215546Sopenharmony_ci{
229bf215546Sopenharmony_ci   VKCTX(CmdPushConstants)(ctx->batch.state->cmdbuf, ctx->curr_program->base.layout, VK_SHADER_STAGE_VERTEX_BIT,
230bf215546Sopenharmony_ci                      offsetof(struct zink_gfx_push_constant, draw_id), sizeof(unsigned),
231bf215546Sopenharmony_ci                      &draw_id);
232bf215546Sopenharmony_ci}
233bf215546Sopenharmony_ci
234bf215546Sopenharmony_ciALWAYS_INLINE static void
235bf215546Sopenharmony_cidraw_indexed_need_index_buffer_unref(struct zink_context *ctx,
236bf215546Sopenharmony_ci             const struct pipe_draw_info *dinfo,
237bf215546Sopenharmony_ci             const struct pipe_draw_start_count_bias *draws,
238bf215546Sopenharmony_ci             unsigned num_draws,
239bf215546Sopenharmony_ci             unsigned draw_id,
240bf215546Sopenharmony_ci             bool needs_drawid)
241bf215546Sopenharmony_ci{
242bf215546Sopenharmony_ci   VkCommandBuffer cmdbuf = ctx->batch.state->cmdbuf;
243bf215546Sopenharmony_ci   if (dinfo->increment_draw_id && needs_drawid) {
244bf215546Sopenharmony_ci      for (unsigned i = 0; i < num_draws; i++) {
245bf215546Sopenharmony_ci         update_drawid(ctx, draw_id);
246bf215546Sopenharmony_ci         VKCTX(CmdDrawIndexed)(cmdbuf,
247bf215546Sopenharmony_ci            draws[i].count, dinfo->instance_count,
248bf215546Sopenharmony_ci            0, draws[i].index_bias, dinfo->start_instance);
249bf215546Sopenharmony_ci         draw_id++;
250bf215546Sopenharmony_ci      }
251bf215546Sopenharmony_ci   } else {
252bf215546Sopenharmony_ci      if (needs_drawid)
253bf215546Sopenharmony_ci         update_drawid(ctx, draw_id);
254bf215546Sopenharmony_ci      for (unsigned i = 0; i < num_draws; i++)
255bf215546Sopenharmony_ci         VKCTX(CmdDrawIndexed)(cmdbuf,
256bf215546Sopenharmony_ci            draws[i].count, dinfo->instance_count,
257bf215546Sopenharmony_ci            0, draws[i].index_bias, dinfo->start_instance);
258bf215546Sopenharmony_ci
259bf215546Sopenharmony_ci   }
260bf215546Sopenharmony_ci}
261bf215546Sopenharmony_ci
262bf215546Sopenharmony_citemplate <zink_multidraw HAS_MULTIDRAW>
263bf215546Sopenharmony_ciALWAYS_INLINE static void
264bf215546Sopenharmony_cidraw_indexed(struct zink_context *ctx,
265bf215546Sopenharmony_ci             const struct pipe_draw_info *dinfo,
266bf215546Sopenharmony_ci             const struct pipe_draw_start_count_bias *draws,
267bf215546Sopenharmony_ci             unsigned num_draws,
268bf215546Sopenharmony_ci             unsigned draw_id,
269bf215546Sopenharmony_ci             bool needs_drawid)
270bf215546Sopenharmony_ci{
271bf215546Sopenharmony_ci   VkCommandBuffer cmdbuf = ctx->batch.state->cmdbuf;
272bf215546Sopenharmony_ci   if (dinfo->increment_draw_id && needs_drawid) {
273bf215546Sopenharmony_ci      for (unsigned i = 0; i < num_draws; i++) {
274bf215546Sopenharmony_ci         update_drawid(ctx, draw_id);
275bf215546Sopenharmony_ci         VKCTX(CmdDrawIndexed)(cmdbuf,
276bf215546Sopenharmony_ci            draws[i].count, dinfo->instance_count,
277bf215546Sopenharmony_ci            draws[i].start, draws[i].index_bias, dinfo->start_instance);
278bf215546Sopenharmony_ci         draw_id++;
279bf215546Sopenharmony_ci      }
280bf215546Sopenharmony_ci   } else {
281bf215546Sopenharmony_ci      if (needs_drawid)
282bf215546Sopenharmony_ci         update_drawid(ctx, draw_id);
283bf215546Sopenharmony_ci      if (HAS_MULTIDRAW) {
284bf215546Sopenharmony_ci         VKCTX(CmdDrawMultiIndexedEXT)(cmdbuf, num_draws, (const VkMultiDrawIndexedInfoEXT*)draws,
285bf215546Sopenharmony_ci                                       dinfo->instance_count,
286bf215546Sopenharmony_ci                                       dinfo->start_instance, sizeof(struct pipe_draw_start_count_bias),
287bf215546Sopenharmony_ci                                       dinfo->index_bias_varies ? NULL : &draws[0].index_bias);
288bf215546Sopenharmony_ci      } else {
289bf215546Sopenharmony_ci         for (unsigned i = 0; i < num_draws; i++)
290bf215546Sopenharmony_ci            VKCTX(CmdDrawIndexed)(cmdbuf,
291bf215546Sopenharmony_ci               draws[i].count, dinfo->instance_count,
292bf215546Sopenharmony_ci               draws[i].start, draws[i].index_bias, dinfo->start_instance);
293bf215546Sopenharmony_ci      }
294bf215546Sopenharmony_ci   }
295bf215546Sopenharmony_ci}
296bf215546Sopenharmony_ci
297bf215546Sopenharmony_citemplate <zink_multidraw HAS_MULTIDRAW>
298bf215546Sopenharmony_ciALWAYS_INLINE static void
299bf215546Sopenharmony_cidraw(struct zink_context *ctx,
300bf215546Sopenharmony_ci     const struct pipe_draw_info *dinfo,
301bf215546Sopenharmony_ci     const struct pipe_draw_start_count_bias *draws,
302bf215546Sopenharmony_ci     unsigned num_draws,
303bf215546Sopenharmony_ci     unsigned draw_id,
304bf215546Sopenharmony_ci     bool needs_drawid)
305bf215546Sopenharmony_ci{
306bf215546Sopenharmony_ci   VkCommandBuffer cmdbuf = ctx->batch.state->cmdbuf;
307bf215546Sopenharmony_ci   if (dinfo->increment_draw_id && needs_drawid) {
308bf215546Sopenharmony_ci      for (unsigned i = 0; i < num_draws; i++) {
309bf215546Sopenharmony_ci         update_drawid(ctx, draw_id);
310bf215546Sopenharmony_ci         VKCTX(CmdDraw)(cmdbuf, draws[i].count, dinfo->instance_count, draws[i].start, dinfo->start_instance);
311bf215546Sopenharmony_ci         draw_id++;
312bf215546Sopenharmony_ci      }
313bf215546Sopenharmony_ci   } else {
314bf215546Sopenharmony_ci      if (needs_drawid)
315bf215546Sopenharmony_ci         update_drawid(ctx, draw_id);
316bf215546Sopenharmony_ci      if (HAS_MULTIDRAW)
317bf215546Sopenharmony_ci         VKCTX(CmdDrawMultiEXT)(cmdbuf, num_draws, (const VkMultiDrawInfoEXT*)draws,
318bf215546Sopenharmony_ci                                dinfo->instance_count, dinfo->start_instance,
319bf215546Sopenharmony_ci                                sizeof(struct pipe_draw_start_count_bias));
320bf215546Sopenharmony_ci      else {
321bf215546Sopenharmony_ci         for (unsigned i = 0; i < num_draws; i++)
322bf215546Sopenharmony_ci            VKCTX(CmdDraw)(cmdbuf, draws[i].count, dinfo->instance_count, draws[i].start, dinfo->start_instance);
323bf215546Sopenharmony_ci
324bf215546Sopenharmony_ci      }
325bf215546Sopenharmony_ci   }
326bf215546Sopenharmony_ci}
327bf215546Sopenharmony_ci
328bf215546Sopenharmony_cistatic void
329bf215546Sopenharmony_ciupdate_barriers(struct zink_context *ctx, bool is_compute,
330bf215546Sopenharmony_ci                struct pipe_resource *index, struct pipe_resource *indirect, struct pipe_resource *indirect_draw_count)
331bf215546Sopenharmony_ci{
332bf215546Sopenharmony_ci   if (!ctx->need_barriers[is_compute]->entries)
333bf215546Sopenharmony_ci      return;
334bf215546Sopenharmony_ci   struct set *need_barriers = ctx->need_barriers[is_compute];
335bf215546Sopenharmony_ci   ctx->barrier_set_idx[is_compute] = !ctx->barrier_set_idx[is_compute];
336bf215546Sopenharmony_ci   ctx->need_barriers[is_compute] = &ctx->update_barriers[is_compute][ctx->barrier_set_idx[is_compute]];
337bf215546Sopenharmony_ci   set_foreach(need_barriers, he) {
338bf215546Sopenharmony_ci      struct zink_resource *res = (struct zink_resource *)he->key;
339bf215546Sopenharmony_ci      if (res->bind_count[is_compute]) {
340bf215546Sopenharmony_ci         VkPipelineStageFlagBits pipeline = is_compute ? VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT : res->gfx_barrier;
341bf215546Sopenharmony_ci         if (res->base.b.target == PIPE_BUFFER)
342bf215546Sopenharmony_ci            zink_resource_buffer_barrier(ctx, res, res->barrier_access[is_compute], pipeline);
343bf215546Sopenharmony_ci         else {
344bf215546Sopenharmony_ci            VkImageLayout layout = zink_descriptor_util_image_layout_eval(ctx, res, is_compute);
345bf215546Sopenharmony_ci            if (layout != res->layout)
346bf215546Sopenharmony_ci               zink_resource_image_barrier(ctx, res, layout, res->barrier_access[is_compute], pipeline);
347bf215546Sopenharmony_ci         }
348bf215546Sopenharmony_ci         if (zink_resource_access_is_write(res->barrier_access[is_compute]))
349bf215546Sopenharmony_ci            res->obj->unordered_read = res->obj->unordered_write = false;
350bf215546Sopenharmony_ci         else
351bf215546Sopenharmony_ci            res->obj->unordered_read = false;
352bf215546Sopenharmony_ci         /* always barrier on draw if this resource has either multiple image write binds or
353bf215546Sopenharmony_ci          * image write binds and image read binds
354bf215546Sopenharmony_ci          */
355bf215546Sopenharmony_ci         if (res->write_bind_count[is_compute] && res->bind_count[is_compute] > 1)
356bf215546Sopenharmony_ci            _mesa_set_add_pre_hashed(ctx->need_barriers[is_compute], he->hash, res);
357bf215546Sopenharmony_ci      }
358bf215546Sopenharmony_ci      _mesa_set_remove(need_barriers, he);
359bf215546Sopenharmony_ci      if (!need_barriers->entries)
360bf215546Sopenharmony_ci         break;
361bf215546Sopenharmony_ci   }
362bf215546Sopenharmony_ci}
363bf215546Sopenharmony_ci
364bf215546Sopenharmony_citemplate <bool BATCH_CHANGED>
365bf215546Sopenharmony_cistatic bool
366bf215546Sopenharmony_ciupdate_gfx_pipeline(struct zink_context *ctx, struct zink_batch_state *bs, enum pipe_prim_type mode)
367bf215546Sopenharmony_ci{
368bf215546Sopenharmony_ci   VkPipeline prev_pipeline = ctx->gfx_pipeline_state.pipeline;
369bf215546Sopenharmony_ci   update_gfx_program(ctx);
370bf215546Sopenharmony_ci   VkPipeline pipeline = zink_get_gfx_pipeline(ctx, ctx->curr_program, &ctx->gfx_pipeline_state, mode);
371bf215546Sopenharmony_ci   bool pipeline_changed = prev_pipeline != pipeline;
372bf215546Sopenharmony_ci   if (BATCH_CHANGED || pipeline_changed)
373bf215546Sopenharmony_ci      VKCTX(CmdBindPipeline)(bs->cmdbuf, VK_PIPELINE_BIND_POINT_GRAPHICS, pipeline);
374bf215546Sopenharmony_ci   return pipeline_changed;
375bf215546Sopenharmony_ci}
376bf215546Sopenharmony_ci
377bf215546Sopenharmony_cistatic bool
378bf215546Sopenharmony_cihack_conditional_render(struct pipe_context *pctx,
379bf215546Sopenharmony_ci                        const struct pipe_draw_info *dinfo,
380bf215546Sopenharmony_ci                        unsigned drawid_offset,
381bf215546Sopenharmony_ci                        const struct pipe_draw_indirect_info *dindirect,
382bf215546Sopenharmony_ci                        const struct pipe_draw_start_count_bias *draws,
383bf215546Sopenharmony_ci                        unsigned num_draws)
384bf215546Sopenharmony_ci{
385bf215546Sopenharmony_ci   struct zink_context *ctx = zink_context(pctx);
386bf215546Sopenharmony_ci   struct zink_batch_state *bs = ctx->batch.state;
387bf215546Sopenharmony_ci   static bool warned;
388bf215546Sopenharmony_ci   if (!warned) {
389bf215546Sopenharmony_ci      fprintf(stderr, "ZINK: warning, this is cpu-based conditional rendering, say bye-bye to fps\n");
390bf215546Sopenharmony_ci      warned = true;
391bf215546Sopenharmony_ci   }
392bf215546Sopenharmony_ci   if (!zink_check_conditional_render(ctx))
393bf215546Sopenharmony_ci      return false;
394bf215546Sopenharmony_ci   if (bs != ctx->batch.state) {
395bf215546Sopenharmony_ci      bool prev = ctx->render_condition_active;
396bf215546Sopenharmony_ci      ctx->render_condition_active = false;
397bf215546Sopenharmony_ci      zink_select_draw_vbo(ctx);
398bf215546Sopenharmony_ci      pctx->draw_vbo(pctx, dinfo, drawid_offset, dindirect, draws, num_draws);
399bf215546Sopenharmony_ci      ctx->render_condition_active = prev;
400bf215546Sopenharmony_ci      return false;
401bf215546Sopenharmony_ci   }
402bf215546Sopenharmony_ci   return true;
403bf215546Sopenharmony_ci}
404bf215546Sopenharmony_ci
405bf215546Sopenharmony_citemplate <zink_multidraw HAS_MULTIDRAW, zink_dynamic_state DYNAMIC_STATE, bool BATCH_CHANGED, bool DRAW_STATE>
406bf215546Sopenharmony_civoid
407bf215546Sopenharmony_cizink_draw(struct pipe_context *pctx,
408bf215546Sopenharmony_ci          const struct pipe_draw_info *dinfo,
409bf215546Sopenharmony_ci          unsigned drawid_offset,
410bf215546Sopenharmony_ci          const struct pipe_draw_indirect_info *dindirect,
411bf215546Sopenharmony_ci          const struct pipe_draw_start_count_bias *draws,
412bf215546Sopenharmony_ci          unsigned num_draws,
413bf215546Sopenharmony_ci          struct pipe_vertex_state *vstate,
414bf215546Sopenharmony_ci          uint32_t partial_velem_mask)
415bf215546Sopenharmony_ci{
416bf215546Sopenharmony_ci   if (!dindirect && (!draws[0].count || !dinfo->instance_count))
417bf215546Sopenharmony_ci      return;
418bf215546Sopenharmony_ci
419bf215546Sopenharmony_ci   struct zink_context *ctx = zink_context(pctx);
420bf215546Sopenharmony_ci   struct zink_screen *screen = zink_screen(pctx->screen);
421bf215546Sopenharmony_ci   struct zink_rasterizer_state *rast_state = ctx->rast_state;
422bf215546Sopenharmony_ci   struct zink_depth_stencil_alpha_state *dsa_state = ctx->dsa_state;
423bf215546Sopenharmony_ci   struct zink_batch *batch = &ctx->batch;
424bf215546Sopenharmony_ci   struct zink_so_target *so_target =
425bf215546Sopenharmony_ci      dindirect && dindirect->count_from_stream_output ?
426bf215546Sopenharmony_ci         zink_so_target(dindirect->count_from_stream_output) : NULL;
427bf215546Sopenharmony_ci   VkBuffer counter_buffers[PIPE_MAX_SO_OUTPUTS];
428bf215546Sopenharmony_ci   VkDeviceSize counter_buffer_offsets[PIPE_MAX_SO_OUTPUTS];
429bf215546Sopenharmony_ci   bool need_index_buffer_unref = false;
430bf215546Sopenharmony_ci   bool mode_changed = ctx->gfx_pipeline_state.gfx_prim_mode != dinfo->mode;
431bf215546Sopenharmony_ci   bool reads_drawid = ctx->shader_reads_drawid;
432bf215546Sopenharmony_ci   bool reads_basevertex = ctx->shader_reads_basevertex;
433bf215546Sopenharmony_ci   unsigned work_count = ctx->batch.work_count;
434bf215546Sopenharmony_ci   enum pipe_prim_type mode = (enum pipe_prim_type)dinfo->mode;
435bf215546Sopenharmony_ci
436bf215546Sopenharmony_ci   if (unlikely(!screen->info.have_EXT_conditional_rendering)) {
437bf215546Sopenharmony_ci      if (!hack_conditional_render(pctx, dinfo, drawid_offset, dindirect, draws, num_draws))
438bf215546Sopenharmony_ci         return;
439bf215546Sopenharmony_ci   }
440bf215546Sopenharmony_ci
441bf215546Sopenharmony_ci   if (ctx->memory_barrier)
442bf215546Sopenharmony_ci      zink_flush_memory_barrier(ctx, false);
443bf215546Sopenharmony_ci
444bf215546Sopenharmony_ci   if (unlikely(ctx->buffer_rebind_counter < screen->buffer_rebind_counter)) {
445bf215546Sopenharmony_ci      ctx->buffer_rebind_counter = screen->buffer_rebind_counter;
446bf215546Sopenharmony_ci      zink_rebind_all_buffers(ctx);
447bf215546Sopenharmony_ci   }
448bf215546Sopenharmony_ci
449bf215546Sopenharmony_ci   if (unlikely(ctx->image_rebind_counter < screen->image_rebind_counter)) {
450bf215546Sopenharmony_ci      ctx->image_rebind_counter = screen->image_rebind_counter;
451bf215546Sopenharmony_ci      zink_rebind_all_images(ctx);
452bf215546Sopenharmony_ci   }
453bf215546Sopenharmony_ci
454bf215546Sopenharmony_ci   unsigned index_offset = 0;
455bf215546Sopenharmony_ci   unsigned index_size = dinfo->index_size;
456bf215546Sopenharmony_ci   struct pipe_resource *index_buffer = NULL;
457bf215546Sopenharmony_ci   if (index_size > 0) {
458bf215546Sopenharmony_ci      if (dinfo->has_user_indices) {
459bf215546Sopenharmony_ci         if (!util_upload_index_buffer(pctx, dinfo, &draws[0], &index_buffer, &index_offset, 4)) {
460bf215546Sopenharmony_ci            debug_printf("util_upload_index_buffer() failed\n");
461bf215546Sopenharmony_ci            return;
462bf215546Sopenharmony_ci         }
463bf215546Sopenharmony_ci         /* this will have extra refs from tc */
464bf215546Sopenharmony_ci         if (screen->threaded)
465bf215546Sopenharmony_ci            zink_batch_reference_resource_move(batch, zink_resource(index_buffer));
466bf215546Sopenharmony_ci         else
467bf215546Sopenharmony_ci            zink_batch_reference_resource(batch, zink_resource(index_buffer));
468bf215546Sopenharmony_ci      } else {
469bf215546Sopenharmony_ci         index_buffer = dinfo->index.resource;
470bf215546Sopenharmony_ci         zink_batch_reference_resource_rw(batch, zink_resource(index_buffer), false);
471bf215546Sopenharmony_ci      }
472bf215546Sopenharmony_ci      assert(index_size <= 4 && index_size != 3);
473bf215546Sopenharmony_ci      assert(index_size != 1 || screen->info.have_EXT_index_type_uint8);
474bf215546Sopenharmony_ci   }
475bf215546Sopenharmony_ci
476bf215546Sopenharmony_ci   bool have_streamout = !!ctx->num_so_targets;
477bf215546Sopenharmony_ci   if (have_streamout) {
478bf215546Sopenharmony_ci      zink_emit_xfb_counter_barrier(ctx);
479bf215546Sopenharmony_ci      if (ctx->dirty_so_targets) {
480bf215546Sopenharmony_ci         /* have to loop here and below because barriers must be emitted out of renderpass,
481bf215546Sopenharmony_ci          * but xfb buffers can't be bound before the renderpass is active to avoid
482bf215546Sopenharmony_ci          * breaking from recursion
483bf215546Sopenharmony_ci          */
484bf215546Sopenharmony_ci         for (unsigned i = 0; i < ctx->num_so_targets; i++) {
485bf215546Sopenharmony_ci            struct zink_so_target *t = (struct zink_so_target *)ctx->so_targets[i];
486bf215546Sopenharmony_ci            if (t) {
487bf215546Sopenharmony_ci               struct zink_resource *res = zink_resource(t->base.buffer);
488bf215546Sopenharmony_ci               zink_resource_buffer_barrier(ctx, res,
489bf215546Sopenharmony_ci                                            VK_ACCESS_TRANSFORM_FEEDBACK_WRITE_BIT_EXT, VK_PIPELINE_STAGE_TRANSFORM_FEEDBACK_BIT_EXT);
490bf215546Sopenharmony_ci               res->obj->unordered_read = res->obj->unordered_write = false;
491bf215546Sopenharmony_ci            }
492bf215546Sopenharmony_ci         }
493bf215546Sopenharmony_ci      }
494bf215546Sopenharmony_ci   }
495bf215546Sopenharmony_ci
496bf215546Sopenharmony_ci   barrier_draw_buffers(ctx, dinfo, dindirect, index_buffer);
497bf215546Sopenharmony_ci   /* this may re-emit draw buffer barriers, but such synchronization is harmless */
498bf215546Sopenharmony_ci   update_barriers(ctx, false, index_buffer, dindirect ? dindirect->buffer : NULL, dindirect ? dindirect->indirect_draw_count : NULL);
499bf215546Sopenharmony_ci
500bf215546Sopenharmony_ci   /* ensure synchronization between doing streamout with counter buffer
501bf215546Sopenharmony_ci    * and using counter buffer for indirect draw
502bf215546Sopenharmony_ci    */
503bf215546Sopenharmony_ci   if (so_target && so_target->counter_buffer_valid) {
504bf215546Sopenharmony_ci      struct zink_resource *res = zink_resource(so_target->counter_buffer);
505bf215546Sopenharmony_ci      zink_resource_buffer_barrier(ctx, res,
506bf215546Sopenharmony_ci                                   VK_ACCESS_TRANSFORM_FEEDBACK_COUNTER_READ_BIT_EXT,
507bf215546Sopenharmony_ci                                   VK_PIPELINE_STAGE_DRAW_INDIRECT_BIT);
508bf215546Sopenharmony_ci      res->obj->unordered_read = false;
509bf215546Sopenharmony_ci   }
510bf215546Sopenharmony_ci
511bf215546Sopenharmony_ci   zink_query_update_gs_states(ctx, dinfo->was_line_loop);
512bf215546Sopenharmony_ci
513bf215546Sopenharmony_ci   if (unlikely(zink_debug & ZINK_DEBUG_SYNC)) {
514bf215546Sopenharmony_ci      zink_batch_no_rp(ctx);
515bf215546Sopenharmony_ci      VkMemoryBarrier mb;
516bf215546Sopenharmony_ci      mb.sType = VK_STRUCTURE_TYPE_MEMORY_BARRIER;
517bf215546Sopenharmony_ci      mb.pNext = NULL;
518bf215546Sopenharmony_ci      mb.srcAccessMask = VK_ACCESS_MEMORY_WRITE_BIT;
519bf215546Sopenharmony_ci      mb.dstAccessMask = VK_ACCESS_MEMORY_READ_BIT;
520bf215546Sopenharmony_ci      VKSCR(CmdPipelineBarrier)(ctx->batch.state->cmdbuf,
521bf215546Sopenharmony_ci                                VK_PIPELINE_STAGE_ALL_COMMANDS_BIT,
522bf215546Sopenharmony_ci                                VK_PIPELINE_STAGE_ALL_COMMANDS_BIT,
523bf215546Sopenharmony_ci                                0, 1, &mb, 0, NULL, 0, NULL);
524bf215546Sopenharmony_ci   }
525bf215546Sopenharmony_ci
526bf215546Sopenharmony_ci   zink_batch_rp(ctx);
527bf215546Sopenharmony_ci   /* check dead swapchain */
528bf215546Sopenharmony_ci   if (unlikely(!ctx->batch.in_rp))
529bf215546Sopenharmony_ci      return;
530bf215546Sopenharmony_ci
531bf215546Sopenharmony_ci   if (BATCH_CHANGED)
532bf215546Sopenharmony_ci      zink_update_descriptor_refs(ctx, false);
533bf215546Sopenharmony_ci
534bf215546Sopenharmony_ci   /* these must be after renderpass start to avoid issues with recursion */
535bf215546Sopenharmony_ci   bool drawid_broken = false;
536bf215546Sopenharmony_ci   if (reads_drawid && (!dindirect || !dindirect->buffer))
537bf215546Sopenharmony_ci      drawid_broken = (drawid_offset != 0 ||
538bf215546Sopenharmony_ci                      (!HAS_MULTIDRAW && num_draws > 1) ||
539bf215546Sopenharmony_ci                      (HAS_MULTIDRAW && num_draws > 1 && !dinfo->increment_draw_id));
540bf215546Sopenharmony_ci   if (drawid_broken != zink_get_last_vertex_key(ctx)->push_drawid)
541bf215546Sopenharmony_ci      zink_set_last_vertex_key(ctx)->push_drawid = drawid_broken;
542bf215546Sopenharmony_ci   if (mode_changed) {
543bf215546Sopenharmony_ci      bool points_changed = false;
544bf215546Sopenharmony_ci      if (mode == PIPE_PRIM_POINTS) {
545bf215546Sopenharmony_ci         ctx->gfx_pipeline_state.has_points++;
546bf215546Sopenharmony_ci         points_changed = true;
547bf215546Sopenharmony_ci      } else if (ctx->gfx_pipeline_state.gfx_prim_mode == PIPE_PRIM_POINTS) {
548bf215546Sopenharmony_ci         ctx->gfx_pipeline_state.has_points--;
549bf215546Sopenharmony_ci         points_changed = true;
550bf215546Sopenharmony_ci      }
551bf215546Sopenharmony_ci      if (points_changed && ctx->rast_state->base.point_quad_rasterization)
552bf215546Sopenharmony_ci         zink_set_fs_point_coord_key(ctx);
553bf215546Sopenharmony_ci   }
554bf215546Sopenharmony_ci   ctx->gfx_pipeline_state.gfx_prim_mode = mode;
555bf215546Sopenharmony_ci
556bf215546Sopenharmony_ci   if (index_size) {
557bf215546Sopenharmony_ci      const VkIndexType index_type[3] = {
558bf215546Sopenharmony_ci         VK_INDEX_TYPE_UINT8_EXT,
559bf215546Sopenharmony_ci         VK_INDEX_TYPE_UINT16,
560bf215546Sopenharmony_ci         VK_INDEX_TYPE_UINT32,
561bf215546Sopenharmony_ci      };
562bf215546Sopenharmony_ci      struct zink_resource *res = zink_resource(index_buffer);
563bf215546Sopenharmony_ci      VKCTX(CmdBindIndexBuffer)(batch->state->cmdbuf, res->obj->buffer, index_offset, index_type[index_size >> 1]);
564bf215546Sopenharmony_ci   }
565bf215546Sopenharmony_ci   if (DYNAMIC_STATE < ZINK_DYNAMIC_STATE2) {
566bf215546Sopenharmony_ci      if (ctx->gfx_pipeline_state.dyn_state2.primitive_restart != dinfo->primitive_restart)
567bf215546Sopenharmony_ci         ctx->gfx_pipeline_state.dirty = true;
568bf215546Sopenharmony_ci      ctx->gfx_pipeline_state.dyn_state2.primitive_restart = dinfo->primitive_restart;
569bf215546Sopenharmony_ci   }
570bf215546Sopenharmony_ci
571bf215546Sopenharmony_ci   if (have_streamout && ctx->dirty_so_targets)
572bf215546Sopenharmony_ci      zink_emit_stream_output_targets(pctx);
573bf215546Sopenharmony_ci
574bf215546Sopenharmony_ci   bool pipeline_changed = update_gfx_pipeline<BATCH_CHANGED>(ctx, batch->state, mode);
575bf215546Sopenharmony_ci
576bf215546Sopenharmony_ci   if (BATCH_CHANGED || ctx->vp_state_changed || (DYNAMIC_STATE == ZINK_NO_DYNAMIC_STATE && pipeline_changed)) {
577bf215546Sopenharmony_ci      VkViewport viewports[PIPE_MAX_VIEWPORTS];
578bf215546Sopenharmony_ci      for (unsigned i = 0; i < ctx->vp_state.num_viewports; i++) {
579bf215546Sopenharmony_ci         VkViewport viewport = {
580bf215546Sopenharmony_ci            ctx->vp_state.viewport_states[i].translate[0] - ctx->vp_state.viewport_states[i].scale[0],
581bf215546Sopenharmony_ci            ctx->vp_state.viewport_states[i].translate[1] - ctx->vp_state.viewport_states[i].scale[1],
582bf215546Sopenharmony_ci            MAX2(ctx->vp_state.viewport_states[i].scale[0] * 2, 1),
583bf215546Sopenharmony_ci            ctx->vp_state.viewport_states[i].scale[1] * 2,
584bf215546Sopenharmony_ci            CLAMP(ctx->rast_state->base.clip_halfz ?
585bf215546Sopenharmony_ci                  ctx->vp_state.viewport_states[i].translate[2] :
586bf215546Sopenharmony_ci                  ctx->vp_state.viewport_states[i].translate[2] - ctx->vp_state.viewport_states[i].scale[2],
587bf215546Sopenharmony_ci                  0, 1),
588bf215546Sopenharmony_ci            CLAMP(ctx->vp_state.viewport_states[i].translate[2] + ctx->vp_state.viewport_states[i].scale[2],
589bf215546Sopenharmony_ci                  0, 1)
590bf215546Sopenharmony_ci         };
591bf215546Sopenharmony_ci         if (!ctx->rast_state->base.half_pixel_center) {
592bf215546Sopenharmony_ci             /* magic constant value from dxvk */
593bf215546Sopenharmony_ci             float cf = 0.5f - (1.0f / 128.0f);
594bf215546Sopenharmony_ci             viewport.x += cf;
595bf215546Sopenharmony_ci             if (viewport.height < 0)
596bf215546Sopenharmony_ci                viewport.y += cf;
597bf215546Sopenharmony_ci             else
598bf215546Sopenharmony_ci                viewport.y -= cf;
599bf215546Sopenharmony_ci         }
600bf215546Sopenharmony_ci         viewports[i] = viewport;
601bf215546Sopenharmony_ci      }
602bf215546Sopenharmony_ci      if (DYNAMIC_STATE != ZINK_NO_DYNAMIC_STATE)
603bf215546Sopenharmony_ci         VKCTX(CmdSetViewportWithCountEXT)(batch->state->cmdbuf, ctx->vp_state.num_viewports, viewports);
604bf215546Sopenharmony_ci      else
605bf215546Sopenharmony_ci         VKCTX(CmdSetViewport)(batch->state->cmdbuf, 0, ctx->vp_state.num_viewports, viewports);
606bf215546Sopenharmony_ci   }
607bf215546Sopenharmony_ci   if (BATCH_CHANGED || ctx->scissor_changed || ctx->vp_state_changed || (DYNAMIC_STATE == ZINK_NO_DYNAMIC_STATE && pipeline_changed)) {
608bf215546Sopenharmony_ci      VkRect2D scissors[PIPE_MAX_VIEWPORTS];
609bf215546Sopenharmony_ci      if (ctx->rast_state->base.scissor) {
610bf215546Sopenharmony_ci         for (unsigned i = 0; i < ctx->vp_state.num_viewports; i++) {
611bf215546Sopenharmony_ci            scissors[i].offset.x = ctx->vp_state.scissor_states[i].minx;
612bf215546Sopenharmony_ci            scissors[i].offset.y = ctx->vp_state.scissor_states[i].miny;
613bf215546Sopenharmony_ci            scissors[i].extent.width = ctx->vp_state.scissor_states[i].maxx - ctx->vp_state.scissor_states[i].minx;
614bf215546Sopenharmony_ci            scissors[i].extent.height = ctx->vp_state.scissor_states[i].maxy - ctx->vp_state.scissor_states[i].miny;
615bf215546Sopenharmony_ci         }
616bf215546Sopenharmony_ci      } else {
617bf215546Sopenharmony_ci         for (unsigned i = 0; i < ctx->vp_state.num_viewports; i++) {
618bf215546Sopenharmony_ci            scissors[i].offset.x = 0;
619bf215546Sopenharmony_ci            scissors[i].offset.y = 0;
620bf215546Sopenharmony_ci            scissors[i].extent.width = ctx->fb_state.width;
621bf215546Sopenharmony_ci            scissors[i].extent.height = ctx->fb_state.height;
622bf215546Sopenharmony_ci         }
623bf215546Sopenharmony_ci      }
624bf215546Sopenharmony_ci      if (DYNAMIC_STATE != ZINK_NO_DYNAMIC_STATE)
625bf215546Sopenharmony_ci         VKCTX(CmdSetScissorWithCountEXT)(batch->state->cmdbuf, ctx->vp_state.num_viewports, scissors);
626bf215546Sopenharmony_ci      else
627bf215546Sopenharmony_ci         VKCTX(CmdSetScissor)(batch->state->cmdbuf, 0, ctx->vp_state.num_viewports, scissors);
628bf215546Sopenharmony_ci   }
629bf215546Sopenharmony_ci   ctx->vp_state_changed = false;
630bf215546Sopenharmony_ci   ctx->scissor_changed = false;
631bf215546Sopenharmony_ci
632bf215546Sopenharmony_ci   if (BATCH_CHANGED || ctx->stencil_ref_changed) {
633bf215546Sopenharmony_ci      VKCTX(CmdSetStencilReference)(batch->state->cmdbuf, VK_STENCIL_FACE_FRONT_BIT,
634bf215546Sopenharmony_ci                               ctx->stencil_ref.ref_value[0]);
635bf215546Sopenharmony_ci      VKCTX(CmdSetStencilReference)(batch->state->cmdbuf, VK_STENCIL_FACE_BACK_BIT,
636bf215546Sopenharmony_ci                               ctx->stencil_ref.ref_value[1]);
637bf215546Sopenharmony_ci      ctx->stencil_ref_changed = false;
638bf215546Sopenharmony_ci   }
639bf215546Sopenharmony_ci
640bf215546Sopenharmony_ci   if (DYNAMIC_STATE != ZINK_NO_DYNAMIC_STATE && (BATCH_CHANGED || ctx->dsa_state_changed)) {
641bf215546Sopenharmony_ci      VKCTX(CmdSetDepthBoundsTestEnableEXT)(batch->state->cmdbuf, dsa_state->hw_state.depth_bounds_test);
642bf215546Sopenharmony_ci      if (dsa_state->hw_state.depth_bounds_test)
643bf215546Sopenharmony_ci         VKCTX(CmdSetDepthBounds)(batch->state->cmdbuf,
644bf215546Sopenharmony_ci                             dsa_state->hw_state.min_depth_bounds,
645bf215546Sopenharmony_ci                             dsa_state->hw_state.max_depth_bounds);
646bf215546Sopenharmony_ci      VKCTX(CmdSetDepthTestEnableEXT)(batch->state->cmdbuf, dsa_state->hw_state.depth_test);
647bf215546Sopenharmony_ci      if (dsa_state->hw_state.depth_test)
648bf215546Sopenharmony_ci         VKCTX(CmdSetDepthCompareOpEXT)(batch->state->cmdbuf, dsa_state->hw_state.depth_compare_op);
649bf215546Sopenharmony_ci      VKCTX(CmdSetDepthWriteEnableEXT)(batch->state->cmdbuf, dsa_state->hw_state.depth_write);
650bf215546Sopenharmony_ci      VKCTX(CmdSetStencilTestEnableEXT)(batch->state->cmdbuf, dsa_state->hw_state.stencil_test);
651bf215546Sopenharmony_ci      if (dsa_state->hw_state.stencil_test) {
652bf215546Sopenharmony_ci         VKCTX(CmdSetStencilOpEXT)(batch->state->cmdbuf, VK_STENCIL_FACE_FRONT_BIT,
653bf215546Sopenharmony_ci                                       dsa_state->hw_state.stencil_front.failOp,
654bf215546Sopenharmony_ci                                       dsa_state->hw_state.stencil_front.passOp,
655bf215546Sopenharmony_ci                                       dsa_state->hw_state.stencil_front.depthFailOp,
656bf215546Sopenharmony_ci                                       dsa_state->hw_state.stencil_front.compareOp);
657bf215546Sopenharmony_ci         VKCTX(CmdSetStencilOpEXT)(batch->state->cmdbuf, VK_STENCIL_FACE_BACK_BIT,
658bf215546Sopenharmony_ci                                       dsa_state->hw_state.stencil_back.failOp,
659bf215546Sopenharmony_ci                                       dsa_state->hw_state.stencil_back.passOp,
660bf215546Sopenharmony_ci                                       dsa_state->hw_state.stencil_back.depthFailOp,
661bf215546Sopenharmony_ci                                       dsa_state->hw_state.stencil_back.compareOp);
662bf215546Sopenharmony_ci         if (dsa_state->base.stencil[1].enabled) {
663bf215546Sopenharmony_ci            VKCTX(CmdSetStencilWriteMask)(batch->state->cmdbuf, VK_STENCIL_FACE_FRONT_BIT, dsa_state->hw_state.stencil_front.writeMask);
664bf215546Sopenharmony_ci            VKCTX(CmdSetStencilWriteMask)(batch->state->cmdbuf, VK_STENCIL_FACE_BACK_BIT, dsa_state->hw_state.stencil_back.writeMask);
665bf215546Sopenharmony_ci            VKCTX(CmdSetStencilCompareMask)(batch->state->cmdbuf, VK_STENCIL_FACE_FRONT_BIT, dsa_state->hw_state.stencil_front.compareMask);
666bf215546Sopenharmony_ci            VKCTX(CmdSetStencilCompareMask)(batch->state->cmdbuf, VK_STENCIL_FACE_BACK_BIT, dsa_state->hw_state.stencil_back.compareMask);
667bf215546Sopenharmony_ci         } else {
668bf215546Sopenharmony_ci            VKCTX(CmdSetStencilWriteMask)(batch->state->cmdbuf, VK_STENCIL_FACE_FRONT_AND_BACK, dsa_state->hw_state.stencil_front.writeMask);
669bf215546Sopenharmony_ci            VKCTX(CmdSetStencilCompareMask)(batch->state->cmdbuf, VK_STENCIL_FACE_FRONT_AND_BACK, dsa_state->hw_state.stencil_front.compareMask);
670bf215546Sopenharmony_ci         }
671bf215546Sopenharmony_ci      } else {
672bf215546Sopenharmony_ci         VKCTX(CmdSetStencilWriteMask)(batch->state->cmdbuf, VK_STENCIL_FACE_FRONT_AND_BACK, dsa_state->hw_state.stencil_front.writeMask);
673bf215546Sopenharmony_ci         VKCTX(CmdSetStencilCompareMask)(batch->state->cmdbuf, VK_STENCIL_FACE_FRONT_AND_BACK, dsa_state->hw_state.stencil_front.compareMask);
674bf215546Sopenharmony_ci      }
675bf215546Sopenharmony_ci   }
676bf215546Sopenharmony_ci   ctx->dsa_state_changed = false;
677bf215546Sopenharmony_ci
678bf215546Sopenharmony_ci   bool rast_state_changed = ctx->rast_state_changed;
679bf215546Sopenharmony_ci   if (DYNAMIC_STATE != ZINK_NO_DYNAMIC_STATE && (BATCH_CHANGED || rast_state_changed)) {
680bf215546Sopenharmony_ci      VKCTX(CmdSetFrontFaceEXT)(batch->state->cmdbuf, (VkFrontFace)ctx->gfx_pipeline_state.dyn_state1.front_face);
681bf215546Sopenharmony_ci      VKCTX(CmdSetCullModeEXT)(batch->state->cmdbuf, ctx->gfx_pipeline_state.dyn_state1.cull_mode);
682bf215546Sopenharmony_ci   }
683bf215546Sopenharmony_ci   if ((BATCH_CHANGED || rast_state_changed) &&
684bf215546Sopenharmony_ci       screen->info.have_EXT_line_rasterization && rast_state->base.line_stipple_enable)
685bf215546Sopenharmony_ci      VKCTX(CmdSetLineStippleEXT)(batch->state->cmdbuf, rast_state->base.line_stipple_factor, rast_state->base.line_stipple_pattern);
686bf215546Sopenharmony_ci
687bf215546Sopenharmony_ci   if (BATCH_CHANGED || ctx->rast_state_changed) {
688bf215546Sopenharmony_ci      enum pipe_prim_type reduced_prim = ctx->last_vertex_stage->reduced_prim;
689bf215546Sopenharmony_ci      if (reduced_prim == PIPE_PRIM_MAX)
690bf215546Sopenharmony_ci         reduced_prim = u_reduced_prim(mode);
691bf215546Sopenharmony_ci
692bf215546Sopenharmony_ci      bool depth_bias = false;
693bf215546Sopenharmony_ci      switch (reduced_prim) {
694bf215546Sopenharmony_ci      case PIPE_PRIM_POINTS:
695bf215546Sopenharmony_ci         depth_bias = rast_state->offset_point;
696bf215546Sopenharmony_ci         break;
697bf215546Sopenharmony_ci
698bf215546Sopenharmony_ci      case PIPE_PRIM_LINES:
699bf215546Sopenharmony_ci         depth_bias = rast_state->offset_line;
700bf215546Sopenharmony_ci         break;
701bf215546Sopenharmony_ci
702bf215546Sopenharmony_ci      case PIPE_PRIM_TRIANGLES:
703bf215546Sopenharmony_ci         depth_bias = rast_state->offset_tri;
704bf215546Sopenharmony_ci         break;
705bf215546Sopenharmony_ci
706bf215546Sopenharmony_ci      default:
707bf215546Sopenharmony_ci         unreachable("unexpected reduced prim");
708bf215546Sopenharmony_ci      }
709bf215546Sopenharmony_ci
710bf215546Sopenharmony_ci      VKCTX(CmdSetLineWidth)(batch->state->cmdbuf, rast_state->line_width);
711bf215546Sopenharmony_ci      if (depth_bias) {
712bf215546Sopenharmony_ci         if (rast_state->base.offset_units_unscaled) {
713bf215546Sopenharmony_ci            VKCTX(CmdSetDepthBias)(batch->state->cmdbuf, rast_state->offset_units * ctx->depth_bias_scale_factor, rast_state->offset_clamp, rast_state->offset_scale);
714bf215546Sopenharmony_ci         } else {
715bf215546Sopenharmony_ci            VKCTX(CmdSetDepthBias)(batch->state->cmdbuf, rast_state->offset_units, rast_state->offset_clamp, rast_state->offset_scale);
716bf215546Sopenharmony_ci         }
717bf215546Sopenharmony_ci      } else {
718bf215546Sopenharmony_ci         VKCTX(CmdSetDepthBias)(batch->state->cmdbuf, 0.0f, 0.0f, 0.0f);
719bf215546Sopenharmony_ci      }
720bf215546Sopenharmony_ci   }
721bf215546Sopenharmony_ci   ctx->rast_state_changed = false;
722bf215546Sopenharmony_ci
723bf215546Sopenharmony_ci   if (DYNAMIC_STATE != ZINK_NO_DYNAMIC_STATE) {
724bf215546Sopenharmony_ci      if (ctx->sample_locations_changed) {
725bf215546Sopenharmony_ci         VkSampleLocationsInfoEXT loc;
726bf215546Sopenharmony_ci         zink_init_vk_sample_locations(ctx, &loc);
727bf215546Sopenharmony_ci         VKCTX(CmdSetSampleLocationsEXT)(batch->state->cmdbuf, &loc);
728bf215546Sopenharmony_ci      }
729bf215546Sopenharmony_ci      ctx->sample_locations_changed = false;
730bf215546Sopenharmony_ci   }
731bf215546Sopenharmony_ci
732bf215546Sopenharmony_ci   if ((BATCH_CHANGED || ctx->blend_state_changed) &&
733bf215546Sopenharmony_ci       ctx->gfx_pipeline_state.blend_state->need_blend_constants) {
734bf215546Sopenharmony_ci      VKCTX(CmdSetBlendConstants)(batch->state->cmdbuf, ctx->blend_constants);
735bf215546Sopenharmony_ci   }
736bf215546Sopenharmony_ci   ctx->blend_state_changed = false;
737bf215546Sopenharmony_ci
738bf215546Sopenharmony_ci   if (DRAW_STATE)
739bf215546Sopenharmony_ci      zink_bind_vertex_state(batch, ctx, vstate, partial_velem_mask);
740bf215546Sopenharmony_ci   else if (BATCH_CHANGED || ctx->vertex_buffers_dirty) {
741bf215546Sopenharmony_ci      if (DYNAMIC_STATE == ZINK_DYNAMIC_VERTEX_INPUT || ctx->gfx_pipeline_state.uses_dynamic_stride)
742bf215546Sopenharmony_ci         zink_bind_vertex_buffers<DYNAMIC_STATE>(batch, ctx);
743bf215546Sopenharmony_ci      else
744bf215546Sopenharmony_ci         zink_bind_vertex_buffers<ZINK_NO_DYNAMIC_STATE>(batch, ctx);
745bf215546Sopenharmony_ci   }
746bf215546Sopenharmony_ci
747bf215546Sopenharmony_ci   if (BATCH_CHANGED) {
748bf215546Sopenharmony_ci      ctx->pipeline_changed[0] = false;
749bf215546Sopenharmony_ci      zink_select_draw_vbo(ctx);
750bf215546Sopenharmony_ci   }
751bf215546Sopenharmony_ci
752bf215546Sopenharmony_ci   if (DYNAMIC_STATE != ZINK_NO_DYNAMIC_STATE && (BATCH_CHANGED || mode_changed)) {
753bf215546Sopenharmony_ci      VKCTX(CmdSetPrimitiveTopologyEXT)(batch->state->cmdbuf, zink_primitive_topology(mode));
754bf215546Sopenharmony_ci   }
755bf215546Sopenharmony_ci
756bf215546Sopenharmony_ci   if (DYNAMIC_STATE >= ZINK_DYNAMIC_STATE2 && (BATCH_CHANGED || ctx->primitive_restart != dinfo->primitive_restart)) {
757bf215546Sopenharmony_ci      VKCTX(CmdSetPrimitiveRestartEnableEXT)(batch->state->cmdbuf, dinfo->primitive_restart);
758bf215546Sopenharmony_ci      ctx->primitive_restart = dinfo->primitive_restart;
759bf215546Sopenharmony_ci   }
760bf215546Sopenharmony_ci
761bf215546Sopenharmony_ci   if (DYNAMIC_STATE >= ZINK_DYNAMIC_STATE2 && (BATCH_CHANGED || ctx->rasterizer_discard_changed)) {
762bf215546Sopenharmony_ci      VKCTX(CmdSetRasterizerDiscardEnableEXT)(batch->state->cmdbuf, ctx->gfx_pipeline_state.dyn_state2.rasterizer_discard);
763bf215546Sopenharmony_ci      ctx->rasterizer_discard_changed = false;
764bf215546Sopenharmony_ci   }
765bf215546Sopenharmony_ci
766bf215546Sopenharmony_ci   if (zink_program_has_descriptors(&ctx->curr_program->base))
767bf215546Sopenharmony_ci      screen->descriptors_update(ctx, false);
768bf215546Sopenharmony_ci
769bf215546Sopenharmony_ci   if (ctx->di.any_bindless_dirty &&
770bf215546Sopenharmony_ci       /* some apps (d3dretrace) call MakeTextureHandleResidentARB randomly */
771bf215546Sopenharmony_ci       zink_program_has_descriptors(&ctx->curr_program->base) &&
772bf215546Sopenharmony_ci       ctx->curr_program->base.dd->bindless)
773bf215546Sopenharmony_ci      zink_descriptors_update_bindless(ctx);
774bf215546Sopenharmony_ci
775bf215546Sopenharmony_ci   if (reads_basevertex) {
776bf215546Sopenharmony_ci      unsigned draw_mode_is_indexed = index_size > 0;
777bf215546Sopenharmony_ci      VKCTX(CmdPushConstants)(batch->state->cmdbuf, ctx->curr_program->base.layout, VK_SHADER_STAGE_VERTEX_BIT,
778bf215546Sopenharmony_ci                         offsetof(struct zink_gfx_push_constant, draw_mode_is_indexed), sizeof(unsigned),
779bf215546Sopenharmony_ci                         &draw_mode_is_indexed);
780bf215546Sopenharmony_ci   }
781bf215546Sopenharmony_ci   if (ctx->curr_program->shaders[PIPE_SHADER_TESS_CTRL] && ctx->curr_program->shaders[PIPE_SHADER_TESS_CTRL]->is_generated) {
782bf215546Sopenharmony_ci      VKCTX(CmdPushConstants)(batch->state->cmdbuf, ctx->curr_program->base.layout, VK_SHADER_STAGE_TESSELLATION_CONTROL_BIT,
783bf215546Sopenharmony_ci                         offsetof(struct zink_gfx_push_constant, default_inner_level), sizeof(float) * 6,
784bf215546Sopenharmony_ci                         &ctx->tess_levels[0]);
785bf215546Sopenharmony_ci   }
786bf215546Sopenharmony_ci
787bf215546Sopenharmony_ci   if (have_streamout) {
788bf215546Sopenharmony_ci      for (unsigned i = 0; i < ctx->num_so_targets; i++) {
789bf215546Sopenharmony_ci         struct zink_so_target *t = zink_so_target(ctx->so_targets[i]);
790bf215546Sopenharmony_ci         counter_buffers[i] = VK_NULL_HANDLE;
791bf215546Sopenharmony_ci         if (t) {
792bf215546Sopenharmony_ci            struct zink_resource *res = zink_resource(t->counter_buffer);
793bf215546Sopenharmony_ci            t->stride = ctx->last_vertex_stage->sinfo.so_info.stride[i] * sizeof(uint32_t);
794bf215546Sopenharmony_ci            zink_batch_reference_resource_rw(batch, res, true);
795bf215546Sopenharmony_ci            res->obj->unordered_read = res->obj->unordered_write = false;
796bf215546Sopenharmony_ci            if (t->counter_buffer_valid) {
797bf215546Sopenharmony_ci               counter_buffers[i] = res->obj->buffer;
798bf215546Sopenharmony_ci               counter_buffer_offsets[i] = t->counter_buffer_offset;
799bf215546Sopenharmony_ci            }
800bf215546Sopenharmony_ci         }
801bf215546Sopenharmony_ci      }
802bf215546Sopenharmony_ci      VKCTX(CmdBeginTransformFeedbackEXT)(batch->state->cmdbuf, 0, ctx->num_so_targets, counter_buffers, counter_buffer_offsets);
803bf215546Sopenharmony_ci   }
804bf215546Sopenharmony_ci
805bf215546Sopenharmony_ci   bool needs_drawid = reads_drawid && zink_get_last_vertex_key(ctx)->push_drawid;
806bf215546Sopenharmony_ci   work_count += num_draws;
807bf215546Sopenharmony_ci   if (index_size > 0) {
808bf215546Sopenharmony_ci      if (dindirect && dindirect->buffer) {
809bf215546Sopenharmony_ci         assert(num_draws == 1);
810bf215546Sopenharmony_ci         if (needs_drawid)
811bf215546Sopenharmony_ci            update_drawid(ctx, drawid_offset);
812bf215546Sopenharmony_ci         struct zink_resource *indirect = zink_resource(dindirect->buffer);
813bf215546Sopenharmony_ci         zink_batch_reference_resource_rw(batch, indirect, false);
814bf215546Sopenharmony_ci         if (dindirect->indirect_draw_count) {
815bf215546Sopenharmony_ci             struct zink_resource *indirect_draw_count = zink_resource(dindirect->indirect_draw_count);
816bf215546Sopenharmony_ci             zink_batch_reference_resource_rw(batch, indirect_draw_count, false);
817bf215546Sopenharmony_ci             VKCTX(CmdDrawIndexedIndirectCount)(batch->state->cmdbuf, indirect->obj->buffer, dindirect->offset,
818bf215546Sopenharmony_ci                                                indirect_draw_count->obj->buffer, dindirect->indirect_draw_count_offset,
819bf215546Sopenharmony_ci                                                dindirect->draw_count, dindirect->stride);
820bf215546Sopenharmony_ci         } else
821bf215546Sopenharmony_ci            VKCTX(CmdDrawIndexedIndirect)(batch->state->cmdbuf, indirect->obj->buffer, dindirect->offset, dindirect->draw_count, dindirect->stride);
822bf215546Sopenharmony_ci      } else {
823bf215546Sopenharmony_ci         if (need_index_buffer_unref)
824bf215546Sopenharmony_ci            draw_indexed_need_index_buffer_unref(ctx, dinfo, draws, num_draws, drawid_offset, needs_drawid);
825bf215546Sopenharmony_ci         else
826bf215546Sopenharmony_ci            draw_indexed<HAS_MULTIDRAW>(ctx, dinfo, draws, num_draws, drawid_offset, needs_drawid);
827bf215546Sopenharmony_ci      }
828bf215546Sopenharmony_ci   } else {
829bf215546Sopenharmony_ci      if (so_target && screen->info.tf_props.transformFeedbackDraw) {
830bf215546Sopenharmony_ci         /* GTF-GL46.gtf40.GL3Tests.transform_feedback2.transform_feedback2_api attempts a bogus xfb
831bf215546Sopenharmony_ci          * draw using a streamout target that has no data
832bf215546Sopenharmony_ci          * to avoid hanging the gpu, reject any such draws
833bf215546Sopenharmony_ci          */
834bf215546Sopenharmony_ci         if (so_target->counter_buffer_valid) {
835bf215546Sopenharmony_ci            if (needs_drawid)
836bf215546Sopenharmony_ci               update_drawid(ctx, drawid_offset);
837bf215546Sopenharmony_ci            zink_batch_reference_resource_rw(batch, zink_resource(so_target->base.buffer), false);
838bf215546Sopenharmony_ci            zink_batch_reference_resource_rw(batch, zink_resource(so_target->counter_buffer), true);
839bf215546Sopenharmony_ci            VKCTX(CmdDrawIndirectByteCountEXT)(batch->state->cmdbuf, dinfo->instance_count, dinfo->start_instance,
840bf215546Sopenharmony_ci                                          zink_resource(so_target->counter_buffer)->obj->buffer, so_target->counter_buffer_offset, 0,
841bf215546Sopenharmony_ci                                          MIN2(so_target->stride, screen->info.tf_props.maxTransformFeedbackBufferDataStride));
842bf215546Sopenharmony_ci         }
843bf215546Sopenharmony_ci      } else if (dindirect && dindirect->buffer) {
844bf215546Sopenharmony_ci         assert(num_draws == 1);
845bf215546Sopenharmony_ci         if (needs_drawid)
846bf215546Sopenharmony_ci            update_drawid(ctx, drawid_offset);
847bf215546Sopenharmony_ci         struct zink_resource *indirect = zink_resource(dindirect->buffer);
848bf215546Sopenharmony_ci         zink_batch_reference_resource_rw(batch, indirect, false);
849bf215546Sopenharmony_ci         if (dindirect->indirect_draw_count) {
850bf215546Sopenharmony_ci             struct zink_resource *indirect_draw_count = zink_resource(dindirect->indirect_draw_count);
851bf215546Sopenharmony_ci             zink_batch_reference_resource_rw(batch, indirect_draw_count, false);
852bf215546Sopenharmony_ci             VKCTX(CmdDrawIndirectCount)(batch->state->cmdbuf, indirect->obj->buffer, dindirect->offset,
853bf215546Sopenharmony_ci                                           indirect_draw_count->obj->buffer, dindirect->indirect_draw_count_offset,
854bf215546Sopenharmony_ci                                           dindirect->draw_count, dindirect->stride);
855bf215546Sopenharmony_ci         } else
856bf215546Sopenharmony_ci            VKCTX(CmdDrawIndirect)(batch->state->cmdbuf, indirect->obj->buffer, dindirect->offset, dindirect->draw_count, dindirect->stride);
857bf215546Sopenharmony_ci      } else {
858bf215546Sopenharmony_ci         draw<HAS_MULTIDRAW>(ctx, dinfo, draws, num_draws, drawid_offset, needs_drawid);
859bf215546Sopenharmony_ci      }
860bf215546Sopenharmony_ci   }
861bf215546Sopenharmony_ci
862bf215546Sopenharmony_ci   if (have_streamout) {
863bf215546Sopenharmony_ci      for (unsigned i = 0; i < ctx->num_so_targets; i++) {
864bf215546Sopenharmony_ci         struct zink_so_target *t = zink_so_target(ctx->so_targets[i]);
865bf215546Sopenharmony_ci         if (t) {
866bf215546Sopenharmony_ci            counter_buffers[i] = zink_resource(t->counter_buffer)->obj->buffer;
867bf215546Sopenharmony_ci            counter_buffer_offsets[i] = t->counter_buffer_offset;
868bf215546Sopenharmony_ci            t->counter_buffer_valid = true;
869bf215546Sopenharmony_ci         }
870bf215546Sopenharmony_ci      }
871bf215546Sopenharmony_ci      VKCTX(CmdEndTransformFeedbackEXT)(batch->state->cmdbuf, 0, ctx->num_so_targets, counter_buffers, counter_buffer_offsets);
872bf215546Sopenharmony_ci   }
873bf215546Sopenharmony_ci   batch->has_work = true;
874bf215546Sopenharmony_ci   batch->last_was_compute = false;
875bf215546Sopenharmony_ci   ctx->batch.work_count = work_count;
876bf215546Sopenharmony_ci   /* flush if there's >100k draws */
877bf215546Sopenharmony_ci   if (unlikely(work_count >= 30000) || ctx->oom_flush)
878bf215546Sopenharmony_ci      pctx->flush(pctx, NULL, 0);
879bf215546Sopenharmony_ci}
880bf215546Sopenharmony_ci
881bf215546Sopenharmony_citemplate <zink_multidraw HAS_MULTIDRAW, zink_dynamic_state DYNAMIC_STATE, bool BATCH_CHANGED>
882bf215546Sopenharmony_cistatic void
883bf215546Sopenharmony_cizink_draw_vbo(struct pipe_context *pctx,
884bf215546Sopenharmony_ci              const struct pipe_draw_info *info,
885bf215546Sopenharmony_ci              unsigned drawid_offset,
886bf215546Sopenharmony_ci              const struct pipe_draw_indirect_info *indirect,
887bf215546Sopenharmony_ci              const struct pipe_draw_start_count_bias *draws,
888bf215546Sopenharmony_ci              unsigned num_draws)
889bf215546Sopenharmony_ci{
890bf215546Sopenharmony_ci   zink_draw<HAS_MULTIDRAW, DYNAMIC_STATE, BATCH_CHANGED, false>(pctx, info, drawid_offset, indirect, draws, num_draws, NULL, 0);
891bf215546Sopenharmony_ci}
892bf215546Sopenharmony_ci
893bf215546Sopenharmony_citemplate <zink_multidraw HAS_MULTIDRAW, zink_dynamic_state DYNAMIC_STATE, bool BATCH_CHANGED>
894bf215546Sopenharmony_cistatic void
895bf215546Sopenharmony_cizink_draw_vertex_state(struct pipe_context *pctx,
896bf215546Sopenharmony_ci                       struct pipe_vertex_state *vstate,
897bf215546Sopenharmony_ci                       uint32_t partial_velem_mask,
898bf215546Sopenharmony_ci                       struct pipe_draw_vertex_state_info info,
899bf215546Sopenharmony_ci                       const struct pipe_draw_start_count_bias *draws,
900bf215546Sopenharmony_ci                       unsigned num_draws)
901bf215546Sopenharmony_ci{
902bf215546Sopenharmony_ci   struct pipe_draw_info dinfo = {};
903bf215546Sopenharmony_ci
904bf215546Sopenharmony_ci   dinfo.mode = info.mode;
905bf215546Sopenharmony_ci   dinfo.index_size = 4;
906bf215546Sopenharmony_ci   dinfo.instance_count = 1;
907bf215546Sopenharmony_ci   dinfo.index.resource = vstate->input.indexbuf;
908bf215546Sopenharmony_ci   struct zink_context *ctx = zink_context(pctx);
909bf215546Sopenharmony_ci   struct zink_resource *res = zink_resource(vstate->input.vbuffer.buffer.resource);
910bf215546Sopenharmony_ci   zink_resource_buffer_barrier(ctx, res, VK_ACCESS_VERTEX_ATTRIBUTE_READ_BIT,
911bf215546Sopenharmony_ci                                VK_PIPELINE_STAGE_VERTEX_INPUT_BIT);
912bf215546Sopenharmony_ci   res->obj->unordered_read = false;
913bf215546Sopenharmony_ci   struct zink_vertex_elements_hw_state *hw_state = ctx->gfx_pipeline_state.element_state;
914bf215546Sopenharmony_ci   ctx->gfx_pipeline_state.element_state = &((struct zink_vertex_state*)vstate)->velems.hw_state;
915bf215546Sopenharmony_ci
916bf215546Sopenharmony_ci   zink_draw<HAS_MULTIDRAW, DYNAMIC_STATE, BATCH_CHANGED, true>(pctx, &dinfo, 0, NULL, draws, num_draws, vstate, partial_velem_mask);
917bf215546Sopenharmony_ci   ctx->gfx_pipeline_state.element_state = hw_state;
918bf215546Sopenharmony_ci
919bf215546Sopenharmony_ci   if (info.take_vertex_state_ownership)
920bf215546Sopenharmony_ci      pipe_vertex_state_reference(&vstate, NULL);
921bf215546Sopenharmony_ci}
922bf215546Sopenharmony_ci
923bf215546Sopenharmony_citemplate <bool BATCH_CHANGED>
924bf215546Sopenharmony_cistatic void
925bf215546Sopenharmony_cizink_launch_grid(struct pipe_context *pctx, const struct pipe_grid_info *info)
926bf215546Sopenharmony_ci{
927bf215546Sopenharmony_ci   struct zink_context *ctx = zink_context(pctx);
928bf215546Sopenharmony_ci   struct zink_screen *screen = zink_screen(pctx->screen);
929bf215546Sopenharmony_ci   struct zink_batch *batch = &ctx->batch;
930bf215546Sopenharmony_ci
931bf215546Sopenharmony_ci   if (ctx->render_condition_active)
932bf215546Sopenharmony_ci      zink_start_conditional_render(ctx);
933bf215546Sopenharmony_ci
934bf215546Sopenharmony_ci   if (info->indirect) {
935bf215546Sopenharmony_ci      /*
936bf215546Sopenharmony_ci         VK_ACCESS_INDIRECT_COMMAND_READ_BIT specifies read access to indirect command data read as
937bf215546Sopenharmony_ci         part of an indirect build, trace, drawing or dispatching command. Such access occurs in the
938bf215546Sopenharmony_ci         VK_PIPELINE_STAGE_DRAW_INDIRECT_BIT pipeline stage.
939bf215546Sopenharmony_ci
940bf215546Sopenharmony_ci         - Chapter 7. Synchronization and Cache Control
941bf215546Sopenharmony_ci       */
942bf215546Sopenharmony_ci      check_buffer_barrier(ctx, info->indirect, VK_ACCESS_INDIRECT_COMMAND_READ_BIT, VK_PIPELINE_STAGE_DRAW_INDIRECT_BIT);
943bf215546Sopenharmony_ci   }
944bf215546Sopenharmony_ci
945bf215546Sopenharmony_ci   update_barriers(ctx, true, NULL, info->indirect, NULL);
946bf215546Sopenharmony_ci   if (ctx->memory_barrier)
947bf215546Sopenharmony_ci      zink_flush_memory_barrier(ctx, true);
948bf215546Sopenharmony_ci
949bf215546Sopenharmony_ci   if (unlikely(zink_debug & ZINK_DEBUG_SYNC)) {
950bf215546Sopenharmony_ci      zink_batch_no_rp(ctx);
951bf215546Sopenharmony_ci      VkMemoryBarrier mb;
952bf215546Sopenharmony_ci      mb.sType = VK_STRUCTURE_TYPE_MEMORY_BARRIER;
953bf215546Sopenharmony_ci      mb.pNext = NULL;
954bf215546Sopenharmony_ci      mb.srcAccessMask = VK_ACCESS_MEMORY_WRITE_BIT;
955bf215546Sopenharmony_ci      mb.dstAccessMask = VK_ACCESS_MEMORY_READ_BIT;
956bf215546Sopenharmony_ci      VKSCR(CmdPipelineBarrier)(ctx->batch.state->cmdbuf,
957bf215546Sopenharmony_ci                                VK_PIPELINE_STAGE_ALL_COMMANDS_BIT,
958bf215546Sopenharmony_ci                                VK_PIPELINE_STAGE_ALL_COMMANDS_BIT,
959bf215546Sopenharmony_ci                                0, 1, &mb, 0, NULL, 0, NULL);
960bf215546Sopenharmony_ci   }
961bf215546Sopenharmony_ci
962bf215546Sopenharmony_ci   if (zink_program_has_descriptors(&ctx->curr_compute->base))
963bf215546Sopenharmony_ci      screen->descriptors_update(ctx, true);
964bf215546Sopenharmony_ci   if (ctx->di.any_bindless_dirty && ctx->curr_compute->base.dd->bindless)
965bf215546Sopenharmony_ci      zink_descriptors_update_bindless(ctx);
966bf215546Sopenharmony_ci
967bf215546Sopenharmony_ci   zink_program_update_compute_pipeline_state(ctx, ctx->curr_compute, info->block);
968bf215546Sopenharmony_ci   VkPipeline prev_pipeline = ctx->compute_pipeline_state.pipeline;
969bf215546Sopenharmony_ci
970bf215546Sopenharmony_ci   if (BATCH_CHANGED) {
971bf215546Sopenharmony_ci      zink_update_descriptor_refs(ctx, true);
972bf215546Sopenharmony_ci      zink_batch_reference_program(&ctx->batch, &ctx->curr_compute->base);
973bf215546Sopenharmony_ci   }
974bf215546Sopenharmony_ci   if (ctx->dirty_shader_stages & BITFIELD_BIT(PIPE_SHADER_COMPUTE)) {
975bf215546Sopenharmony_ci      /* update inlinable constants */
976bf215546Sopenharmony_ci      zink_update_compute_program(ctx);
977bf215546Sopenharmony_ci      ctx->dirty_shader_stages &= ~BITFIELD_BIT(PIPE_SHADER_COMPUTE);
978bf215546Sopenharmony_ci   }
979bf215546Sopenharmony_ci
980bf215546Sopenharmony_ci   VkPipeline pipeline = zink_get_compute_pipeline(screen, ctx->curr_compute,
981bf215546Sopenharmony_ci                                               &ctx->compute_pipeline_state);
982bf215546Sopenharmony_ci
983bf215546Sopenharmony_ci   if (prev_pipeline != pipeline || BATCH_CHANGED)
984bf215546Sopenharmony_ci      VKCTX(CmdBindPipeline)(batch->state->cmdbuf, VK_PIPELINE_BIND_POINT_COMPUTE, pipeline);
985bf215546Sopenharmony_ci   if (BATCH_CHANGED) {
986bf215546Sopenharmony_ci      ctx->pipeline_changed[1] = false;
987bf215546Sopenharmony_ci      zink_select_launch_grid(ctx);
988bf215546Sopenharmony_ci   }
989bf215546Sopenharmony_ci
990bf215546Sopenharmony_ci   if (BITSET_TEST(ctx->compute_stage->nir->info.system_values_read, SYSTEM_VALUE_WORK_DIM))
991bf215546Sopenharmony_ci      VKCTX(CmdPushConstants)(batch->state->cmdbuf, ctx->curr_compute->base.layout, VK_SHADER_STAGE_COMPUTE_BIT,
992bf215546Sopenharmony_ci                         offsetof(struct zink_cs_push_constant, work_dim), sizeof(uint32_t),
993bf215546Sopenharmony_ci                         &info->work_dim);
994bf215546Sopenharmony_ci
995bf215546Sopenharmony_ci   batch->work_count++;
996bf215546Sopenharmony_ci   zink_batch_no_rp(ctx);
997bf215546Sopenharmony_ci   if (info->indirect) {
998bf215546Sopenharmony_ci      VKCTX(CmdDispatchIndirect)(batch->state->cmdbuf, zink_resource(info->indirect)->obj->buffer, info->indirect_offset);
999bf215546Sopenharmony_ci      zink_batch_reference_resource_rw(batch, zink_resource(info->indirect), false);
1000bf215546Sopenharmony_ci   } else
1001bf215546Sopenharmony_ci      VKCTX(CmdDispatch)(batch->state->cmdbuf, info->grid[0], info->grid[1], info->grid[2]);
1002bf215546Sopenharmony_ci   batch->has_work = true;
1003bf215546Sopenharmony_ci   batch->last_was_compute = true;
1004bf215546Sopenharmony_ci   /* flush if there's >100k computes */
1005bf215546Sopenharmony_ci   if (unlikely(ctx->batch.work_count >= 30000) || ctx->oom_flush)
1006bf215546Sopenharmony_ci      pctx->flush(pctx, NULL, 0);
1007bf215546Sopenharmony_ci}
1008bf215546Sopenharmony_ci
1009bf215546Sopenharmony_citemplate <zink_multidraw HAS_MULTIDRAW, zink_dynamic_state DYNAMIC_STATE, bool BATCH_CHANGED>
1010bf215546Sopenharmony_cistatic void
1011bf215546Sopenharmony_ciinit_batch_changed_functions(struct zink_context *ctx, pipe_draw_vbo_func draw_vbo_array[2][4][2], pipe_draw_vertex_state_func draw_state_array[2][4][2])
1012bf215546Sopenharmony_ci{
1013bf215546Sopenharmony_ci   draw_vbo_array[HAS_MULTIDRAW][DYNAMIC_STATE][BATCH_CHANGED] = zink_draw_vbo<HAS_MULTIDRAW, DYNAMIC_STATE, BATCH_CHANGED>;
1014bf215546Sopenharmony_ci   draw_state_array[HAS_MULTIDRAW][DYNAMIC_STATE][BATCH_CHANGED] = zink_draw_vertex_state<HAS_MULTIDRAW, DYNAMIC_STATE, BATCH_CHANGED>;
1015bf215546Sopenharmony_ci}
1016bf215546Sopenharmony_ci
1017bf215546Sopenharmony_citemplate <zink_multidraw HAS_MULTIDRAW, zink_dynamic_state DYNAMIC_STATE>
1018bf215546Sopenharmony_cistatic void
1019bf215546Sopenharmony_ciinit_dynamic_state_functions(struct zink_context *ctx, pipe_draw_vbo_func draw_vbo_array[2][4][2], pipe_draw_vertex_state_func draw_state_array[2][4][2])
1020bf215546Sopenharmony_ci{
1021bf215546Sopenharmony_ci   init_batch_changed_functions<HAS_MULTIDRAW, DYNAMIC_STATE, false>(ctx, draw_vbo_array, draw_state_array);
1022bf215546Sopenharmony_ci   init_batch_changed_functions<HAS_MULTIDRAW, DYNAMIC_STATE, true>(ctx, draw_vbo_array, draw_state_array);
1023bf215546Sopenharmony_ci}
1024bf215546Sopenharmony_ci
1025bf215546Sopenharmony_citemplate <zink_multidraw HAS_MULTIDRAW>
1026bf215546Sopenharmony_cistatic void
1027bf215546Sopenharmony_ciinit_multidraw_functions(struct zink_context *ctx, pipe_draw_vbo_func draw_vbo_array[2][4][2], pipe_draw_vertex_state_func draw_state_array[2][4][2])
1028bf215546Sopenharmony_ci{
1029bf215546Sopenharmony_ci   init_dynamic_state_functions<HAS_MULTIDRAW, ZINK_NO_DYNAMIC_STATE>(ctx, draw_vbo_array, draw_state_array);
1030bf215546Sopenharmony_ci   init_dynamic_state_functions<HAS_MULTIDRAW, ZINK_DYNAMIC_STATE>(ctx, draw_vbo_array, draw_state_array);
1031bf215546Sopenharmony_ci   init_dynamic_state_functions<HAS_MULTIDRAW, ZINK_DYNAMIC_STATE2>(ctx, draw_vbo_array, draw_state_array);
1032bf215546Sopenharmony_ci   init_dynamic_state_functions<HAS_MULTIDRAW, ZINK_DYNAMIC_VERTEX_INPUT>(ctx, draw_vbo_array, draw_state_array);
1033bf215546Sopenharmony_ci}
1034bf215546Sopenharmony_ci
1035bf215546Sopenharmony_cistatic void
1036bf215546Sopenharmony_ciinit_all_draw_functions(struct zink_context *ctx, pipe_draw_vbo_func draw_vbo_array[2][4][2], pipe_draw_vertex_state_func draw_state_array[2][4][2])
1037bf215546Sopenharmony_ci{
1038bf215546Sopenharmony_ci   init_multidraw_functions<ZINK_NO_MULTIDRAW>(ctx, draw_vbo_array, draw_state_array);
1039bf215546Sopenharmony_ci   init_multidraw_functions<ZINK_MULTIDRAW>(ctx, draw_vbo_array, draw_state_array);
1040bf215546Sopenharmony_ci}
1041bf215546Sopenharmony_ci
1042bf215546Sopenharmony_citemplate <bool BATCH_CHANGED>
1043bf215546Sopenharmony_cistatic void
1044bf215546Sopenharmony_ciinit_grid_batch_changed_functions(struct zink_context *ctx)
1045bf215546Sopenharmony_ci{
1046bf215546Sopenharmony_ci   ctx->launch_grid[BATCH_CHANGED] = zink_launch_grid<BATCH_CHANGED>;
1047bf215546Sopenharmony_ci}
1048bf215546Sopenharmony_ci
1049bf215546Sopenharmony_cistatic void
1050bf215546Sopenharmony_ciinit_all_grid_functions(struct zink_context *ctx)
1051bf215546Sopenharmony_ci{
1052bf215546Sopenharmony_ci   init_grid_batch_changed_functions<false>(ctx);
1053bf215546Sopenharmony_ci   init_grid_batch_changed_functions<true>(ctx);
1054bf215546Sopenharmony_ci}
1055bf215546Sopenharmony_ci
1056bf215546Sopenharmony_cistatic void
1057bf215546Sopenharmony_cizink_invalid_draw_vbo(struct pipe_context *pipe,
1058bf215546Sopenharmony_ci                      const struct pipe_draw_info *dinfo,
1059bf215546Sopenharmony_ci                      unsigned drawid_offset,
1060bf215546Sopenharmony_ci                      const struct pipe_draw_indirect_info *dindirect,
1061bf215546Sopenharmony_ci                      const struct pipe_draw_start_count_bias *draws,
1062bf215546Sopenharmony_ci                      unsigned num_draws)
1063bf215546Sopenharmony_ci{
1064bf215546Sopenharmony_ci   unreachable("vertex shader not bound");
1065bf215546Sopenharmony_ci}
1066bf215546Sopenharmony_ci
1067bf215546Sopenharmony_cistatic void
1068bf215546Sopenharmony_cizink_invalid_draw_vertex_state(struct pipe_context *pipe,
1069bf215546Sopenharmony_ci                               struct pipe_vertex_state *vstate,
1070bf215546Sopenharmony_ci                               uint32_t partial_velem_mask,
1071bf215546Sopenharmony_ci                               struct pipe_draw_vertex_state_info info,
1072bf215546Sopenharmony_ci                               const struct pipe_draw_start_count_bias *draws,
1073bf215546Sopenharmony_ci                               unsigned num_draws)
1074bf215546Sopenharmony_ci{
1075bf215546Sopenharmony_ci   unreachable("vertex shader not bound");
1076bf215546Sopenharmony_ci}
1077bf215546Sopenharmony_ci
1078bf215546Sopenharmony_cistatic void
1079bf215546Sopenharmony_cizink_invalid_launch_grid(struct pipe_context *pctx, const struct pipe_grid_info *info)
1080bf215546Sopenharmony_ci{
1081bf215546Sopenharmony_ci   unreachable("compute shader not bound");
1082bf215546Sopenharmony_ci}
1083bf215546Sopenharmony_ci
1084bf215546Sopenharmony_citemplate <unsigned STAGE_MASK>
1085bf215546Sopenharmony_cistatic uint32_t
1086bf215546Sopenharmony_cihash_gfx_program(const void *key)
1087bf215546Sopenharmony_ci{
1088bf215546Sopenharmony_ci   const struct zink_shader **shaders = (const struct zink_shader**)key;
1089bf215546Sopenharmony_ci   uint32_t base_hash = shaders[PIPE_SHADER_VERTEX]->hash ^ shaders[PIPE_SHADER_FRAGMENT]->hash;
1090bf215546Sopenharmony_ci   if (STAGE_MASK == 0) //VS+FS
1091bf215546Sopenharmony_ci      return base_hash;
1092bf215546Sopenharmony_ci   if (STAGE_MASK == 1) //VS+GS+FS
1093bf215546Sopenharmony_ci      return base_hash ^ shaders[PIPE_SHADER_GEOMETRY]->hash;
1094bf215546Sopenharmony_ci   /*VS+TCS+FS isn't a thing */
1095bf215546Sopenharmony_ci   /*VS+TCS+GS+FS isn't a thing */
1096bf215546Sopenharmony_ci   if (STAGE_MASK == 4) //VS+TES+FS
1097bf215546Sopenharmony_ci      return base_hash ^ shaders[PIPE_SHADER_TESS_EVAL]->hash;
1098bf215546Sopenharmony_ci   if (STAGE_MASK == 5) //VS+TES+GS+FS
1099bf215546Sopenharmony_ci      return base_hash ^ shaders[PIPE_SHADER_GEOMETRY]->hash ^ shaders[PIPE_SHADER_TESS_EVAL]->hash;
1100bf215546Sopenharmony_ci   if (STAGE_MASK == 6) //VS+TCS+TES+FS
1101bf215546Sopenharmony_ci      return base_hash ^ shaders[PIPE_SHADER_TESS_CTRL]->hash ^ shaders[PIPE_SHADER_TESS_EVAL]->hash;
1102bf215546Sopenharmony_ci
1103bf215546Sopenharmony_ci   /* all stages */
1104bf215546Sopenharmony_ci   return base_hash ^ shaders[PIPE_SHADER_GEOMETRY]->hash ^ shaders[PIPE_SHADER_TESS_CTRL]->hash ^ shaders[PIPE_SHADER_TESS_EVAL]->hash;
1105bf215546Sopenharmony_ci}
1106bf215546Sopenharmony_ci
1107bf215546Sopenharmony_citemplate <unsigned STAGE_MASK>
1108bf215546Sopenharmony_cistatic bool
1109bf215546Sopenharmony_ciequals_gfx_program(const void *a, const void *b)
1110bf215546Sopenharmony_ci{
1111bf215546Sopenharmony_ci   const void **sa = (const void**)a;
1112bf215546Sopenharmony_ci   const void **sb = (const void**)b;
1113bf215546Sopenharmony_ci   if (STAGE_MASK == 0) //VS+FS
1114bf215546Sopenharmony_ci      return !memcmp(a, b, sizeof(void*) * 2);
1115bf215546Sopenharmony_ci   if (STAGE_MASK == 1) //VS+GS+FS
1116bf215546Sopenharmony_ci      return !memcmp(a, b, sizeof(void*) * 3);
1117bf215546Sopenharmony_ci   /*VS+TCS+FS isn't a thing */
1118bf215546Sopenharmony_ci   /*VS+TCS+GS+FS isn't a thing */
1119bf215546Sopenharmony_ci   if (STAGE_MASK == 4) //VS+TES+FS
1120bf215546Sopenharmony_ci      return sa[PIPE_SHADER_TESS_EVAL] == sb[PIPE_SHADER_TESS_EVAL] && !memcmp(a, b, sizeof(void*) * 2);
1121bf215546Sopenharmony_ci   if (STAGE_MASK == 5) //VS+TES+GS+FS
1122bf215546Sopenharmony_ci      return sa[PIPE_SHADER_TESS_EVAL] == sb[PIPE_SHADER_TESS_EVAL] && !memcmp(a, b, sizeof(void*) * 3);
1123bf215546Sopenharmony_ci   if (STAGE_MASK == 6) //VS+TCS+TES+FS
1124bf215546Sopenharmony_ci      return !memcmp(&sa[PIPE_SHADER_TESS_CTRL], &sb[PIPE_SHADER_TESS_CTRL], sizeof(void*) * 2) &&
1125bf215546Sopenharmony_ci             !memcmp(a, b, sizeof(void*) * 2);
1126bf215546Sopenharmony_ci
1127bf215546Sopenharmony_ci   /* all stages */
1128bf215546Sopenharmony_ci   return !memcmp(a, b, sizeof(void*) * ZINK_SHADER_COUNT);
1129bf215546Sopenharmony_ci}
1130bf215546Sopenharmony_ci
1131bf215546Sopenharmony_ciextern "C"
1132bf215546Sopenharmony_civoid
1133bf215546Sopenharmony_cizink_init_draw_functions(struct zink_context *ctx, struct zink_screen *screen)
1134bf215546Sopenharmony_ci{
1135bf215546Sopenharmony_ci   pipe_draw_vbo_func draw_vbo_array[2][4] //multidraw, zink_dynamic_state
1136bf215546Sopenharmony_ci                                    [2];   //batch changed
1137bf215546Sopenharmony_ci   pipe_draw_vertex_state_func draw_state_array[2][4] //multidraw, zink_dynamic_state
1138bf215546Sopenharmony_ci                                               [2];   //batch changed
1139bf215546Sopenharmony_ci   zink_dynamic_state dynamic;
1140bf215546Sopenharmony_ci   if (screen->info.have_EXT_extended_dynamic_state) {
1141bf215546Sopenharmony_ci      if (screen->info.have_EXT_extended_dynamic_state2) {
1142bf215546Sopenharmony_ci         if (screen->info.have_EXT_vertex_input_dynamic_state)
1143bf215546Sopenharmony_ci            dynamic = ZINK_DYNAMIC_VERTEX_INPUT;
1144bf215546Sopenharmony_ci         else
1145bf215546Sopenharmony_ci            dynamic = ZINK_DYNAMIC_STATE2;
1146bf215546Sopenharmony_ci      } else {
1147bf215546Sopenharmony_ci         dynamic = ZINK_DYNAMIC_STATE;
1148bf215546Sopenharmony_ci      }
1149bf215546Sopenharmony_ci   } else {
1150bf215546Sopenharmony_ci      dynamic = ZINK_NO_DYNAMIC_STATE;
1151bf215546Sopenharmony_ci   }
1152bf215546Sopenharmony_ci   init_all_draw_functions(ctx, draw_vbo_array, draw_state_array);
1153bf215546Sopenharmony_ci   memcpy(ctx->draw_vbo, &draw_vbo_array[screen->info.have_EXT_multi_draw]
1154bf215546Sopenharmony_ci                                        [dynamic],
1155bf215546Sopenharmony_ci                                        sizeof(ctx->draw_vbo));
1156bf215546Sopenharmony_ci   memcpy(ctx->draw_state, &draw_state_array[screen->info.have_EXT_multi_draw]
1157bf215546Sopenharmony_ci                                          [dynamic],
1158bf215546Sopenharmony_ci                                          sizeof(ctx->draw_state));
1159bf215546Sopenharmony_ci
1160bf215546Sopenharmony_ci   /* Bind a fake draw_vbo, so that draw_vbo isn't NULL, which would skip
1161bf215546Sopenharmony_ci    * initialization of callbacks in upper layers (such as u_threaded_context).
1162bf215546Sopenharmony_ci    */
1163bf215546Sopenharmony_ci   ctx->base.draw_vbo = zink_invalid_draw_vbo;
1164bf215546Sopenharmony_ci   ctx->base.draw_vertex_state = zink_invalid_draw_vertex_state;
1165bf215546Sopenharmony_ci
1166bf215546Sopenharmony_ci   _mesa_hash_table_init(&ctx->program_cache[0], ctx, hash_gfx_program<0>, equals_gfx_program<0>);
1167bf215546Sopenharmony_ci   _mesa_hash_table_init(&ctx->program_cache[1], ctx, hash_gfx_program<1>, equals_gfx_program<1>);
1168bf215546Sopenharmony_ci   _mesa_hash_table_init(&ctx->program_cache[2], ctx, hash_gfx_program<2>, equals_gfx_program<2>);
1169bf215546Sopenharmony_ci   _mesa_hash_table_init(&ctx->program_cache[3], ctx, hash_gfx_program<3>, equals_gfx_program<3>);
1170bf215546Sopenharmony_ci   _mesa_hash_table_init(&ctx->program_cache[4], ctx, hash_gfx_program<4>, equals_gfx_program<4>);
1171bf215546Sopenharmony_ci   _mesa_hash_table_init(&ctx->program_cache[5], ctx, hash_gfx_program<5>, equals_gfx_program<5>);
1172bf215546Sopenharmony_ci   _mesa_hash_table_init(&ctx->program_cache[6], ctx, hash_gfx_program<6>, equals_gfx_program<6>);
1173bf215546Sopenharmony_ci   _mesa_hash_table_init(&ctx->program_cache[7], ctx, hash_gfx_program<7>, equals_gfx_program<7>);
1174bf215546Sopenharmony_ci}
1175bf215546Sopenharmony_ci
1176bf215546Sopenharmony_civoid
1177bf215546Sopenharmony_cizink_init_grid_functions(struct zink_context *ctx)
1178bf215546Sopenharmony_ci{
1179bf215546Sopenharmony_ci   init_all_grid_functions(ctx);
1180bf215546Sopenharmony_ci   /* Bind a fake launch_grid, so that draw_vbo isn't NULL, which would skip
1181bf215546Sopenharmony_ci    * initialization of callbacks in upper layers (such as u_threaded_context).
1182bf215546Sopenharmony_ci    */
1183bf215546Sopenharmony_ci   ctx->base.launch_grid = zink_invalid_launch_grid;
1184bf215546Sopenharmony_ci}
1185