1/*
2 * Copyright (C) 2018-2019 Alyssa Rosenzweig <alyssa@rosenzweig.io>
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 * SOFTWARE.
22 */
23
24#ifndef __MIDGARD_H_
25#define __MIDGARD_H_
26
27#include "compiler/nir/nir.h"
28#include "util/u_dynarray.h"
29
30/* Define the general compiler entry point */
31
32#define MAX_SYSVAL_COUNT 32
33
34/* Allow 2D of sysval IDs, while allowing nonparametric sysvals to equal
35 * their class for equal comparison */
36
37#define PAN_SYSVAL(type, no) ((no << 16) | PAN_SYSVAL_##type)
38
39/* Define some common types. We start at one for easy indexing of hash
40 * tables internal to the compiler */
41
42enum {
43        PAN_SYSVAL_VIEWPORT_SCALE = 1,
44        PAN_SYSVAL_VIEWPORT_OFFSET = 2,
45} pan_sysval;
46
47typedef struct {
48        int work_register_count;
49        int uniform_count;
50        int uniform_cutoff;
51
52        int attribute_count;
53        int varying_count;
54
55        /* Prepended before uniforms, mapping to SYSVAL_ names for the
56         * sysval */
57
58        unsigned sysval_count;
59        unsigned sysvals[MAX_SYSVAL_COUNT];
60
61        unsigned varyings[32];
62
63        /* Boolean properties of the program */
64        bool can_discard;
65        bool writes_point_size;
66
67        int first_tag;
68
69        struct util_dynarray compiled;
70
71        /* For a blend shader using a constant color -- patch point. If
72         * negative, there's no constant. */
73
74        int blend_patch_offset;
75
76        /* IN: For a fragment shader with a lowered alpha test, the ref value */
77        float alpha_ref;
78} midgard_program;
79
80int
81midgard_compile_shader_nir(nir_shader *nir, midgard_program *program, bool is_blend);
82
83/* NIR options are shared between the standalone compiler and the online
84 * compiler. Defining it here is the simplest, though maybe not the Right
85 * solution. */
86
87static const nir_shader_compiler_options midgard_nir_options = {
88        .lower_ffma = true,
89        .lower_sub = true,
90        .lower_scmp = true,
91        .lower_flrp32 = true,
92        .lower_flrp64 = true,
93        .lower_ffract = true,
94        .lower_fmod32 = true,
95        .lower_fmod64 = true,
96        .lower_fdiv = true,
97        .lower_idiv = true,
98        .lower_isign = true,
99        .lower_fpow = true,
100        .lower_find_lsb = true,
101
102        /* TODO: We have native ops to help here, which we'll want to look into
103         * eventually */
104        .lower_fsign = true,
105
106        .vertex_id_zero_based = true,
107        .lower_extract_byte = true,
108        .lower_extract_word = true,
109
110        .native_integers = true
111};
112
113#endif
114