1bf215546Sopenharmony_ci/*
2bf215546Sopenharmony_ci * Copyright 2018 Collabora Ltd.
3bf215546Sopenharmony_ci *
4bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a
5bf215546Sopenharmony_ci * copy of this software and associated documentation files (the "Software"),
6bf215546Sopenharmony_ci * to deal in the Software without restriction, including without limitation
7bf215546Sopenharmony_ci * on the rights to use, copy, modify, merge, publish, distribute, sub
8bf215546Sopenharmony_ci * license, and/or sell copies of the Software, and to permit persons to whom
9bf215546Sopenharmony_ci * the Software is furnished to do so, subject to the following conditions:
10bf215546Sopenharmony_ci *
11bf215546Sopenharmony_ci * The above copyright notice and this permission notice (including the next
12bf215546Sopenharmony_ci * paragraph) shall be included in all copies or substantial portions of the
13bf215546Sopenharmony_ci * Software.
14bf215546Sopenharmony_ci *
15bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16bf215546Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17bf215546Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
18bf215546Sopenharmony_ci * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
19bf215546Sopenharmony_ci * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
20bf215546Sopenharmony_ci * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
21bf215546Sopenharmony_ci * USE OR OTHER DEALINGS IN THE SOFTWARE.
22bf215546Sopenharmony_ci */
23bf215546Sopenharmony_ci
24bf215546Sopenharmony_ci#ifndef ZINK_COMPILER_H
25bf215546Sopenharmony_ci#define ZINK_COMPILER_H
26bf215546Sopenharmony_ci
27bf215546Sopenharmony_ci#include "pipe/p_defines.h"
28bf215546Sopenharmony_ci#include "pipe/p_state.h"
29bf215546Sopenharmony_ci
30bf215546Sopenharmony_ci#include "compiler/nir/nir.h"
31bf215546Sopenharmony_ci#include "compiler/shader_info.h"
32bf215546Sopenharmony_ci#include "util/u_live_shader_cache.h"
33bf215546Sopenharmony_ci
34bf215546Sopenharmony_ci#include <vulkan/vulkan.h>
35bf215546Sopenharmony_ci#include "zink_descriptors.h"
36bf215546Sopenharmony_ci
37bf215546Sopenharmony_ci#define ZINK_WORKGROUP_SIZE_X 1
38bf215546Sopenharmony_ci#define ZINK_WORKGROUP_SIZE_Y 2
39bf215546Sopenharmony_ci#define ZINK_WORKGROUP_SIZE_Z 3
40bf215546Sopenharmony_ci
41bf215546Sopenharmony_ci/* stop inlining shaders if they have >limit ssa vals after inlining:
42bf215546Sopenharmony_ci * recompile time isn't worth the inline
43bf215546Sopenharmony_ci */
44bf215546Sopenharmony_ci#define ZINK_ALWAYS_INLINE_LIMIT 1500
45bf215546Sopenharmony_ci
46bf215546Sopenharmony_cistruct pipe_screen;
47bf215546Sopenharmony_cistruct zink_context;
48bf215546Sopenharmony_cistruct zink_screen;
49bf215546Sopenharmony_cistruct zink_shader_key;
50bf215546Sopenharmony_cistruct zink_shader_module;
51bf215546Sopenharmony_cistruct zink_gfx_program;
52bf215546Sopenharmony_cistruct spirv_shader;
53bf215546Sopenharmony_ci
54bf215546Sopenharmony_cistruct nir_shader_compiler_options;
55bf215546Sopenharmony_cistruct nir_shader;
56bf215546Sopenharmony_ci
57bf215546Sopenharmony_cistruct set;
58bf215546Sopenharmony_ci
59bf215546Sopenharmony_cistruct tgsi_token;
60bf215546Sopenharmony_cistruct zink_shader_info {
61bf215546Sopenharmony_ci   struct pipe_stream_output_info so_info;
62bf215546Sopenharmony_ci   unsigned so_info_slots[PIPE_MAX_SO_OUTPUTS];
63bf215546Sopenharmony_ci   uint32_t so_propagate; //left shifted by 32
64bf215546Sopenharmony_ci   bool last_vertex;
65bf215546Sopenharmony_ci   bool have_xfb;
66bf215546Sopenharmony_ci   bool have_sparse;
67bf215546Sopenharmony_ci   bool have_vulkan_memory_model;
68bf215546Sopenharmony_ci};
69bf215546Sopenharmony_ci
70bf215546Sopenharmony_ci
71bf215546Sopenharmony_ciconst void *
72bf215546Sopenharmony_cizink_get_compiler_options(struct pipe_screen *screen,
73bf215546Sopenharmony_ci                          enum pipe_shader_ir ir,
74bf215546Sopenharmony_ci                          enum pipe_shader_type shader);
75bf215546Sopenharmony_ci
76bf215546Sopenharmony_cistruct nir_shader *
77bf215546Sopenharmony_cizink_tgsi_to_nir(struct pipe_screen *screen, const struct tgsi_token *tokens);
78bf215546Sopenharmony_ci
79bf215546Sopenharmony_cistruct zink_shader {
80bf215546Sopenharmony_ci   struct util_live_shader base;
81bf215546Sopenharmony_ci   uint32_t hash;
82bf215546Sopenharmony_ci   struct nir_shader *nir;
83bf215546Sopenharmony_ci   enum pipe_prim_type reduced_prim; // PIPE_PRIM_MAX for vs
84bf215546Sopenharmony_ci
85bf215546Sopenharmony_ci   struct zink_shader_info sinfo;
86bf215546Sopenharmony_ci
87bf215546Sopenharmony_ci   struct {
88bf215546Sopenharmony_ci      int index;
89bf215546Sopenharmony_ci      int binding;
90bf215546Sopenharmony_ci      VkDescriptorType type;
91bf215546Sopenharmony_ci      unsigned char size;
92bf215546Sopenharmony_ci   } bindings[ZINK_DESCRIPTOR_TYPES][ZINK_MAX_DESCRIPTORS_PER_TYPE];
93bf215546Sopenharmony_ci   size_t num_bindings[ZINK_DESCRIPTOR_TYPES];
94bf215546Sopenharmony_ci   unsigned num_texel_buffers;
95bf215546Sopenharmony_ci   uint32_t ubos_used; // bitfield of which ubo indices are used
96bf215546Sopenharmony_ci   uint32_t ssbos_used; // bitfield of which ssbo indices are used
97bf215546Sopenharmony_ci   bool bindless;
98bf215546Sopenharmony_ci   bool can_inline;
99bf215546Sopenharmony_ci   struct spirv_shader *spirv;
100bf215546Sopenharmony_ci
101bf215546Sopenharmony_ci   simple_mtx_t lock;
102bf215546Sopenharmony_ci   struct set *programs;
103bf215546Sopenharmony_ci
104bf215546Sopenharmony_ci   union {
105bf215546Sopenharmony_ci      struct zink_shader *generated; // a generated shader that this shader "owns"
106bf215546Sopenharmony_ci      bool is_generated; // if this is a driver-created shader (e.g., tcs)
107bf215546Sopenharmony_ci      nir_variable *fbfetch; //for fs output
108bf215546Sopenharmony_ci   };
109bf215546Sopenharmony_ci};
110bf215546Sopenharmony_ci
111bf215546Sopenharmony_civoid
112bf215546Sopenharmony_cizink_screen_init_compiler(struct zink_screen *screen);
113bf215546Sopenharmony_civoid
114bf215546Sopenharmony_cizink_compiler_assign_io(nir_shader *producer, nir_shader *consumer);
115bf215546Sopenharmony_ciVkShaderModule
116bf215546Sopenharmony_cizink_shader_compile(struct zink_screen *screen, struct zink_shader *zs, nir_shader *nir, const struct zink_shader_key *key);
117bf215546Sopenharmony_ciVkShaderModule
118bf215546Sopenharmony_cizink_shader_spirv_compile(struct zink_screen *screen, struct zink_shader *zs, struct spirv_shader *spirv);
119bf215546Sopenharmony_cistruct zink_shader *
120bf215546Sopenharmony_cizink_shader_create(struct zink_screen *screen, struct nir_shader *nir,
121bf215546Sopenharmony_ci                 const struct pipe_stream_output_info *so_info);
122bf215546Sopenharmony_ci
123bf215546Sopenharmony_cichar *
124bf215546Sopenharmony_cizink_shader_finalize(struct pipe_screen *pscreen, void *nirptr);
125bf215546Sopenharmony_ci
126bf215546Sopenharmony_civoid
127bf215546Sopenharmony_cizink_shader_free(struct zink_context *ctx, struct zink_shader *shader);
128bf215546Sopenharmony_ci
129bf215546Sopenharmony_ciVkShaderModule
130bf215546Sopenharmony_cizink_shader_tcs_compile(struct zink_screen *screen, struct zink_shader *zs, unsigned patch_vertices);
131bf215546Sopenharmony_cistruct zink_shader *
132bf215546Sopenharmony_cizink_shader_tcs_create(struct zink_screen *screen, struct zink_shader *vs, unsigned vertices_per_patch);
133bf215546Sopenharmony_ci
134bf215546Sopenharmony_cistatic inline bool
135bf215546Sopenharmony_cizink_shader_descriptor_is_buffer(struct zink_shader *zs, enum zink_descriptor_type type, unsigned i)
136bf215546Sopenharmony_ci{
137bf215546Sopenharmony_ci   return zs->bindings[type][i].type == VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER ||
138bf215546Sopenharmony_ci          zs->bindings[type][i].type == VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER;
139bf215546Sopenharmony_ci}
140bf215546Sopenharmony_ci
141bf215546Sopenharmony_ci#endif
142