swr: switch to using SwrGetInterface api table
[mesa.git] / src / gallium / drivers / swr / swr_context.h
1 /****************************************************************************
2 * Copyright (C) 2015 Intel Corporation. All Rights Reserved.
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
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 ***************************************************************************/
23
24 #ifndef SWR_CONTEXT_H
25 #define SWR_CONTEXT_H
26
27 #include "common/os.h"
28
29 #include "pipe/p_context.h"
30 #include "pipe/p_state.h"
31 #include "util/u_blitter.h"
32 #include "jit_api.h"
33 #include "swr_state.h"
34 #include <unordered_map>
35
36 #define SWR_NEW_BLEND (1 << 0)
37 #define SWR_NEW_RASTERIZER (1 << 1)
38 #define SWR_NEW_DEPTH_STENCIL_ALPHA (1 << 2)
39 #define SWR_NEW_SAMPLER (1 << 3)
40 #define SWR_NEW_SAMPLER_VIEW (1 << 4)
41 #define SWR_NEW_VS (1 << 5)
42 #define SWR_NEW_FS (1 << 6)
43 #define SWR_NEW_GS (1 << 7)
44 #define SWR_NEW_VSCONSTANTS (1 << 8)
45 #define SWR_NEW_FSCONSTANTS (1 << 9)
46 #define SWR_NEW_GSCONSTANTS (1 << 10)
47 #define SWR_NEW_VERTEX (1 << 11)
48 #define SWR_NEW_STIPPLE (1 << 12)
49 #define SWR_NEW_SCISSOR (1 << 13)
50 #define SWR_NEW_VIEWPORT (1 << 14)
51 #define SWR_NEW_FRAMEBUFFER (1 << 15)
52 #define SWR_NEW_CLIP (1 << 16)
53 #define SWR_NEW_SO (1 << 17)
54
55 namespace std
56 {
57 template <> struct hash<BLEND_COMPILE_STATE> {
58 std::size_t operator()(const BLEND_COMPILE_STATE &k) const
59 {
60 return util_hash_crc32(&k, sizeof(k));
61 }
62 };
63 };
64
65 struct swr_jit_texture {
66 uint32_t width; // same as number of elements
67 uint32_t height;
68 uint32_t depth; // doubles as array size
69 uint32_t first_level;
70 uint32_t last_level;
71 const uint8_t *base_ptr;
72 uint32_t row_stride[PIPE_MAX_TEXTURE_LEVELS];
73 uint32_t img_stride[PIPE_MAX_TEXTURE_LEVELS];
74 uint32_t mip_offsets[PIPE_MAX_TEXTURE_LEVELS];
75 };
76
77 struct swr_jit_sampler {
78 float min_lod;
79 float max_lod;
80 float lod_bias;
81 float border_color[4];
82 };
83
84 struct swr_draw_context {
85 const float *constantVS[PIPE_MAX_CONSTANT_BUFFERS];
86 uint32_t num_constantsVS[PIPE_MAX_CONSTANT_BUFFERS];
87 const float *constantFS[PIPE_MAX_CONSTANT_BUFFERS];
88 uint32_t num_constantsFS[PIPE_MAX_CONSTANT_BUFFERS];
89 const float *constantGS[PIPE_MAX_CONSTANT_BUFFERS];
90 uint32_t num_constantsGS[PIPE_MAX_CONSTANT_BUFFERS];
91
92 swr_jit_texture texturesVS[PIPE_MAX_SHADER_SAMPLER_VIEWS];
93 swr_jit_sampler samplersVS[PIPE_MAX_SAMPLERS];
94 swr_jit_texture texturesFS[PIPE_MAX_SHADER_SAMPLER_VIEWS];
95 swr_jit_sampler samplersFS[PIPE_MAX_SAMPLERS];
96 swr_jit_texture texturesGS[PIPE_MAX_SHADER_SAMPLER_VIEWS];
97 swr_jit_sampler samplersGS[PIPE_MAX_SAMPLERS];
98
99 float userClipPlanes[PIPE_MAX_CLIP_PLANES][4];
100
101 uint32_t polyStipple[32];
102
103 SWR_SURFACE_STATE renderTargets[SWR_NUM_ATTACHMENTS];
104 struct swr_query_result *pStats; // @llvm_struct
105 SWR_INTERFACE *pAPI; // @llvm_struct - Needed for the swr_memory callbacks
106 };
107
108 /* gen_llvm_types FINI */
109
110 struct swr_context {
111 struct pipe_context pipe; /**< base class */
112
113 HANDLE swrContext;
114
115 /** Constant state objects */
116 struct swr_blend_state *blend;
117 struct pipe_sampler_state *samplers[PIPE_SHADER_TYPES][PIPE_MAX_SAMPLERS];
118 struct pipe_depth_stencil_alpha_state *depth_stencil;
119 struct pipe_rasterizer_state *rasterizer;
120
121 struct swr_vertex_shader *vs;
122 struct swr_fragment_shader *fs;
123 struct swr_geometry_shader *gs;
124 struct swr_vertex_element_state *velems;
125
126 /** Other rendering state */
127 struct pipe_blend_color blend_color;
128 struct pipe_stencil_ref stencil_ref;
129 struct pipe_clip_state clip;
130 struct pipe_constant_buffer
131 constants[PIPE_SHADER_TYPES][PIPE_MAX_CONSTANT_BUFFERS];
132 struct pipe_framebuffer_state framebuffer;
133 struct swr_poly_stipple poly_stipple;
134 struct pipe_scissor_state scissor;
135 SWR_RECT swr_scissor;
136 struct pipe_sampler_view *
137 sampler_views[PIPE_SHADER_TYPES][PIPE_MAX_SHADER_SAMPLER_VIEWS];
138
139 struct pipe_viewport_state viewport;
140 struct pipe_vertex_buffer vertex_buffer[PIPE_MAX_ATTRIBS];
141
142 struct blitter_context *blitter;
143
144 /** Conditional query object and mode */
145 struct pipe_query *render_cond_query;
146 enum pipe_render_cond_flag render_cond_mode;
147 boolean render_cond_cond;
148 unsigned active_queries;
149
150 unsigned num_vertex_buffers;
151 unsigned num_samplers[PIPE_SHADER_TYPES];
152 unsigned num_sampler_views[PIPE_SHADER_TYPES];
153
154 unsigned sample_mask;
155
156 // streamout
157 pipe_stream_output_target *so_targets[MAX_SO_STREAMS];
158 uint32_t num_so_targets;
159
160 /* Temp storage for user_buffer constants */
161 struct swr_scratch_buffers *scratch;
162
163 // blend jit functions
164 std::unordered_map<BLEND_COMPILE_STATE, PFN_BLEND_JIT_FUNC> *blendJIT;
165
166 /* Derived SWR API DrawState */
167 struct swr_derived_state derived;
168
169 /* SWR private state - draw context */
170 struct swr_draw_context swrDC;
171
172 unsigned dirty; /**< Mask of SWR_NEW_x flags */
173
174 SWR_INTERFACE api;
175 };
176
177 static INLINE struct swr_context *
178 swr_context(struct pipe_context *pipe)
179 {
180 return (struct swr_context *)pipe;
181 }
182
183 static INLINE void
184 swr_update_draw_context(struct swr_context *ctx,
185 struct swr_query_result *pqr = nullptr)
186 {
187 swr_draw_context *pDC =
188 (swr_draw_context *)ctx->api.pfnSwrGetPrivateContextState(ctx->swrContext);
189 if (pqr)
190 ctx->swrDC.pStats = pqr;
191 memcpy(pDC, &ctx->swrDC, sizeof(swr_draw_context));
192 }
193
194 struct pipe_context *swr_create_context(struct pipe_screen *, void *priv, unsigned flags);
195
196 void swr_state_init(struct pipe_context *pipe);
197
198 void swr_clear_init(struct pipe_context *pipe);
199
200 void swr_draw_init(struct pipe_context *pipe);
201
202 void swr_finish(struct pipe_context *pipe);
203
204 void swr_do_msaa_resolve(struct pipe_resource *src_resource,
205 struct pipe_resource *dst_resource);
206 #endif