1b8e80941Smrg/*
2b8e80941Smrg * Copyright (C) 2014 Rob Clark <robclark@freedesktop.org>
3b8e80941Smrg *
4b8e80941Smrg * Permission is hereby granted, free of charge, to any person obtaining a
5b8e80941Smrg * copy of this software and associated documentation files (the "Software"),
6b8e80941Smrg * to deal in the Software without restriction, including without limitation
7b8e80941Smrg * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8b8e80941Smrg * and/or sell copies of the Software, and to permit persons to whom the
9b8e80941Smrg * Software is furnished to do so, subject to the following conditions:
10b8e80941Smrg *
11b8e80941Smrg * The above copyright notice and this permission notice (including the next
12b8e80941Smrg * paragraph) shall be included in all copies or substantial portions of the
13b8e80941Smrg * Software.
14b8e80941Smrg *
15b8e80941Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16b8e80941Smrg * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17b8e80941Smrg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18b8e80941Smrg * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19b8e80941Smrg * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20b8e80941Smrg * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21b8e80941Smrg * SOFTWARE.
22b8e80941Smrg *
23b8e80941Smrg * Authors:
24b8e80941Smrg *    Rob Clark <robclark@freedesktop.org>
25b8e80941Smrg */
26b8e80941Smrg
27b8e80941Smrg#ifndef IR3_SHADER_H_
28b8e80941Smrg#define IR3_SHADER_H_
29b8e80941Smrg
30b8e80941Smrg#include <stdio.h>
31b8e80941Smrg
32b8e80941Smrg#include "compiler/shader_enums.h"
33b8e80941Smrg#include "compiler/nir/nir.h"
34b8e80941Smrg#include "util/bitscan.h"
35b8e80941Smrg
36b8e80941Smrg#include "ir3.h"
37b8e80941Smrg
38b8e80941Smrgstruct glsl_type;
39b8e80941Smrg
40b8e80941Smrg/* driver param indices: */
41b8e80941Smrgenum ir3_driver_param {
42b8e80941Smrg	/* compute shader driver params: */
43b8e80941Smrg	IR3_DP_NUM_WORK_GROUPS_X = 0,
44b8e80941Smrg	IR3_DP_NUM_WORK_GROUPS_Y = 1,
45b8e80941Smrg	IR3_DP_NUM_WORK_GROUPS_Z = 2,
46b8e80941Smrg	IR3_DP_LOCAL_GROUP_SIZE_X = 4,
47b8e80941Smrg	IR3_DP_LOCAL_GROUP_SIZE_Y = 5,
48b8e80941Smrg	IR3_DP_LOCAL_GROUP_SIZE_Z = 6,
49b8e80941Smrg	/* NOTE: gl_NumWorkGroups should be vec4 aligned because
50b8e80941Smrg	 * glDispatchComputeIndirect() needs to load these from
51b8e80941Smrg	 * the info->indirect buffer.  Keep that in mind when/if
52b8e80941Smrg	 * adding any addition CS driver params.
53b8e80941Smrg	 */
54b8e80941Smrg	IR3_DP_CS_COUNT   = 8,   /* must be aligned to vec4 */
55b8e80941Smrg
56b8e80941Smrg	/* vertex shader driver params: */
57b8e80941Smrg	IR3_DP_VTXID_BASE = 0,
58b8e80941Smrg	IR3_DP_VTXCNT_MAX = 1,
59b8e80941Smrg	/* user-clip-plane components, up to 8x vec4's: */
60b8e80941Smrg	IR3_DP_UCP0_X     = 4,
61b8e80941Smrg	/* .... */
62b8e80941Smrg	IR3_DP_UCP7_W     = 35,
63b8e80941Smrg	IR3_DP_VS_COUNT   = 36   /* must be aligned to vec4 */
64b8e80941Smrg};
65b8e80941Smrg
66b8e80941Smrg#define IR3_MAX_SHADER_BUFFERS   32
67b8e80941Smrg#define IR3_MAX_SHADER_IMAGES    32
68b8e80941Smrg#define IR3_MAX_SO_BUFFERS        4
69b8e80941Smrg#define IR3_MAX_SO_OUTPUTS       64
70b8e80941Smrg#define IR3_MAX_CONSTANT_BUFFERS 32
71b8e80941Smrg
72b8e80941Smrg
73b8e80941Smrg/**
74b8e80941Smrg * For consts needed to pass internal values to shader which may or may not
75b8e80941Smrg * be required, rather than allocating worst-case const space, we scan the
76b8e80941Smrg * shader and allocate consts as-needed:
77b8e80941Smrg *
78b8e80941Smrg *   + SSBO sizes: only needed if shader has a get_buffer_size intrinsic
79b8e80941Smrg *     for a given SSBO
80b8e80941Smrg *
81b8e80941Smrg *   + Image dimensions: needed to calculate pixel offset, but only for
82b8e80941Smrg *     images that have a image_store intrinsic
83b8e80941Smrg */
84b8e80941Smrgstruct ir3_driver_const_layout {
85b8e80941Smrg	struct {
86b8e80941Smrg		uint32_t mask;  /* bitmask of SSBOs that have get_buffer_size */
87b8e80941Smrg		uint32_t count; /* number of consts allocated */
88b8e80941Smrg		/* one const allocated per SSBO which has get_buffer_size,
89b8e80941Smrg		 * ssbo_sizes.off[ssbo_id] is offset from start of ssbo_sizes
90b8e80941Smrg		 * consts:
91b8e80941Smrg		 */
92b8e80941Smrg		uint32_t off[IR3_MAX_SHADER_BUFFERS];
93b8e80941Smrg	} ssbo_size;
94b8e80941Smrg
95b8e80941Smrg	struct {
96b8e80941Smrg		uint32_t mask;  /* bitmask of images that have image_store */
97b8e80941Smrg		uint32_t count; /* number of consts allocated */
98b8e80941Smrg		/* three const allocated per image which has image_store:
99b8e80941Smrg		 *  + cpp         (bytes per pixel)
100b8e80941Smrg		 *  + pitch       (y pitch)
101b8e80941Smrg		 *  + array_pitch (z pitch)
102b8e80941Smrg		 */
103b8e80941Smrg		uint32_t off[IR3_MAX_SHADER_IMAGES];
104b8e80941Smrg	} image_dims;
105b8e80941Smrg};
106b8e80941Smrg
107b8e80941Smrg/**
108b8e80941Smrg * A single output for vertex transform feedback.
109b8e80941Smrg */
110b8e80941Smrgstruct ir3_stream_output {
111b8e80941Smrg	unsigned register_index:6;  /**< 0 to 63 (OUT index) */
112b8e80941Smrg	unsigned start_component:2; /** 0 to 3 */
113b8e80941Smrg	unsigned num_components:3;  /** 1 to 4 */
114b8e80941Smrg	unsigned output_buffer:3;   /**< 0 to PIPE_MAX_SO_BUFFERS */
115b8e80941Smrg	unsigned dst_offset:16;     /**< offset into the buffer in dwords */
116b8e80941Smrg	unsigned stream:2;          /**< 0 to 3 */
117b8e80941Smrg};
118b8e80941Smrg
119b8e80941Smrg/**
120b8e80941Smrg * Stream output for vertex transform feedback.
121b8e80941Smrg */
122b8e80941Smrgstruct ir3_stream_output_info {
123b8e80941Smrg	unsigned num_outputs;
124b8e80941Smrg	/** stride for an entire vertex for each buffer in dwords */
125b8e80941Smrg	uint16_t stride[IR3_MAX_SO_BUFFERS];
126b8e80941Smrg
127b8e80941Smrg	/**
128b8e80941Smrg	 * Array of stream outputs, in the order they are to be written in.
129b8e80941Smrg	 * Selected components are tightly packed into the output buffer.
130b8e80941Smrg	 */
131b8e80941Smrg	struct ir3_stream_output output[IR3_MAX_SO_OUTPUTS];
132b8e80941Smrg};
133b8e80941Smrg
134b8e80941Smrg/* Configuration key used to identify a shader variant.. different
135b8e80941Smrg * shader variants can be used to implement features not supported
136b8e80941Smrg * in hw (two sided color), binning-pass vertex shader, etc.
137b8e80941Smrg */
138b8e80941Smrgstruct ir3_shader_key {
139b8e80941Smrg	union {
140b8e80941Smrg		struct {
141b8e80941Smrg			/*
142b8e80941Smrg			 * Combined Vertex/Fragment shader parameters:
143b8e80941Smrg			 */
144b8e80941Smrg			unsigned ucp_enables : 8;
145b8e80941Smrg
146b8e80941Smrg			/* do we need to check {v,f}saturate_{s,t,r}? */
147b8e80941Smrg			unsigned has_per_samp : 1;
148b8e80941Smrg
149b8e80941Smrg			/*
150b8e80941Smrg			 * Vertex shader variant parameters:
151b8e80941Smrg			 */
152b8e80941Smrg			unsigned vclamp_color : 1;
153b8e80941Smrg
154b8e80941Smrg			/*
155b8e80941Smrg			 * Fragment shader variant parameters:
156b8e80941Smrg			 */
157b8e80941Smrg			unsigned sample_shading : 1;
158b8e80941Smrg			unsigned msaa           : 1;
159b8e80941Smrg			unsigned color_two_side : 1;
160b8e80941Smrg			unsigned half_precision : 1;
161b8e80941Smrg			/* used when shader needs to handle flat varyings (a4xx)
162b8e80941Smrg			 * for front/back color inputs to frag shader:
163b8e80941Smrg			 */
164b8e80941Smrg			unsigned rasterflat : 1;
165b8e80941Smrg			unsigned fclamp_color : 1;
166b8e80941Smrg		};
167b8e80941Smrg		uint32_t global;
168b8e80941Smrg	};
169b8e80941Smrg
170b8e80941Smrg	/* bitmask of sampler which needs coords clamped for vertex
171b8e80941Smrg	 * shader:
172b8e80941Smrg	 */
173b8e80941Smrg	uint16_t vsaturate_s, vsaturate_t, vsaturate_r;
174b8e80941Smrg
175b8e80941Smrg	/* bitmask of sampler which needs coords clamped for frag
176b8e80941Smrg	 * shader:
177b8e80941Smrg	 */
178b8e80941Smrg	uint16_t fsaturate_s, fsaturate_t, fsaturate_r;
179b8e80941Smrg
180b8e80941Smrg	/* bitmask of ms shifts */
181b8e80941Smrg	uint32_t vsamples, fsamples;
182b8e80941Smrg
183b8e80941Smrg	/* bitmask of samplers which need astc srgb workaround: */
184b8e80941Smrg	uint16_t vastc_srgb, fastc_srgb;
185b8e80941Smrg};
186b8e80941Smrg
187b8e80941Smrgstatic inline bool
188b8e80941Smrgir3_shader_key_equal(struct ir3_shader_key *a, struct ir3_shader_key *b)
189b8e80941Smrg{
190b8e80941Smrg	/* slow-path if we need to check {v,f}saturate_{s,t,r} */
191b8e80941Smrg	if (a->has_per_samp || b->has_per_samp)
192b8e80941Smrg		return memcmp(a, b, sizeof(struct ir3_shader_key)) == 0;
193b8e80941Smrg	return a->global == b->global;
194b8e80941Smrg}
195b8e80941Smrg
196b8e80941Smrg/* will the two keys produce different lowering for a fragment shader? */
197b8e80941Smrgstatic inline bool
198b8e80941Smrgir3_shader_key_changes_fs(struct ir3_shader_key *key, struct ir3_shader_key *last_key)
199b8e80941Smrg{
200b8e80941Smrg	if (last_key->has_per_samp || key->has_per_samp) {
201b8e80941Smrg		if ((last_key->fsaturate_s != key->fsaturate_s) ||
202b8e80941Smrg				(last_key->fsaturate_t != key->fsaturate_t) ||
203b8e80941Smrg				(last_key->fsaturate_r != key->fsaturate_r) ||
204b8e80941Smrg				(last_key->fsamples != key->fsamples) ||
205b8e80941Smrg				(last_key->fastc_srgb != key->fastc_srgb))
206b8e80941Smrg			return true;
207b8e80941Smrg	}
208b8e80941Smrg
209b8e80941Smrg	if (last_key->fclamp_color != key->fclamp_color)
210b8e80941Smrg		return true;
211b8e80941Smrg
212b8e80941Smrg	if (last_key->color_two_side != key->color_two_side)
213b8e80941Smrg		return true;
214b8e80941Smrg
215b8e80941Smrg	if (last_key->half_precision != key->half_precision)
216b8e80941Smrg		return true;
217b8e80941Smrg
218b8e80941Smrg	if (last_key->rasterflat != key->rasterflat)
219b8e80941Smrg		return true;
220b8e80941Smrg
221b8e80941Smrg	if (last_key->ucp_enables != key->ucp_enables)
222b8e80941Smrg		return true;
223b8e80941Smrg
224b8e80941Smrg	return false;
225b8e80941Smrg}
226b8e80941Smrg
227b8e80941Smrg/* will the two keys produce different lowering for a vertex shader? */
228b8e80941Smrgstatic inline bool
229b8e80941Smrgir3_shader_key_changes_vs(struct ir3_shader_key *key, struct ir3_shader_key *last_key)
230b8e80941Smrg{
231b8e80941Smrg	if (last_key->has_per_samp || key->has_per_samp) {
232b8e80941Smrg		if ((last_key->vsaturate_s != key->vsaturate_s) ||
233b8e80941Smrg				(last_key->vsaturate_t != key->vsaturate_t) ||
234b8e80941Smrg				(last_key->vsaturate_r != key->vsaturate_r) ||
235b8e80941Smrg				(last_key->vsamples != key->vsamples) ||
236b8e80941Smrg				(last_key->vastc_srgb != key->vastc_srgb))
237b8e80941Smrg			return true;
238b8e80941Smrg	}
239b8e80941Smrg
240b8e80941Smrg	if (last_key->vclamp_color != key->vclamp_color)
241b8e80941Smrg		return true;
242b8e80941Smrg
243b8e80941Smrg	if (last_key->ucp_enables != key->ucp_enables)
244b8e80941Smrg		return true;
245b8e80941Smrg
246b8e80941Smrg	return false;
247b8e80941Smrg}
248b8e80941Smrg
249b8e80941Smrg/* clears shader-key flags which don't apply to the given shader
250b8e80941Smrg * stage
251b8e80941Smrg */
252b8e80941Smrgstatic inline void
253b8e80941Smrgir3_normalize_key(struct ir3_shader_key *key, gl_shader_stage type)
254b8e80941Smrg{
255b8e80941Smrg	switch (type) {
256b8e80941Smrg	case MESA_SHADER_FRAGMENT:
257b8e80941Smrg		if (key->has_per_samp) {
258b8e80941Smrg			key->vsaturate_s = 0;
259b8e80941Smrg			key->vsaturate_t = 0;
260b8e80941Smrg			key->vsaturate_r = 0;
261b8e80941Smrg			key->vastc_srgb = 0;
262b8e80941Smrg			key->vsamples = 0;
263b8e80941Smrg		}
264b8e80941Smrg		break;
265b8e80941Smrg	case MESA_SHADER_VERTEX:
266b8e80941Smrg		key->color_two_side = false;
267b8e80941Smrg		key->half_precision = false;
268b8e80941Smrg		key->rasterflat = false;
269b8e80941Smrg		if (key->has_per_samp) {
270b8e80941Smrg			key->fsaturate_s = 0;
271b8e80941Smrg			key->fsaturate_t = 0;
272b8e80941Smrg			key->fsaturate_r = 0;
273b8e80941Smrg			key->fastc_srgb = 0;
274b8e80941Smrg			key->fsamples = 0;
275b8e80941Smrg		}
276b8e80941Smrg		break;
277b8e80941Smrg	default:
278b8e80941Smrg		/* TODO */
279b8e80941Smrg		break;
280b8e80941Smrg	}
281b8e80941Smrg}
282b8e80941Smrg
283b8e80941Smrg/**
284b8e80941Smrg * On a4xx+a5xx, Images share state with textures and SSBOs:
285b8e80941Smrg *
286b8e80941Smrg *   + Uses texture (cat5) state/instruction (isam) to read
287b8e80941Smrg *   + Uses SSBO state and instructions (cat6) to write and for atomics
288b8e80941Smrg *
289b8e80941Smrg * Starting with a6xx, Images and SSBOs are basically the same thing,
290b8e80941Smrg * with texture state and isam also used for SSBO reads.
291b8e80941Smrg *
292b8e80941Smrg * On top of that, gallium makes the SSBO (shader_buffers) state semi
293b8e80941Smrg * sparse, with the first half of the state space used for atomic
294b8e80941Smrg * counters lowered to atomic buffers.  We could ignore this, but I
295b8e80941Smrg * don't think we could *really* handle the case of a single shader
296b8e80941Smrg * that used the max # of textures + images + SSBOs.  And once we are
297b8e80941Smrg * offsetting images by num_ssbos (or visa versa) to map them into
298b8e80941Smrg * the same hardware state, the hardware state has become coupled to
299b8e80941Smrg * the shader state, so at this point we might as well just use a
300b8e80941Smrg * mapping table to remap things from image/SSBO idx to hw idx.
301b8e80941Smrg *
302b8e80941Smrg * To make things less (more?) confusing, for the hw "SSBO" state
303b8e80941Smrg * (since it is really both SSBO and Image) I'll use the name "IBO"
304b8e80941Smrg */
305b8e80941Smrgstruct ir3_ibo_mapping {
306b8e80941Smrg#define IBO_INVALID 0xff
307b8e80941Smrg	/* Maps logical SSBO state to hw state: */
308b8e80941Smrg	uint8_t ssbo_to_ibo[IR3_MAX_SHADER_BUFFERS];
309b8e80941Smrg	uint8_t ssbo_to_tex[IR3_MAX_SHADER_BUFFERS];
310b8e80941Smrg
311b8e80941Smrg	/* Maps logical Image state to hw state: */
312b8e80941Smrg	uint8_t image_to_ibo[IR3_MAX_SHADER_IMAGES];
313b8e80941Smrg	uint8_t image_to_tex[IR3_MAX_SHADER_IMAGES];
314b8e80941Smrg
315b8e80941Smrg	/* Maps hw state back to logical SSBO or Image state:
316b8e80941Smrg	 *
317b8e80941Smrg	 * note IBO_SSBO ORd into values to indicate that the
318b8e80941Smrg	 * hw slot is used for SSBO state vs Image state.
319b8e80941Smrg	 */
320b8e80941Smrg#define IBO_SSBO    0x80
321b8e80941Smrg	uint8_t ibo_to_image[32];
322b8e80941Smrg	uint8_t tex_to_image[32];
323b8e80941Smrg
324b8e80941Smrg	uint8_t num_ibo;
325b8e80941Smrg	uint8_t num_tex;    /* including real textures */
326b8e80941Smrg	uint8_t tex_base;   /* the number of real textures, ie. image/ssbo start here */
327b8e80941Smrg};
328b8e80941Smrg
329b8e80941Smrgstruct ir3_shader_variant {
330b8e80941Smrg	struct fd_bo *bo;
331b8e80941Smrg
332b8e80941Smrg	/* variant id (for debug) */
333b8e80941Smrg	uint32_t id;
334b8e80941Smrg
335b8e80941Smrg	struct ir3_shader_key key;
336b8e80941Smrg
337b8e80941Smrg	/* vertex shaders can have an extra version for hwbinning pass,
338b8e80941Smrg	 * which is pointed to by so->binning:
339b8e80941Smrg	 */
340b8e80941Smrg	bool binning_pass;
341b8e80941Smrg	struct ir3_shader_variant *binning;
342b8e80941Smrg
343b8e80941Smrg	struct ir3_driver_const_layout const_layout;
344b8e80941Smrg	struct ir3_info info;
345b8e80941Smrg	struct ir3 *ir;
346b8e80941Smrg
347b8e80941Smrg	/* Levels of nesting of flow control:
348b8e80941Smrg	 */
349b8e80941Smrg	unsigned branchstack;
350b8e80941Smrg
351b8e80941Smrg	unsigned max_sun;
352b8e80941Smrg
353b8e80941Smrg	/* the instructions length is in units of instruction groups
354b8e80941Smrg	 * (4 instructions for a3xx, 16 instructions for a4xx.. each
355b8e80941Smrg	 * instruction is 2 dwords):
356b8e80941Smrg	 */
357b8e80941Smrg	unsigned instrlen;
358b8e80941Smrg
359b8e80941Smrg	/* the constants length is in units of vec4's, and is the sum of
360b8e80941Smrg	 * the uniforms and the built-in compiler constants
361b8e80941Smrg	 */
362b8e80941Smrg	unsigned constlen;
363b8e80941Smrg
364b8e80941Smrg	/* number of uniforms (in vec4), not including built-in compiler
365b8e80941Smrg	 * constants, etc.
366b8e80941Smrg	 */
367b8e80941Smrg	unsigned num_uniforms;
368b8e80941Smrg
369b8e80941Smrg	unsigned num_ubos;
370b8e80941Smrg
371b8e80941Smrg	/* About Linkage:
372b8e80941Smrg	 *   + Let the frag shader determine the position/compmask for the
373b8e80941Smrg	 *     varyings, since it is the place where we know if the varying
374b8e80941Smrg	 *     is actually used, and if so, which components are used.  So
375b8e80941Smrg	 *     what the hw calls "outloc" is taken from the "inloc" of the
376b8e80941Smrg	 *     frag shader.
377b8e80941Smrg	 *   + From the vert shader, we only need the output regid
378b8e80941Smrg	 */
379b8e80941Smrg
380b8e80941Smrg	bool frag_coord, frag_face, color0_mrt;
381b8e80941Smrg
382b8e80941Smrg	/* NOTE: for input/outputs, slot is:
383b8e80941Smrg	 *   gl_vert_attrib  - for VS inputs
384b8e80941Smrg	 *   gl_varying_slot - for VS output / FS input
385b8e80941Smrg	 *   gl_frag_result  - for FS output
386b8e80941Smrg	 */
387b8e80941Smrg
388b8e80941Smrg	/* varyings/outputs: */
389b8e80941Smrg	unsigned outputs_count;
390b8e80941Smrg	struct {
391b8e80941Smrg		uint8_t slot;
392b8e80941Smrg		uint8_t regid;
393b8e80941Smrg		bool    half : 1;
394b8e80941Smrg	} outputs[16 + 2];  /* +POSITION +PSIZE */
395b8e80941Smrg	bool writes_pos, writes_smask, writes_psize;
396b8e80941Smrg
397b8e80941Smrg	/* attributes (VS) / varyings (FS):
398b8e80941Smrg	 * Note that sysval's should come *after* normal inputs.
399b8e80941Smrg	 */
400b8e80941Smrg	unsigned inputs_count;
401b8e80941Smrg	struct {
402b8e80941Smrg		uint8_t slot;
403b8e80941Smrg		uint8_t regid;
404b8e80941Smrg		uint8_t compmask;
405b8e80941Smrg		uint8_t ncomp;
406b8e80941Smrg		/* location of input (ie. offset passed to bary.f, etc).  This
407b8e80941Smrg		 * matches the SP_VS_VPC_DST_REG.OUTLOCn value (a3xx and a4xx
408b8e80941Smrg		 * have the OUTLOCn value offset by 8, presumably to account
409b8e80941Smrg		 * for gl_Position/gl_PointSize)
410b8e80941Smrg		 */
411b8e80941Smrg		uint8_t inloc;
412b8e80941Smrg		/* vertex shader specific: */
413b8e80941Smrg		bool    sysval     : 1;   /* slot is a gl_system_value */
414b8e80941Smrg		/* fragment shader specific: */
415b8e80941Smrg		bool    bary       : 1;   /* fetched varying (vs one loaded into reg) */
416b8e80941Smrg		bool    rasterflat : 1;   /* special handling for emit->rasterflat */
417b8e80941Smrg		bool    use_ldlv   : 1;   /* internal to ir3_compiler_nir */
418b8e80941Smrg		bool    half       : 1;
419b8e80941Smrg		enum glsl_interp_mode interpolate;
420b8e80941Smrg	} inputs[16 + 2];  /* +POSITION +FACE */
421b8e80941Smrg
422b8e80941Smrg	/* sum of input components (scalar).  For frag shaders, it only counts
423b8e80941Smrg	 * the varying inputs:
424b8e80941Smrg	 */
425b8e80941Smrg	unsigned total_in;
426b8e80941Smrg
427b8e80941Smrg	/* For frag shaders, the total number of inputs (not scalar,
428b8e80941Smrg	 * ie. SP_VS_PARAM_REG.TOTALVSOUTVAR)
429b8e80941Smrg	 */
430b8e80941Smrg	unsigned varying_in;
431b8e80941Smrg
432b8e80941Smrg	/* Remapping table to map Image and SSBO to hw state: */
433b8e80941Smrg	struct ir3_ibo_mapping image_mapping;
434b8e80941Smrg
435b8e80941Smrg	/* number of samplers/textures (which are currently 1:1): */
436b8e80941Smrg	int num_samp;
437b8e80941Smrg
438b8e80941Smrg	/* is there an implicit sampler to read framebuffer (FS only).. if
439b8e80941Smrg	 * so the sampler-idx is 'num_samp - 1' (ie. it is appended after
440b8e80941Smrg	 * the last "real" texture)
441b8e80941Smrg	 */
442b8e80941Smrg	bool fb_read;
443b8e80941Smrg
444b8e80941Smrg	/* do we have one or more SSBO instructions: */
445b8e80941Smrg	bool has_ssbo;
446b8e80941Smrg
447b8e80941Smrg	/* do we need derivatives: */
448b8e80941Smrg	bool need_pixlod;
449b8e80941Smrg
450b8e80941Smrg	/* do we have kill, image write, etc (which prevents early-z): */
451b8e80941Smrg	bool no_earlyz;
452b8e80941Smrg
453b8e80941Smrg	bool per_samp;
454b8e80941Smrg
455b8e80941Smrg	/* Layout of constant registers, each section (in vec4). Pointer size
456b8e80941Smrg	 * is 32b (a3xx, a4xx), or 64b (a5xx+), which effects the size of the
457b8e80941Smrg	 * UBO and stream-out consts.
458b8e80941Smrg	 */
459b8e80941Smrg	struct {
460b8e80941Smrg		/* user const start at zero */
461b8e80941Smrg		unsigned ubo;
462b8e80941Smrg		/* NOTE that a3xx might need a section for SSBO addresses too */
463b8e80941Smrg		unsigned ssbo_sizes;
464b8e80941Smrg		unsigned image_dims;
465b8e80941Smrg		unsigned driver_param;
466b8e80941Smrg		unsigned tfbo;
467b8e80941Smrg		unsigned immediate;
468b8e80941Smrg	} constbase;
469b8e80941Smrg
470b8e80941Smrg	unsigned immediates_count;
471b8e80941Smrg	unsigned immediates_size;
472b8e80941Smrg	struct {
473b8e80941Smrg		uint32_t val[4];
474b8e80941Smrg	} *immediates;
475b8e80941Smrg
476b8e80941Smrg	/* for astc srgb workaround, the number/base of additional
477b8e80941Smrg	 * alpha tex states we need, and index of original tex states
478b8e80941Smrg	 */
479b8e80941Smrg	struct {
480b8e80941Smrg		unsigned base, count;
481b8e80941Smrg		unsigned orig_idx[16];
482b8e80941Smrg	} astc_srgb;
483b8e80941Smrg
484b8e80941Smrg	/* shader variants form a linked list: */
485b8e80941Smrg	struct ir3_shader_variant *next;
486b8e80941Smrg
487b8e80941Smrg	/* replicated here to avoid passing extra ptrs everywhere: */
488b8e80941Smrg	gl_shader_stage type;
489b8e80941Smrg	struct ir3_shader *shader;
490b8e80941Smrg};
491b8e80941Smrg
492b8e80941Smrgstruct ir3_ubo_range {
493b8e80941Smrg	uint32_t offset; /* start offset of this block in const register file */
494b8e80941Smrg	uint32_t start, end; /* range of block that's actually used */
495b8e80941Smrg};
496b8e80941Smrg
497b8e80941Smrgstruct ir3_ubo_analysis_state
498b8e80941Smrg{
499b8e80941Smrg	struct ir3_ubo_range range[IR3_MAX_CONSTANT_BUFFERS];
500b8e80941Smrg	uint32_t size;
501b8e80941Smrg	uint32_t lower_count;
502b8e80941Smrg};
503b8e80941Smrg
504b8e80941Smrg
505b8e80941Smrgstruct ir3_shader {
506b8e80941Smrg	gl_shader_stage type;
507b8e80941Smrg
508b8e80941Smrg	/* shader id (for debug): */
509b8e80941Smrg	uint32_t id;
510b8e80941Smrg	uint32_t variant_count;
511b8e80941Smrg
512b8e80941Smrg	/* so we know when we can disable TGSI related hacks: */
513b8e80941Smrg	bool from_tgsi;
514b8e80941Smrg
515b8e80941Smrg	struct ir3_compiler *compiler;
516b8e80941Smrg
517b8e80941Smrg	struct ir3_ubo_analysis_state ubo_state;
518b8e80941Smrg
519b8e80941Smrg	struct nir_shader *nir;
520b8e80941Smrg	struct ir3_stream_output_info stream_output;
521b8e80941Smrg
522b8e80941Smrg	struct ir3_shader_variant *variants;
523b8e80941Smrg};
524b8e80941Smrg
525b8e80941Smrgvoid * ir3_shader_assemble(struct ir3_shader_variant *v, uint32_t gpu_id);
526b8e80941Smrgstruct ir3_shader_variant * ir3_shader_get_variant(struct ir3_shader *shader,
527b8e80941Smrg		struct ir3_shader_key *key, bool binning_pass, bool *created);
528b8e80941Smrgstruct ir3_shader * ir3_shader_from_nir(struct ir3_compiler *compiler, nir_shader *nir);
529b8e80941Smrgvoid ir3_shader_destroy(struct ir3_shader *shader);
530b8e80941Smrgvoid ir3_shader_disasm(struct ir3_shader_variant *so, uint32_t *bin, FILE *out);
531b8e80941Smrguint64_t ir3_shader_outputs(const struct ir3_shader *so);
532b8e80941Smrg
533b8e80941Smrgint
534b8e80941Smrgir3_glsl_type_size(const struct glsl_type *type, bool bindless);
535b8e80941Smrg
536b8e80941Smrgstatic inline const char *
537b8e80941Smrgir3_shader_stage(struct ir3_shader *shader)
538b8e80941Smrg{
539b8e80941Smrg	switch (shader->type) {
540b8e80941Smrg	case MESA_SHADER_VERTEX:     return "VERT";
541b8e80941Smrg	case MESA_SHADER_FRAGMENT:   return "FRAG";
542b8e80941Smrg	case MESA_SHADER_COMPUTE:    return "CL";
543b8e80941Smrg	default:
544b8e80941Smrg		unreachable("invalid type");
545b8e80941Smrg		return NULL;
546b8e80941Smrg	}
547b8e80941Smrg}
548b8e80941Smrg
549b8e80941Smrg/*
550b8e80941Smrg * Helper/util:
551b8e80941Smrg */
552b8e80941Smrg
553b8e80941Smrgstatic inline int
554b8e80941Smrgir3_find_output(const struct ir3_shader_variant *so, gl_varying_slot slot)
555b8e80941Smrg{
556b8e80941Smrg	int j;
557b8e80941Smrg
558b8e80941Smrg	for (j = 0; j < so->outputs_count; j++)
559b8e80941Smrg		if (so->outputs[j].slot == slot)
560b8e80941Smrg			return j;
561b8e80941Smrg
562b8e80941Smrg	/* it seems optional to have a OUT.BCOLOR[n] for each OUT.COLOR[n]
563b8e80941Smrg	 * in the vertex shader.. but the fragment shader doesn't know this
564b8e80941Smrg	 * so  it will always have both IN.COLOR[n] and IN.BCOLOR[n].  So
565b8e80941Smrg	 * at link time if there is no matching OUT.BCOLOR[n], we must map
566b8e80941Smrg	 * OUT.COLOR[n] to IN.BCOLOR[n].  And visa versa if there is only
567b8e80941Smrg	 * a OUT.BCOLOR[n] but no matching OUT.COLOR[n]
568b8e80941Smrg	 */
569b8e80941Smrg	if (slot == VARYING_SLOT_BFC0) {
570b8e80941Smrg		slot = VARYING_SLOT_COL0;
571b8e80941Smrg	} else if (slot == VARYING_SLOT_BFC1) {
572b8e80941Smrg		slot = VARYING_SLOT_COL1;
573b8e80941Smrg	} else if (slot == VARYING_SLOT_COL0) {
574b8e80941Smrg		slot = VARYING_SLOT_BFC0;
575b8e80941Smrg	} else if (slot == VARYING_SLOT_COL1) {
576b8e80941Smrg		slot = VARYING_SLOT_BFC1;
577b8e80941Smrg	} else {
578b8e80941Smrg		return 0;
579b8e80941Smrg	}
580b8e80941Smrg
581b8e80941Smrg	for (j = 0; j < so->outputs_count; j++)
582b8e80941Smrg		if (so->outputs[j].slot == slot)
583b8e80941Smrg			return j;
584b8e80941Smrg
585b8e80941Smrg	debug_assert(0);
586b8e80941Smrg
587b8e80941Smrg	return 0;
588b8e80941Smrg}
589b8e80941Smrg
590b8e80941Smrgstatic inline int
591b8e80941Smrgir3_next_varying(const struct ir3_shader_variant *so, int i)
592b8e80941Smrg{
593b8e80941Smrg	while (++i < so->inputs_count)
594b8e80941Smrg		if (so->inputs[i].compmask && so->inputs[i].bary)
595b8e80941Smrg			break;
596b8e80941Smrg	return i;
597b8e80941Smrg}
598b8e80941Smrg
599b8e80941Smrgstruct ir3_shader_linkage {
600b8e80941Smrg	uint8_t max_loc;
601b8e80941Smrg	uint8_t cnt;
602b8e80941Smrg	struct {
603b8e80941Smrg		uint8_t regid;
604b8e80941Smrg		uint8_t compmask;
605b8e80941Smrg		uint8_t loc;
606b8e80941Smrg	} var[32];
607b8e80941Smrg};
608b8e80941Smrg
609b8e80941Smrgstatic inline void
610b8e80941Smrgir3_link_add(struct ir3_shader_linkage *l, uint8_t regid, uint8_t compmask, uint8_t loc)
611b8e80941Smrg{
612b8e80941Smrg	int i = l->cnt++;
613b8e80941Smrg
614b8e80941Smrg	debug_assert(i < ARRAY_SIZE(l->var));
615b8e80941Smrg
616b8e80941Smrg	l->var[i].regid    = regid;
617b8e80941Smrg	l->var[i].compmask = compmask;
618b8e80941Smrg	l->var[i].loc      = loc;
619b8e80941Smrg	l->max_loc = MAX2(l->max_loc, loc + util_last_bit(compmask));
620b8e80941Smrg}
621b8e80941Smrg
622b8e80941Smrgstatic inline void
623b8e80941Smrgir3_link_shaders(struct ir3_shader_linkage *l,
624b8e80941Smrg		const struct ir3_shader_variant *vs,
625b8e80941Smrg		const struct ir3_shader_variant *fs)
626b8e80941Smrg{
627b8e80941Smrg	int j = -1, k;
628b8e80941Smrg
629b8e80941Smrg	while (l->cnt < ARRAY_SIZE(l->var)) {
630b8e80941Smrg		j = ir3_next_varying(fs, j);
631b8e80941Smrg
632b8e80941Smrg		if (j >= fs->inputs_count)
633b8e80941Smrg			break;
634b8e80941Smrg
635b8e80941Smrg		if (fs->inputs[j].inloc >= fs->total_in)
636b8e80941Smrg			continue;
637b8e80941Smrg
638b8e80941Smrg		k = ir3_find_output(vs, fs->inputs[j].slot);
639b8e80941Smrg
640b8e80941Smrg		ir3_link_add(l, vs->outputs[k].regid,
641b8e80941Smrg			fs->inputs[j].compmask, fs->inputs[j].inloc);
642b8e80941Smrg	}
643b8e80941Smrg}
644b8e80941Smrg
645b8e80941Smrgstatic inline uint32_t
646b8e80941Smrgir3_find_output_regid(const struct ir3_shader_variant *so, unsigned slot)
647b8e80941Smrg{
648b8e80941Smrg	int j;
649b8e80941Smrg	for (j = 0; j < so->outputs_count; j++)
650b8e80941Smrg		if (so->outputs[j].slot == slot)
651b8e80941Smrg			return so->outputs[j].regid;
652b8e80941Smrg	return regid(63, 0);
653b8e80941Smrg}
654b8e80941Smrg
655b8e80941Smrgstatic inline uint32_t
656b8e80941Smrgir3_find_sysval_regid(const struct ir3_shader_variant *so, unsigned slot)
657b8e80941Smrg{
658b8e80941Smrg	int j;
659b8e80941Smrg	for (j = 0; j < so->inputs_count; j++)
660b8e80941Smrg		if (so->inputs[j].sysval && (so->inputs[j].slot == slot))
661b8e80941Smrg			return so->inputs[j].regid;
662b8e80941Smrg	return regid(63, 0);
663b8e80941Smrg}
664b8e80941Smrg
665b8e80941Smrg/* calculate register footprint in terms of half-regs (ie. one full
666b8e80941Smrg * reg counts as two half-regs).
667b8e80941Smrg */
668b8e80941Smrgstatic inline uint32_t
669b8e80941Smrgir3_shader_halfregs(const struct ir3_shader_variant *v)
670b8e80941Smrg{
671b8e80941Smrg	return (2 * (v->info.max_reg + 1)) + (v->info.max_half_reg + 1);
672b8e80941Smrg}
673b8e80941Smrg
674b8e80941Smrg#endif /* IR3_SHADER_H_ */
675