aboutsummaryrefslogtreecommitdiffstats
path: root/src/gallium/drivers/swr/swr_state.h
blob: cb69964127476a8af72e10426c31fd97fac5fd00 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
/****************************************************************************
 * Copyright (C) 2015 Intel Corporation.   All Rights Reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 * IN THE SOFTWARE.
 ***************************************************************************/

#ifndef SWR_STATE_H
#define SWR_STATE_H

#include "pipe/p_defines.h"
#include "tgsi/tgsi_scan.h"
#include "tgsi/tgsi_parse.h"
#include "tgsi/tgsi_dump.h"
#include "gallivm/lp_bld_init.h"
#include "gallivm/lp_bld_tgsi.h"
#include "util/u_hash.h"
#include "api.h"
#include "swr_tex_sample.h"
#include "swr_shader.h"
#include <unordered_map>
#include <memory>

template <typename T>
struct ShaderVariant {
   struct gallivm_state *gallivm;
   T shader;

   ShaderVariant(struct gallivm_state *gs, T code) : gallivm(gs), shader(code) {}
   ~ShaderVariant() { gallivm_destroy(gallivm); }
};

typedef ShaderVariant<PFN_VERTEX_FUNC> VariantVS;
typedef ShaderVariant<PFN_PIXEL_KERNEL> VariantFS;

/* skeleton */
struct swr_vertex_shader {
   struct pipe_shader_state pipe;
   struct lp_tgsi_info info;
   unsigned linkageMask;
   std::unordered_map<swr_jit_vs_key, std::unique_ptr<VariantVS>> map;
   SWR_STREAMOUT_STATE soState;
   PFN_SO_FUNC soFunc[PIPE_PRIM_MAX] {0};
};

struct swr_fragment_shader {
   struct pipe_shader_state pipe;
   struct lp_tgsi_info info;
   uint32_t constantMask;
   uint32_t flatConstantMask;
   uint32_t pointSpriteMask;
   std::unordered_map<swr_jit_fs_key, std::unique_ptr<VariantFS>> map;
};

/* Vertex element state */
struct swr_vertex_element_state {
   FETCH_COMPILE_STATE fsState;
   PFN_FETCH_FUNC fsFunc;
   uint32_t stream_pitch[PIPE_MAX_ATTRIBS];
};

struct swr_blend_state {
   struct pipe_blend_state pipe;
   SWR_BLEND_STATE blendState;
   RENDER_TARGET_BLEND_COMPILE_STATE compileState[PIPE_MAX_COLOR_BUFS];
};

/*
 * Derived SWR API DrawState
 * For convenience of making simple changes without re-deriving state.
 */
struct swr_derived_state {
   SWR_RASTSTATE rastState;
   SWR_VIEWPORT vp;
   SWR_VIEWPORT_MATRIX vpm;
};

void swr_update_derived(struct pipe_context *,
                        const struct pipe_draw_info * = nullptr);

/*
 * Conversion functions: Convert mesa state defines to SWR.
 */

static INLINE SWR_LOGIC_OP
swr_convert_logic_op(const UINT op)
{
   switch (op) {
   case PIPE_LOGICOP_CLEAR:
      return LOGICOP_CLEAR;
   case PIPE_LOGICOP_NOR:
      return LOGICOP_NOR;
   case PIPE_LOGICOP_AND_INVERTED:
      return LOGICOP_CLEAR;
   case PIPE_LOGICOP_COPY_INVERTED:
      return LOGICOP_COPY_INVERTED;
   case PIPE_LOGICOP_AND_REVERSE:
      return LOGICOP_AND_REVERSE;
   case PIPE_LOGICOP_INVERT:
      return LOGICOP_INVERT;
   case PIPE_LOGICOP_XOR:
      return LOGICOP_XOR;
   case PIPE_LOGICOP_NAND:
      return LOGICOP_NAND;
   case PIPE_LOGICOP_AND:
      return LOGICOP_AND;
   case PIPE_LOGICOP_EQUIV:
      return LOGICOP_EQUIV;
   case PIPE_LOGICOP_NOOP:
      return LOGICOP_NOOP;
   case PIPE_LOGICOP_OR_INVERTED:
      return LOGICOP_OR_INVERTED;
   case PIPE_LOGICOP_COPY:
      return LOGICOP_COPY;
   case PIPE_LOGICOP_OR_REVERSE:
      return LOGICOP_OR_REVERSE;
   case PIPE_LOGICOP_OR:
      return LOGICOP_OR;
   case PIPE_LOGICOP_SET:
      return LOGICOP_SET;
   default:
      assert(0 && "Unsupported logic op");
      return LOGICOP_NOOP;
   }
}

static INLINE SWR_STENCILOP
swr_convert_stencil_op(const UINT op)
{
   switch (op) {
   case PIPE_STENCIL_OP_KEEP:
      return STENCILOP_KEEP;
   case PIPE_STENCIL_OP_ZERO:
      return STENCILOP_ZERO;
   case PIPE_STENCIL_OP_REPLACE:
      return STENCILOP_REPLACE;
   case PIPE_STENCIL_OP_INCR:
      return STENCILOP_INCRSAT;
   case PIPE_STENCIL_OP_DECR:
      return STENCILOP_DECRSAT;
   case PIPE_STENCIL_OP_INCR_WRAP:
      return STENCILOP_INCR;
   case PIPE_STENCIL_OP_DECR_WRAP:
      return STENCILOP_DECR;
   case PIPE_STENCIL_OP_INVERT:
      return STENCILOP_INVERT;
   default:
      assert(0 && "Unsupported stencil op");
      return STENCILOP_KEEP;
   }
}

static INLINE SWR_FORMAT
swr_convert_index_type(const UINT index_size)
{
   switch (index_size) {
   case sizeof(unsigned char):
      return R8_UINT;
   case sizeof(unsigned short):
      return R16_UINT;
   case sizeof(unsigned int):
      return R32_UINT;
   default:
      assert(0 && "Unsupported index type");
      return R32_UINT;
   }
}


static INLINE SWR_ZFUNCTION
swr_convert_depth_func(const UINT pipe_func)
{
   switch (pipe_func) {
   case PIPE_FUNC_NEVER:
      return ZFUNC_NEVER;
   case PIPE_FUNC_LESS:
      return ZFUNC_LT;
   case PIPE_FUNC_EQUAL:
      return ZFUNC_EQ;
   case PIPE_FUNC_LEQUAL:
      return ZFUNC_LE;
   case PIPE_FUNC_GREATER:
      return ZFUNC_GT;
   case PIPE_FUNC_NOTEQUAL:
      return ZFUNC_NE;
   case PIPE_FUNC_GEQUAL:
      return ZFUNC_GE;
   case PIPE_FUNC_ALWAYS:
      return ZFUNC_ALWAYS;
   default:
      assert(0 && "Unsupported depth func");
      return ZFUNC_ALWAYS;
   }
}


static INLINE SWR_CULLMODE
swr_convert_cull_mode(const UINT cull_face)
{
   switch (cull_face) {
   case PIPE_FACE_NONE:
      return SWR_CULLMODE_NONE;
   case PIPE_FACE_FRONT:
      return SWR_CULLMODE_FRONT;
   case PIPE_FACE_BACK:
      return SWR_CULLMODE_BACK;
   case PIPE_FACE_FRONT_AND_BACK:
      return SWR_CULLMODE_BOTH;
   default:
      assert(0 && "Invalid cull mode");
      return SWR_CULLMODE_NONE;
   }
}

static INLINE SWR_BLEND_OP
swr_convert_blend_func(const UINT blend_func)
{
   switch (blend_func) {
   case PIPE_BLEND_ADD:
      return BLENDOP_ADD;
   case PIPE_BLEND_SUBTRACT:
      return BLENDOP_SUBTRACT;
   case PIPE_BLEND_REVERSE_SUBTRACT:
      return BLENDOP_REVSUBTRACT;
   case PIPE_BLEND_MIN:
      return BLENDOP_MIN;
   case PIPE_BLEND_MAX:
      return BLENDOP_MAX;
   default:
      assert(0 && "Invalid blend func");
      return BLENDOP_ADD;
   }
}

static INLINE SWR_BLEND_FACTOR
swr_convert_blend_factor(const UINT blend_factor)
{
   switch (blend_factor) {
   case PIPE_BLENDFACTOR_ONE:
      return BLENDFACTOR_ONE;
   case PIPE_BLENDFACTOR_SRC_COLOR:
      return BLENDFACTOR_SRC_COLOR;
   case PIPE_BLENDFACTOR_SRC_ALPHA:
      return BLENDFACTOR_SRC_ALPHA;
   case PIPE_BLENDFACTOR_DST_ALPHA:
      return BLENDFACTOR_DST_ALPHA;
   case PIPE_BLENDFACTOR_DST_COLOR:
      return BLENDFACTOR_DST_COLOR;
   case PIPE_BLENDFACTOR_SRC_ALPHA_SATURATE:
      return BLENDFACTOR_SRC_ALPHA_SATURATE;
   case PIPE_BLENDFACTOR_CONST_COLOR:
      return BLENDFACTOR_CONST_COLOR;
   case PIPE_BLENDFACTOR_CONST_ALPHA:
      return BLENDFACTOR_CONST_ALPHA;
   case PIPE_BLENDFACTOR_SRC1_COLOR:
      return BLENDFACTOR_SRC1_COLOR;
   case PIPE_BLENDFACTOR_SRC1_ALPHA:
      return BLENDFACTOR_SRC1_ALPHA;
   case PIPE_BLENDFACTOR_ZERO:
      return BLENDFACTOR_ZERO;
   case PIPE_BLENDFACTOR_INV_SRC_COLOR:
      return BLENDFACTOR_INV_SRC_COLOR;
   case PIPE_BLENDFACTOR_INV_SRC_ALPHA:
      return BLENDFACTOR_INV_SRC_ALPHA;
   case PIPE_BLENDFACTOR_INV_DST_ALPHA:
      return BLENDFACTOR_INV_DST_ALPHA;
   case PIPE_BLENDFACTOR_INV_DST_COLOR:
      return BLENDFACTOR_INV_DST_COLOR;
   case PIPE_BLENDFACTOR_INV_CONST_COLOR:
      return BLENDFACTOR_INV_CONST_COLOR;
   case PIPE_BLENDFACTOR_INV_CONST_ALPHA:
      return BLENDFACTOR_INV_CONST_ALPHA;
   case PIPE_BLENDFACTOR_INV_SRC1_COLOR:
      return BLENDFACTOR_INV_SRC1_COLOR;
   case PIPE_BLENDFACTOR_INV_SRC1_ALPHA:
      return BLENDFACTOR_INV_SRC1_ALPHA;
   default:
      assert(0 && "Invalid blend factor");
      return BLENDFACTOR_ONE;
   }
}

static INLINE enum SWR_SURFACE_TYPE
swr_convert_target_type(const enum pipe_texture_target target)
{
   switch (target) {
   case PIPE_BUFFER:
      return SURFACE_BUFFER;
   case PIPE_TEXTURE_1D:
   case PIPE_TEXTURE_1D_ARRAY:
      return SURFACE_1D;
   case PIPE_TEXTURE_2D:
   case PIPE_TEXTURE_2D_ARRAY:
   case PIPE_TEXTURE_RECT:
      return SURFACE_2D;
   case PIPE_TEXTURE_3D:
      return SURFACE_3D;
   case PIPE_TEXTURE_CUBE:
   case PIPE_TEXTURE_CUBE_ARRAY:
      return SURFACE_CUBE;
   default:
      assert(0);
      return SURFACE_NULL;
   }
}
#endif