1bf215546Sopenharmony_ci/*
2bf215546Sopenharmony_ci * Copyright (c) 2012-2013 Etnaviv Project
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 * the rights to use, copy, modify, merge, publish, distribute, sub license,
8bf215546Sopenharmony_ci * and/or sell copies of the Software, and to permit persons to whom the
9bf215546Sopenharmony_ci * 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
12bf215546Sopenharmony_ci * next paragraph) shall be included in all copies or substantial portions
13bf215546Sopenharmony_ci * of the 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 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19bf215546Sopenharmony_ci * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20bf215546Sopenharmony_ci * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21bf215546Sopenharmony_ci * DEALINGS IN THE SOFTWARE.
22bf215546Sopenharmony_ci */
23bf215546Sopenharmony_ci/* inlined translation functions between gallium and vivante */
24bf215546Sopenharmony_ci#ifndef H_TRANSLATE
25bf215546Sopenharmony_ci#define H_TRANSLATE
26bf215546Sopenharmony_ci
27bf215546Sopenharmony_ci#include "pipe/p_defines.h"
28bf215546Sopenharmony_ci#include "pipe/p_format.h"
29bf215546Sopenharmony_ci#include "pipe/p_state.h"
30bf215546Sopenharmony_ci
31bf215546Sopenharmony_ci#include "etnaviv_debug.h"
32bf215546Sopenharmony_ci#include "etnaviv_format.h"
33bf215546Sopenharmony_ci#include "etnaviv_util.h"
34bf215546Sopenharmony_ci#include "hw/cmdstream.xml.h"
35bf215546Sopenharmony_ci#include "hw/common_3d.xml.h"
36bf215546Sopenharmony_ci#include "hw/state.xml.h"
37bf215546Sopenharmony_ci#include "hw/state_3d.xml.h"
38bf215546Sopenharmony_ci
39bf215546Sopenharmony_ci#include "util/format/u_format.h"
40bf215546Sopenharmony_ci#include "util/u_math.h"
41bf215546Sopenharmony_ci
42bf215546Sopenharmony_ci/* Returned when there is no match of pipe value to etna value */
43bf215546Sopenharmony_ci#define ETNA_NO_MATCH (~0)
44bf215546Sopenharmony_ci
45bf215546Sopenharmony_cistatic inline uint32_t
46bf215546Sopenharmony_citranslate_cull_face(unsigned cull_face, unsigned front_ccw)
47bf215546Sopenharmony_ci{
48bf215546Sopenharmony_ci   switch (cull_face) {
49bf215546Sopenharmony_ci   case PIPE_FACE_NONE:
50bf215546Sopenharmony_ci   case PIPE_FACE_FRONT_AND_BACK: /* handled in draw_vbo */
51bf215546Sopenharmony_ci      return VIVS_PA_CONFIG_CULL_FACE_MODE_OFF;
52bf215546Sopenharmony_ci   case PIPE_FACE_BACK:
53bf215546Sopenharmony_ci      return front_ccw ? VIVS_PA_CONFIG_CULL_FACE_MODE_CW
54bf215546Sopenharmony_ci                       : VIVS_PA_CONFIG_CULL_FACE_MODE_CCW;
55bf215546Sopenharmony_ci   case PIPE_FACE_FRONT:
56bf215546Sopenharmony_ci      return front_ccw ? VIVS_PA_CONFIG_CULL_FACE_MODE_CCW
57bf215546Sopenharmony_ci                       : VIVS_PA_CONFIG_CULL_FACE_MODE_CW;
58bf215546Sopenharmony_ci   default:
59bf215546Sopenharmony_ci      DBG("Unhandled cull face mode %i", cull_face);
60bf215546Sopenharmony_ci      return ETNA_NO_MATCH;
61bf215546Sopenharmony_ci   }
62bf215546Sopenharmony_ci}
63bf215546Sopenharmony_ci
64bf215546Sopenharmony_cistatic inline uint32_t
65bf215546Sopenharmony_citranslate_polygon_mode(unsigned polygon_mode)
66bf215546Sopenharmony_ci{
67bf215546Sopenharmony_ci   switch (polygon_mode) {
68bf215546Sopenharmony_ci   case PIPE_POLYGON_MODE_FILL:
69bf215546Sopenharmony_ci      return VIVS_PA_CONFIG_FILL_MODE_SOLID;
70bf215546Sopenharmony_ci   case PIPE_POLYGON_MODE_LINE:
71bf215546Sopenharmony_ci      return VIVS_PA_CONFIG_FILL_MODE_WIREFRAME;
72bf215546Sopenharmony_ci   case PIPE_POLYGON_MODE_POINT:
73bf215546Sopenharmony_ci      return VIVS_PA_CONFIG_FILL_MODE_POINT;
74bf215546Sopenharmony_ci   default:
75bf215546Sopenharmony_ci      DBG("Unhandled polygon mode %i", polygon_mode);
76bf215546Sopenharmony_ci      return ETNA_NO_MATCH;
77bf215546Sopenharmony_ci   }
78bf215546Sopenharmony_ci}
79bf215546Sopenharmony_ci
80bf215546Sopenharmony_cistatic inline uint32_t
81bf215546Sopenharmony_citranslate_stencil_mode(bool enable_0, bool enable_1)
82bf215546Sopenharmony_ci{
83bf215546Sopenharmony_ci   if (enable_0) {
84bf215546Sopenharmony_ci      return enable_1 ? VIVS_PE_STENCIL_CONFIG_MODE_TWO_SIDED
85bf215546Sopenharmony_ci                      : VIVS_PE_STENCIL_CONFIG_MODE_ONE_SIDED;
86bf215546Sopenharmony_ci   } else {
87bf215546Sopenharmony_ci      return VIVS_PE_STENCIL_CONFIG_MODE_DISABLED;
88bf215546Sopenharmony_ci   }
89bf215546Sopenharmony_ci}
90bf215546Sopenharmony_ci
91bf215546Sopenharmony_cistatic inline uint32_t
92bf215546Sopenharmony_citranslate_stencil_op(unsigned stencil_op)
93bf215546Sopenharmony_ci{
94bf215546Sopenharmony_ci   switch (stencil_op) {
95bf215546Sopenharmony_ci   case PIPE_STENCIL_OP_KEEP:
96bf215546Sopenharmony_ci      return STENCIL_OP_KEEP;
97bf215546Sopenharmony_ci   case PIPE_STENCIL_OP_ZERO:
98bf215546Sopenharmony_ci      return STENCIL_OP_ZERO;
99bf215546Sopenharmony_ci   case PIPE_STENCIL_OP_REPLACE:
100bf215546Sopenharmony_ci      return STENCIL_OP_REPLACE;
101bf215546Sopenharmony_ci   case PIPE_STENCIL_OP_INCR:
102bf215546Sopenharmony_ci      return STENCIL_OP_INCR;
103bf215546Sopenharmony_ci   case PIPE_STENCIL_OP_DECR:
104bf215546Sopenharmony_ci      return STENCIL_OP_DECR;
105bf215546Sopenharmony_ci   case PIPE_STENCIL_OP_INCR_WRAP:
106bf215546Sopenharmony_ci      return STENCIL_OP_INCR_WRAP;
107bf215546Sopenharmony_ci   case PIPE_STENCIL_OP_DECR_WRAP:
108bf215546Sopenharmony_ci      return STENCIL_OP_DECR_WRAP;
109bf215546Sopenharmony_ci   case PIPE_STENCIL_OP_INVERT:
110bf215546Sopenharmony_ci      return STENCIL_OP_INVERT;
111bf215546Sopenharmony_ci   default:
112bf215546Sopenharmony_ci      DBG("Unhandled stencil op: %i", stencil_op);
113bf215546Sopenharmony_ci      return ETNA_NO_MATCH;
114bf215546Sopenharmony_ci   }
115bf215546Sopenharmony_ci}
116bf215546Sopenharmony_ci
117bf215546Sopenharmony_cistatic inline uint32_t
118bf215546Sopenharmony_citranslate_blend_factor(unsigned blend_factor)
119bf215546Sopenharmony_ci{
120bf215546Sopenharmony_ci   switch (blend_factor) {
121bf215546Sopenharmony_ci   case PIPE_BLENDFACTOR_ONE:
122bf215546Sopenharmony_ci      return BLEND_FUNC_ONE;
123bf215546Sopenharmony_ci   case PIPE_BLENDFACTOR_SRC_COLOR:
124bf215546Sopenharmony_ci      return BLEND_FUNC_SRC_COLOR;
125bf215546Sopenharmony_ci   case PIPE_BLENDFACTOR_SRC_ALPHA:
126bf215546Sopenharmony_ci      return BLEND_FUNC_SRC_ALPHA;
127bf215546Sopenharmony_ci   case PIPE_BLENDFACTOR_DST_ALPHA:
128bf215546Sopenharmony_ci      return BLEND_FUNC_DST_ALPHA;
129bf215546Sopenharmony_ci   case PIPE_BLENDFACTOR_DST_COLOR:
130bf215546Sopenharmony_ci      return BLEND_FUNC_DST_COLOR;
131bf215546Sopenharmony_ci   case PIPE_BLENDFACTOR_SRC_ALPHA_SATURATE:
132bf215546Sopenharmony_ci      return BLEND_FUNC_SRC_ALPHA_SATURATE;
133bf215546Sopenharmony_ci   case PIPE_BLENDFACTOR_CONST_COLOR:
134bf215546Sopenharmony_ci      return BLEND_FUNC_CONSTANT_COLOR;
135bf215546Sopenharmony_ci   case PIPE_BLENDFACTOR_CONST_ALPHA:
136bf215546Sopenharmony_ci      return BLEND_FUNC_CONSTANT_ALPHA;
137bf215546Sopenharmony_ci   case PIPE_BLENDFACTOR_ZERO:
138bf215546Sopenharmony_ci      return BLEND_FUNC_ZERO;
139bf215546Sopenharmony_ci   case PIPE_BLENDFACTOR_INV_SRC_COLOR:
140bf215546Sopenharmony_ci      return BLEND_FUNC_ONE_MINUS_SRC_COLOR;
141bf215546Sopenharmony_ci   case PIPE_BLENDFACTOR_INV_SRC_ALPHA:
142bf215546Sopenharmony_ci      return BLEND_FUNC_ONE_MINUS_SRC_ALPHA;
143bf215546Sopenharmony_ci   case PIPE_BLENDFACTOR_INV_DST_ALPHA:
144bf215546Sopenharmony_ci      return BLEND_FUNC_ONE_MINUS_DST_ALPHA;
145bf215546Sopenharmony_ci   case PIPE_BLENDFACTOR_INV_DST_COLOR:
146bf215546Sopenharmony_ci      return BLEND_FUNC_ONE_MINUS_DST_COLOR;
147bf215546Sopenharmony_ci   case PIPE_BLENDFACTOR_INV_CONST_COLOR:
148bf215546Sopenharmony_ci      return BLEND_FUNC_ONE_MINUS_CONSTANT_COLOR;
149bf215546Sopenharmony_ci   case PIPE_BLENDFACTOR_INV_CONST_ALPHA:
150bf215546Sopenharmony_ci      return BLEND_FUNC_ONE_MINUS_CONSTANT_ALPHA;
151bf215546Sopenharmony_ci   case PIPE_BLENDFACTOR_SRC1_COLOR:
152bf215546Sopenharmony_ci   case PIPE_BLENDFACTOR_SRC1_ALPHA:
153bf215546Sopenharmony_ci   case PIPE_BLENDFACTOR_INV_SRC1_COLOR:
154bf215546Sopenharmony_ci   case PIPE_BLENDFACTOR_INV_SRC1_ALPHA:
155bf215546Sopenharmony_ci   default:
156bf215546Sopenharmony_ci      DBG("Unhandled blend factor: %i", blend_factor);
157bf215546Sopenharmony_ci      return ETNA_NO_MATCH;
158bf215546Sopenharmony_ci   }
159bf215546Sopenharmony_ci}
160bf215546Sopenharmony_ci
161bf215546Sopenharmony_cistatic inline uint32_t
162bf215546Sopenharmony_citranslate_texture_wrapmode(unsigned wrap)
163bf215546Sopenharmony_ci{
164bf215546Sopenharmony_ci   switch (wrap) {
165bf215546Sopenharmony_ci   case PIPE_TEX_WRAP_REPEAT:
166bf215546Sopenharmony_ci      return TEXTURE_WRAPMODE_REPEAT;
167bf215546Sopenharmony_ci   case PIPE_TEX_WRAP_CLAMP:
168bf215546Sopenharmony_ci      return TEXTURE_WRAPMODE_CLAMP_TO_EDGE;
169bf215546Sopenharmony_ci   case PIPE_TEX_WRAP_CLAMP_TO_EDGE:
170bf215546Sopenharmony_ci      return TEXTURE_WRAPMODE_CLAMP_TO_EDGE;
171bf215546Sopenharmony_ci   case PIPE_TEX_WRAP_CLAMP_TO_BORDER:
172bf215546Sopenharmony_ci      return TEXTURE_WRAPMODE_CLAMP_TO_EDGE; /* XXX */
173bf215546Sopenharmony_ci   case PIPE_TEX_WRAP_MIRROR_REPEAT:
174bf215546Sopenharmony_ci      return TEXTURE_WRAPMODE_MIRRORED_REPEAT;
175bf215546Sopenharmony_ci   case PIPE_TEX_WRAP_MIRROR_CLAMP:
176bf215546Sopenharmony_ci      return TEXTURE_WRAPMODE_MIRRORED_REPEAT; /* XXX */
177bf215546Sopenharmony_ci   case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_EDGE:
178bf215546Sopenharmony_ci      return TEXTURE_WRAPMODE_MIRRORED_REPEAT; /* XXX */
179bf215546Sopenharmony_ci   case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_BORDER:
180bf215546Sopenharmony_ci      return TEXTURE_WRAPMODE_MIRRORED_REPEAT; /* XXX */
181bf215546Sopenharmony_ci   default:
182bf215546Sopenharmony_ci      DBG("Unhandled texture wrapmode: %i", wrap);
183bf215546Sopenharmony_ci      return ETNA_NO_MATCH;
184bf215546Sopenharmony_ci   }
185bf215546Sopenharmony_ci}
186bf215546Sopenharmony_ci
187bf215546Sopenharmony_cistatic inline uint32_t
188bf215546Sopenharmony_citranslate_texture_mipfilter(unsigned filter)
189bf215546Sopenharmony_ci{
190bf215546Sopenharmony_ci   switch (filter) {
191bf215546Sopenharmony_ci   case PIPE_TEX_MIPFILTER_NEAREST:
192bf215546Sopenharmony_ci      return TEXTURE_FILTER_NEAREST;
193bf215546Sopenharmony_ci   case PIPE_TEX_MIPFILTER_LINEAR:
194bf215546Sopenharmony_ci      return TEXTURE_FILTER_LINEAR;
195bf215546Sopenharmony_ci   case PIPE_TEX_MIPFILTER_NONE:
196bf215546Sopenharmony_ci      return TEXTURE_FILTER_NONE;
197bf215546Sopenharmony_ci   default:
198bf215546Sopenharmony_ci      DBG("Unhandled texture mipfilter: %i", filter);
199bf215546Sopenharmony_ci      return ETNA_NO_MATCH;
200bf215546Sopenharmony_ci   }
201bf215546Sopenharmony_ci}
202bf215546Sopenharmony_ci
203bf215546Sopenharmony_cistatic inline uint32_t
204bf215546Sopenharmony_citranslate_texture_filter(unsigned filter)
205bf215546Sopenharmony_ci{
206bf215546Sopenharmony_ci   switch (filter) {
207bf215546Sopenharmony_ci   case PIPE_TEX_FILTER_NEAREST:
208bf215546Sopenharmony_ci      return TEXTURE_FILTER_NEAREST;
209bf215546Sopenharmony_ci   case PIPE_TEX_FILTER_LINEAR:
210bf215546Sopenharmony_ci      return TEXTURE_FILTER_LINEAR;
211bf215546Sopenharmony_ci   default:
212bf215546Sopenharmony_ci      DBG("Unhandled texture filter: %i", filter);
213bf215546Sopenharmony_ci      return ETNA_NO_MATCH;
214bf215546Sopenharmony_ci   }
215bf215546Sopenharmony_ci}
216bf215546Sopenharmony_ci
217bf215546Sopenharmony_cistatic inline int
218bf215546Sopenharmony_citranslate_rb_src_dst_swap(enum pipe_format src, enum pipe_format dst)
219bf215546Sopenharmony_ci{
220bf215546Sopenharmony_ci   return translate_pe_format_rb_swap(src) ^ translate_pe_format_rb_swap(dst);
221bf215546Sopenharmony_ci}
222bf215546Sopenharmony_ci
223bf215546Sopenharmony_cistatic inline uint32_t
224bf215546Sopenharmony_citranslate_depth_format(enum pipe_format fmt)
225bf215546Sopenharmony_ci{
226bf215546Sopenharmony_ci   /* Note: Pipe format convention is LSB to MSB, VIVS is MSB to LSB */
227bf215546Sopenharmony_ci   switch (fmt) {
228bf215546Sopenharmony_ci   case PIPE_FORMAT_Z16_UNORM:
229bf215546Sopenharmony_ci      return VIVS_PE_DEPTH_CONFIG_DEPTH_FORMAT_D16;
230bf215546Sopenharmony_ci   case PIPE_FORMAT_X8Z24_UNORM:
231bf215546Sopenharmony_ci      return VIVS_PE_DEPTH_CONFIG_DEPTH_FORMAT_D24S8;
232bf215546Sopenharmony_ci   case PIPE_FORMAT_S8_UINT_Z24_UNORM:
233bf215546Sopenharmony_ci      return VIVS_PE_DEPTH_CONFIG_DEPTH_FORMAT_D24S8;
234bf215546Sopenharmony_ci   default:
235bf215546Sopenharmony_ci      return ETNA_NO_MATCH;
236bf215546Sopenharmony_ci   }
237bf215546Sopenharmony_ci}
238bf215546Sopenharmony_ci
239bf215546Sopenharmony_ci/* render target format for MSAA */
240bf215546Sopenharmony_cistatic inline uint32_t
241bf215546Sopenharmony_citranslate_ts_format(enum pipe_format fmt)
242bf215546Sopenharmony_ci{
243bf215546Sopenharmony_ci   /* Note: Pipe format convention is LSB to MSB, VIVS is MSB to LSB */
244bf215546Sopenharmony_ci   switch (fmt) {
245bf215546Sopenharmony_ci   case PIPE_FORMAT_B4G4R4X4_UNORM:
246bf215546Sopenharmony_ci   case PIPE_FORMAT_B4G4R4A4_UNORM:
247bf215546Sopenharmony_ci      return COMPRESSION_FORMAT_A4R4G4B4;
248bf215546Sopenharmony_ci   case PIPE_FORMAT_B5G5R5X1_UNORM:
249bf215546Sopenharmony_ci      return COMPRESSION_FORMAT_A1R5G5B5;
250bf215546Sopenharmony_ci   case PIPE_FORMAT_B5G5R5A1_UNORM:
251bf215546Sopenharmony_ci      return COMPRESSION_FORMAT_A1R5G5B5;
252bf215546Sopenharmony_ci   case PIPE_FORMAT_B5G6R5_UNORM:
253bf215546Sopenharmony_ci      return COMPRESSION_FORMAT_R5G6B5;
254bf215546Sopenharmony_ci   case PIPE_FORMAT_B8G8R8X8_UNORM:
255bf215546Sopenharmony_ci   case PIPE_FORMAT_B8G8R8X8_SRGB:
256bf215546Sopenharmony_ci   case PIPE_FORMAT_R8G8B8X8_UNORM:
257bf215546Sopenharmony_ci      return COMPRESSION_FORMAT_X8R8G8B8;
258bf215546Sopenharmony_ci   case PIPE_FORMAT_B8G8R8A8_UNORM:
259bf215546Sopenharmony_ci   case PIPE_FORMAT_B8G8R8A8_SRGB:
260bf215546Sopenharmony_ci   case PIPE_FORMAT_R8G8B8A8_UNORM:
261bf215546Sopenharmony_ci      return COMPRESSION_FORMAT_A8R8G8B8;
262bf215546Sopenharmony_ci   case PIPE_FORMAT_S8_UINT_Z24_UNORM:
263bf215546Sopenharmony_ci      return COMPRESSION_FORMAT_D24S8;
264bf215546Sopenharmony_ci   case PIPE_FORMAT_X8Z24_UNORM:
265bf215546Sopenharmony_ci      return COMPRESSION_FORMAT_D24X8;
266bf215546Sopenharmony_ci   case PIPE_FORMAT_Z16_UNORM:
267bf215546Sopenharmony_ci      return COMPRESSION_FORMAT_D16;
268bf215546Sopenharmony_ci   /* MSAA with YUYV not supported */
269bf215546Sopenharmony_ci   default:
270bf215546Sopenharmony_ci      return ETNA_NO_MATCH;
271bf215546Sopenharmony_ci   }
272bf215546Sopenharmony_ci}
273bf215546Sopenharmony_ci
274bf215546Sopenharmony_ci/* Return normalization flag for vertex element format */
275bf215546Sopenharmony_cistatic inline uint32_t
276bf215546Sopenharmony_citranslate_vertex_format_normalize(enum pipe_format fmt)
277bf215546Sopenharmony_ci{
278bf215546Sopenharmony_ci   const struct util_format_description *desc = util_format_description(fmt);
279bf215546Sopenharmony_ci
280bf215546Sopenharmony_ci   /* assumes that normalization of channel 0 holds for all channels;
281bf215546Sopenharmony_ci    * this holds for all vertex formats that we support */
282bf215546Sopenharmony_ci   return desc->channel[0].normalized
283bf215546Sopenharmony_ci             ? VIVS_FE_VERTEX_ELEMENT_CONFIG_NORMALIZE_SIGN_EXTEND
284bf215546Sopenharmony_ci             : VIVS_FE_VERTEX_ELEMENT_CONFIG_NORMALIZE_OFF;
285bf215546Sopenharmony_ci}
286bf215546Sopenharmony_ci
287bf215546Sopenharmony_cistatic inline uint32_t
288bf215546Sopenharmony_citranslate_output_mode(enum pipe_format fmt, bool halti5)
289bf215546Sopenharmony_ci{
290bf215546Sopenharmony_ci   const unsigned bits =
291bf215546Sopenharmony_ci      util_format_get_component_bits(fmt, UTIL_FORMAT_COLORSPACE_RGB, 0);
292bf215546Sopenharmony_ci
293bf215546Sopenharmony_ci   if (bits == 32)
294bf215546Sopenharmony_ci      return COLOR_OUTPUT_MODE_UIF32;
295bf215546Sopenharmony_ci
296bf215546Sopenharmony_ci   if (!util_format_is_pure_integer(fmt))
297bf215546Sopenharmony_ci      return COLOR_OUTPUT_MODE_NORMAL;
298bf215546Sopenharmony_ci
299bf215546Sopenharmony_ci   /* generic integer output mode pre-halti5 (?) */
300bf215546Sopenharmony_ci   if (bits == 10 || !halti5)
301bf215546Sopenharmony_ci      return COLOR_OUTPUT_MODE_A2B10G10R10UI;
302bf215546Sopenharmony_ci
303bf215546Sopenharmony_ci   if (util_format_is_pure_sint(fmt))
304bf215546Sopenharmony_ci      return bits == 8 ? COLOR_OUTPUT_MODE_I8 : COLOR_OUTPUT_MODE_I16;
305bf215546Sopenharmony_ci
306bf215546Sopenharmony_ci   return bits == 8 ? COLOR_OUTPUT_MODE_U8 : COLOR_OUTPUT_MODE_U16;
307bf215546Sopenharmony_ci}
308bf215546Sopenharmony_ci
309bf215546Sopenharmony_cistatic inline uint32_t
310bf215546Sopenharmony_citranslate_index_size(unsigned index_size)
311bf215546Sopenharmony_ci{
312bf215546Sopenharmony_ci   switch (index_size) {
313bf215546Sopenharmony_ci   case 1:
314bf215546Sopenharmony_ci      return VIVS_FE_INDEX_STREAM_CONTROL_TYPE_UNSIGNED_CHAR;
315bf215546Sopenharmony_ci   case 2:
316bf215546Sopenharmony_ci      return VIVS_FE_INDEX_STREAM_CONTROL_TYPE_UNSIGNED_SHORT;
317bf215546Sopenharmony_ci   case 4:
318bf215546Sopenharmony_ci      return VIVS_FE_INDEX_STREAM_CONTROL_TYPE_UNSIGNED_INT;
319bf215546Sopenharmony_ci   default:
320bf215546Sopenharmony_ci      DBG("Unhandled index size %i", index_size);
321bf215546Sopenharmony_ci      return ETNA_NO_MATCH;
322bf215546Sopenharmony_ci   }
323bf215546Sopenharmony_ci}
324bf215546Sopenharmony_ci
325bf215546Sopenharmony_cistatic inline uint32_t
326bf215546Sopenharmony_citranslate_draw_mode(unsigned mode)
327bf215546Sopenharmony_ci{
328bf215546Sopenharmony_ci   switch (mode) {
329bf215546Sopenharmony_ci   case PIPE_PRIM_POINTS:
330bf215546Sopenharmony_ci      return PRIMITIVE_TYPE_POINTS;
331bf215546Sopenharmony_ci   case PIPE_PRIM_LINES:
332bf215546Sopenharmony_ci      return PRIMITIVE_TYPE_LINES;
333bf215546Sopenharmony_ci   case PIPE_PRIM_LINE_LOOP:
334bf215546Sopenharmony_ci      return PRIMITIVE_TYPE_LINE_LOOP;
335bf215546Sopenharmony_ci   case PIPE_PRIM_LINE_STRIP:
336bf215546Sopenharmony_ci      return PRIMITIVE_TYPE_LINE_STRIP;
337bf215546Sopenharmony_ci   case PIPE_PRIM_TRIANGLES:
338bf215546Sopenharmony_ci      return PRIMITIVE_TYPE_TRIANGLES;
339bf215546Sopenharmony_ci   case PIPE_PRIM_TRIANGLE_STRIP:
340bf215546Sopenharmony_ci      return PRIMITIVE_TYPE_TRIANGLE_STRIP;
341bf215546Sopenharmony_ci   case PIPE_PRIM_TRIANGLE_FAN:
342bf215546Sopenharmony_ci      return PRIMITIVE_TYPE_TRIANGLE_FAN;
343bf215546Sopenharmony_ci   case PIPE_PRIM_QUADS:
344bf215546Sopenharmony_ci      return PRIMITIVE_TYPE_QUADS;
345bf215546Sopenharmony_ci   default:
346bf215546Sopenharmony_ci      DBG("Unhandled draw mode primitive %i", mode);
347bf215546Sopenharmony_ci      return ETNA_NO_MATCH;
348bf215546Sopenharmony_ci   }
349bf215546Sopenharmony_ci}
350bf215546Sopenharmony_ci
351bf215546Sopenharmony_ci/* Get size multiple for size of texture/rendertarget with a certain layout
352bf215546Sopenharmony_ci * This is affected by many different parameters:
353bf215546Sopenharmony_ci *   - A horizontal multiple of 16 is used when possible as resolve can be used
354bf215546Sopenharmony_ci *       at the cost of only a little bit extra memory usage.
355bf215546Sopenharmony_ci *   - If the surface is to be used with the resolve engine, set rs_align true.
356bf215546Sopenharmony_ci *       If set, a horizontal multiple of 16 will be used for tiled and linear,
357bf215546Sopenharmony_ci *       otherwise one of 16.  However, such a surface will be incompatible
358bf215546Sopenharmony_ci *       with the samplers if the GPU does hot support the HALIGN feature.
359bf215546Sopenharmony_ci *   - If the surface is supertiled, horizontal and vertical multiple is always 64
360bf215546Sopenharmony_ci *   - If the surface is multi tiled or supertiled, make sure that the vertical size
361bf215546Sopenharmony_ci *     is a multiple of the number of pixel pipes as well.
362bf215546Sopenharmony_ci * */
363bf215546Sopenharmony_cistatic inline void
364bf215546Sopenharmony_cietna_layout_multiple(unsigned layout, unsigned pixel_pipes, bool rs_align,
365bf215546Sopenharmony_ci                     unsigned *paddingX, unsigned *paddingY, unsigned *halign)
366bf215546Sopenharmony_ci{
367bf215546Sopenharmony_ci   switch (layout) {
368bf215546Sopenharmony_ci   case ETNA_LAYOUT_LINEAR:
369bf215546Sopenharmony_ci      *paddingX = rs_align ? 16 : 4;
370bf215546Sopenharmony_ci      *paddingY = 1;
371bf215546Sopenharmony_ci      *halign = rs_align ? TEXTURE_HALIGN_SIXTEEN : TEXTURE_HALIGN_FOUR;
372bf215546Sopenharmony_ci      break;
373bf215546Sopenharmony_ci   case ETNA_LAYOUT_TILED:
374bf215546Sopenharmony_ci      *paddingX = rs_align ? 16 : 4;
375bf215546Sopenharmony_ci      *paddingY = 4;
376bf215546Sopenharmony_ci      *halign = rs_align ? TEXTURE_HALIGN_SIXTEEN : TEXTURE_HALIGN_FOUR;
377bf215546Sopenharmony_ci      break;
378bf215546Sopenharmony_ci   case ETNA_LAYOUT_SUPER_TILED:
379bf215546Sopenharmony_ci      *paddingX = 64;
380bf215546Sopenharmony_ci      *paddingY = 64;
381bf215546Sopenharmony_ci      *halign = TEXTURE_HALIGN_SUPER_TILED;
382bf215546Sopenharmony_ci      break;
383bf215546Sopenharmony_ci   case ETNA_LAYOUT_MULTI_TILED:
384bf215546Sopenharmony_ci      *paddingX = 16;
385bf215546Sopenharmony_ci      *paddingY = 4 * pixel_pipes;
386bf215546Sopenharmony_ci      *halign = TEXTURE_HALIGN_SPLIT_TILED;
387bf215546Sopenharmony_ci      break;
388bf215546Sopenharmony_ci   case ETNA_LAYOUT_MULTI_SUPERTILED:
389bf215546Sopenharmony_ci      *paddingX = 64;
390bf215546Sopenharmony_ci      *paddingY = 64 * pixel_pipes;
391bf215546Sopenharmony_ci      *halign = TEXTURE_HALIGN_SPLIT_SUPER_TILED;
392bf215546Sopenharmony_ci      break;
393bf215546Sopenharmony_ci   default:
394bf215546Sopenharmony_ci      DBG("Unhandled layout %i", layout);
395bf215546Sopenharmony_ci   }
396bf215546Sopenharmony_ci}
397bf215546Sopenharmony_ci
398bf215546Sopenharmony_cistatic inline uint32_t
399bf215546Sopenharmony_citranslate_clear_depth_stencil(enum pipe_format format, float depth,
400bf215546Sopenharmony_ci                              unsigned stencil)
401bf215546Sopenharmony_ci{
402bf215546Sopenharmony_ci   uint32_t clear_value = 0;
403bf215546Sopenharmony_ci
404bf215546Sopenharmony_ci   // XXX util_pack_color
405bf215546Sopenharmony_ci   switch (format) {
406bf215546Sopenharmony_ci   case PIPE_FORMAT_Z16_UNORM:
407bf215546Sopenharmony_ci      clear_value = etna_cfloat_to_uintN(depth, 16);
408bf215546Sopenharmony_ci      clear_value |= clear_value << 16;
409bf215546Sopenharmony_ci      break;
410bf215546Sopenharmony_ci   case PIPE_FORMAT_X8Z24_UNORM:
411bf215546Sopenharmony_ci   case PIPE_FORMAT_S8_UINT_Z24_UNORM:
412bf215546Sopenharmony_ci      clear_value = (etna_cfloat_to_uintN(depth, 24) << 8) | (stencil & 0xFF);
413bf215546Sopenharmony_ci      break;
414bf215546Sopenharmony_ci   default:
415bf215546Sopenharmony_ci      DBG("Unhandled pipe format for depth stencil clear: %i", format);
416bf215546Sopenharmony_ci   }
417bf215546Sopenharmony_ci   return clear_value;
418bf215546Sopenharmony_ci}
419bf215546Sopenharmony_ci
420bf215546Sopenharmony_ci/* Convert MSAA number of samples to x and y scaling factor.
421bf215546Sopenharmony_ci * Return true if supported and false otherwise. */
422bf215546Sopenharmony_cistatic inline bool
423bf215546Sopenharmony_citranslate_samples_to_xyscale(int num_samples, int *xscale_out, int *yscale_out)
424bf215546Sopenharmony_ci{
425bf215546Sopenharmony_ci   int xscale, yscale;
426bf215546Sopenharmony_ci
427bf215546Sopenharmony_ci   switch (num_samples) {
428bf215546Sopenharmony_ci   case 0:
429bf215546Sopenharmony_ci   case 1:
430bf215546Sopenharmony_ci      xscale = 1;
431bf215546Sopenharmony_ci      yscale = 1;
432bf215546Sopenharmony_ci      break;
433bf215546Sopenharmony_ci   case 2:
434bf215546Sopenharmony_ci      xscale = 2;
435bf215546Sopenharmony_ci      yscale = 1;
436bf215546Sopenharmony_ci      break;
437bf215546Sopenharmony_ci   case 4:
438bf215546Sopenharmony_ci      xscale = 2;
439bf215546Sopenharmony_ci      yscale = 2;
440bf215546Sopenharmony_ci      break;
441bf215546Sopenharmony_ci   default:
442bf215546Sopenharmony_ci      return false;
443bf215546Sopenharmony_ci   }
444bf215546Sopenharmony_ci
445bf215546Sopenharmony_ci   if (xscale_out)
446bf215546Sopenharmony_ci      *xscale_out = xscale;
447bf215546Sopenharmony_ci   if (yscale_out)
448bf215546Sopenharmony_ci      *yscale_out = yscale;
449bf215546Sopenharmony_ci
450bf215546Sopenharmony_ci   return true;
451bf215546Sopenharmony_ci}
452bf215546Sopenharmony_ci
453bf215546Sopenharmony_cistatic inline uint32_t
454bf215546Sopenharmony_citranslate_texture_target(unsigned target)
455bf215546Sopenharmony_ci{
456bf215546Sopenharmony_ci   switch (target) {
457bf215546Sopenharmony_ci   case PIPE_TEXTURE_1D:
458bf215546Sopenharmony_ci      return TEXTURE_TYPE_1D;
459bf215546Sopenharmony_ci   case PIPE_TEXTURE_2D:
460bf215546Sopenharmony_ci   case PIPE_TEXTURE_RECT:
461bf215546Sopenharmony_ci   case PIPE_TEXTURE_1D_ARRAY:
462bf215546Sopenharmony_ci      return TEXTURE_TYPE_2D;
463bf215546Sopenharmony_ci   case PIPE_TEXTURE_CUBE:
464bf215546Sopenharmony_ci      return TEXTURE_TYPE_CUBE_MAP;
465bf215546Sopenharmony_ci   case PIPE_TEXTURE_3D:
466bf215546Sopenharmony_ci   case PIPE_TEXTURE_2D_ARRAY:
467bf215546Sopenharmony_ci      return TEXTURE_TYPE_3D;
468bf215546Sopenharmony_ci   default:
469bf215546Sopenharmony_ci      DBG("Unhandled texture target: %i", target);
470bf215546Sopenharmony_ci      return ETNA_NO_MATCH;
471bf215546Sopenharmony_ci   }
472bf215546Sopenharmony_ci}
473bf215546Sopenharmony_ci
474bf215546Sopenharmony_cistatic inline uint32_t
475bf215546Sopenharmony_citranslate_texture_compare(enum pipe_compare_func compare_func)
476bf215546Sopenharmony_ci{
477bf215546Sopenharmony_ci   switch (compare_func) {
478bf215546Sopenharmony_ci   case PIPE_FUNC_NEVER:
479bf215546Sopenharmony_ci      return TEXTURE_COMPARE_FUNC_NEVER;
480bf215546Sopenharmony_ci   case PIPE_FUNC_LESS:
481bf215546Sopenharmony_ci      return TEXTURE_COMPARE_FUNC_LESS;
482bf215546Sopenharmony_ci   case PIPE_FUNC_EQUAL:
483bf215546Sopenharmony_ci      return TEXTURE_COMPARE_FUNC_EQUAL;
484bf215546Sopenharmony_ci   case PIPE_FUNC_LEQUAL:
485bf215546Sopenharmony_ci      return TEXTURE_COMPARE_FUNC_LEQUAL;
486bf215546Sopenharmony_ci   case PIPE_FUNC_GREATER:
487bf215546Sopenharmony_ci      return TEXTURE_COMPARE_FUNC_GREATER;
488bf215546Sopenharmony_ci   case PIPE_FUNC_NOTEQUAL:
489bf215546Sopenharmony_ci      return TEXTURE_COMPARE_FUNC_NOTEQUAL;
490bf215546Sopenharmony_ci   case PIPE_FUNC_GEQUAL:
491bf215546Sopenharmony_ci      return TEXTURE_COMPARE_FUNC_GEQUAL;
492bf215546Sopenharmony_ci   case PIPE_FUNC_ALWAYS:
493bf215546Sopenharmony_ci      return TEXTURE_COMPARE_FUNC_ALWAYS;
494bf215546Sopenharmony_ci   default:
495bf215546Sopenharmony_ci      unreachable("Invalid compare func");
496bf215546Sopenharmony_ci   }
497bf215546Sopenharmony_ci}
498bf215546Sopenharmony_ci
499bf215546Sopenharmony_ci#endif
500