etnaviv: add etna_shader_key and generate variants if needed
[mesa.git] / src / gallium / drivers / etnaviv / etnaviv_context.c
1 /*
2 * Copyright (c) 2012-2015 Etnaviv Project
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, sub license,
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
12 * next paragraph) shall be included in all copies or substantial portions
13 * of the 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 NON-INFRINGEMENT. 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
21 * DEALINGS IN THE SOFTWARE.
22 *
23 * Authors:
24 * Wladimir J. van der Laan <laanwj@gmail.com>
25 * Christian Gmeiner <christian.gmeiner@gmail.com>
26 */
27
28 #include "etnaviv_context.h"
29
30 #include "etnaviv_blend.h"
31 #include "etnaviv_clear_blit.h"
32 #include "etnaviv_compiler.h"
33 #include "etnaviv_debug.h"
34 #include "etnaviv_emit.h"
35 #include "etnaviv_fence.h"
36 #include "etnaviv_query.h"
37 #include "etnaviv_rasterizer.h"
38 #include "etnaviv_screen.h"
39 #include "etnaviv_shader.h"
40 #include "etnaviv_state.h"
41 #include "etnaviv_surface.h"
42 #include "etnaviv_texture.h"
43 #include "etnaviv_transfer.h"
44 #include "etnaviv_translate.h"
45 #include "etnaviv_zsa.h"
46
47 #include "pipe/p_context.h"
48 #include "pipe/p_state.h"
49 #include "util/u_blitter.h"
50 #include "util/u_helpers.h"
51 #include "util/u_memory.h"
52 #include "util/u_prim.h"
53 #include "util/u_upload_mgr.h"
54
55 #include "hw/common.xml.h"
56
57 static void
58 etna_context_destroy(struct pipe_context *pctx)
59 {
60 struct etna_context *ctx = etna_context(pctx);
61
62 if (ctx->primconvert)
63 util_primconvert_destroy(ctx->primconvert);
64
65 if (ctx->blitter)
66 util_blitter_destroy(ctx->blitter);
67
68 if (pctx->stream_uploader)
69 u_upload_destroy(pctx->stream_uploader);
70
71 if (ctx->stream)
72 etna_cmd_stream_del(ctx->stream);
73
74 slab_destroy_child(&ctx->transfer_pool);
75
76 FREE(pctx);
77 }
78
79 /* Update render state where needed based on draw operation */
80 static void
81 etna_update_state_for_draw(struct etna_context *ctx, const struct pipe_draw_info *info)
82 {
83 /* Handle primitive restart:
84 * - If not an indexed draw, we don't care about the state of the primitive restart bit.
85 * - Otherwise, set the bit in INDEX_STREAM_CONTROL in the index buffer state
86 * accordingly
87 * - If the value of the INDEX_STREAM_CONTROL register changed due to this, or
88 * primitive restart is enabled and the restart index changed, mark the index
89 * buffer state as dirty
90 */
91
92 if (info->indexed) {
93 uint32_t new_control = ctx->index_buffer.FE_INDEX_STREAM_CONTROL;
94
95 if (info->primitive_restart)
96 new_control |= VIVS_FE_INDEX_STREAM_CONTROL_PRIMITIVE_RESTART;
97 else
98 new_control &= ~VIVS_FE_INDEX_STREAM_CONTROL_PRIMITIVE_RESTART;
99
100 if (ctx->index_buffer.FE_INDEX_STREAM_CONTROL != new_control ||
101 (info->primitive_restart && ctx->index_buffer.FE_PRIMITIVE_RESTART_INDEX != info->restart_index)) {
102 ctx->index_buffer.FE_INDEX_STREAM_CONTROL = new_control;
103 ctx->index_buffer.FE_PRIMITIVE_RESTART_INDEX = info->restart_index;
104 ctx->dirty |= ETNA_DIRTY_INDEX_BUFFER;
105 }
106 }
107 }
108
109 static bool
110 etna_get_vs(struct etna_context *ctx, struct etna_shader_key key)
111 {
112 const struct etna_shader_variant *old = ctx->shader.vs;
113
114 ctx->shader.vs = etna_shader_variant(ctx->shader.bind_vs, key, &ctx->debug);
115
116 if (!ctx->shader.vs)
117 return false;
118
119 if (old != ctx->shader.vs)
120 ctx->dirty |= ETNA_DIRTY_SHADER;
121
122 return true;
123 }
124
125 static bool
126 etna_get_fs(struct etna_context *ctx, struct etna_shader_key key)
127 {
128 const struct etna_shader_variant *old = ctx->shader.fs;
129
130 ctx->shader.fs = etna_shader_variant(ctx->shader.bind_fs, key, &ctx->debug);
131
132 if (!ctx->shader.fs)
133 return false;
134
135 if (old != ctx->shader.fs)
136 ctx->dirty |= ETNA_DIRTY_SHADER;
137
138 return true;
139 }
140
141 static void
142 etna_draw_vbo(struct pipe_context *pctx, const struct pipe_draw_info *info)
143 {
144 struct etna_context *ctx = etna_context(pctx);
145 struct pipe_framebuffer_state *pfb = &ctx->framebuffer_s;
146 uint32_t draw_mode;
147 unsigned i;
148
149 if (ctx->vertex_elements == NULL || ctx->vertex_elements->num_elements == 0)
150 return; /* Nothing to do */
151
152 if (!(ctx->prim_hwsupport & (1 << info->mode))) {
153 struct primconvert_context *primconvert = ctx->primconvert;
154 util_primconvert_save_index_buffer(primconvert, &ctx->index_buffer.ib);
155 util_primconvert_save_rasterizer_state(primconvert, ctx->rasterizer);
156 util_primconvert_draw_vbo(primconvert, info);
157 return;
158 }
159
160 int prims = u_decomposed_prims_for_vertices(info->mode, info->count);
161 if (unlikely(prims <= 0)) {
162 DBG("Invalid draw primitive mode=%i or no primitives to be drawn", info->mode);
163 return;
164 }
165
166 draw_mode = translate_draw_mode(info->mode);
167 if (draw_mode == ETNA_NO_MATCH) {
168 BUG("Unsupported draw mode");
169 return;
170 }
171
172 /* Upload a user index buffer. */
173 struct pipe_index_buffer ibuffer_saved = {};
174 if (info->indexed && ctx->index_buffer.ib.user_buffer &&
175 !util_save_and_upload_index_buffer(pctx, info, &ctx->index_buffer.ib,
176 &ibuffer_saved)) {
177 BUG("Index buffer upload failed.");
178 return;
179 }
180
181 if (info->indexed && !ctx->index_buffer.FE_INDEX_STREAM_BASE_ADDR.bo) {
182 BUG("Unsupported or no index buffer");
183 return;
184 }
185
186 struct etna_shader_key key = {};
187
188 if (!etna_get_vs(ctx, key) || !etna_get_fs(ctx, key)) {
189 BUG("compiled shaders are not okay");
190 return;
191 }
192
193 /* Update any derived state */
194 if (!etna_state_update(ctx))
195 return;
196
197 /*
198 * Figure out the buffers/features we need:
199 */
200 if (etna_depth_enabled(ctx))
201 resource_written(ctx, pfb->zsbuf->texture);
202
203 if (etna_stencil_enabled(ctx))
204 resource_written(ctx, pfb->zsbuf->texture);
205
206 for (i = 0; i < pfb->nr_cbufs; i++) {
207 struct pipe_resource *surf;
208
209 if (!pfb->cbufs[i])
210 continue;
211
212 surf = pfb->cbufs[i]->texture;
213 resource_written(ctx, surf);
214 }
215
216 /* Mark constant buffers as being read */
217 resource_read(ctx, ctx->constant_buffer[PIPE_SHADER_VERTEX].buffer);
218 resource_read(ctx, ctx->constant_buffer[PIPE_SHADER_FRAGMENT].buffer);
219
220 /* Mark VBOs as being read */
221 for (i = 0; i < ctx->vertex_buffer.count; i++) {
222 assert(!ctx->vertex_buffer.vb[i].user_buffer);
223 resource_read(ctx, ctx->vertex_buffer.vb[i].buffer);
224 }
225
226 /* Mark index buffer as being read */
227 resource_read(ctx, ctx->index_buffer.ib.buffer);
228
229 /* Mark textures as being read */
230 for (i = 0; i < PIPE_MAX_SAMPLERS; i++)
231 if (ctx->sampler_view[i])
232 resource_read(ctx, ctx->sampler_view[i]->texture);
233
234 ctx->stats.prims_emitted += u_reduced_prims_for_vertices(info->mode, info->count);
235 ctx->stats.draw_calls++;
236
237 /* Update state for this draw operation */
238 etna_update_state_for_draw(ctx, info);
239
240 /* First, sync state, then emit DRAW_PRIMITIVES or DRAW_INDEXED_PRIMITIVES */
241 etna_emit_state(ctx);
242
243 if (info->indexed)
244 etna_draw_indexed_primitives(ctx->stream, draw_mode, info->start, prims, info->index_bias);
245 else
246 etna_draw_primitives(ctx->stream, draw_mode, info->start, prims);
247
248 if (DBG_ENABLED(ETNA_DBG_DRAW_STALL)) {
249 /* Stall the FE after every draw operation. This allows better
250 * debug of GPU hang conditions, as the FE will indicate which
251 * draw op has caused the hang. */
252 etna_stall(ctx->stream, SYNC_RECIPIENT_FE, SYNC_RECIPIENT_PE);
253 }
254
255 if (DBG_ENABLED(ETNA_DBG_FLUSH_ALL))
256 pctx->flush(pctx, NULL, 0);
257
258 if (ctx->framebuffer.cbuf)
259 etna_resource(ctx->framebuffer.cbuf->texture)->seqno++;
260 if (ctx->framebuffer.zsbuf)
261 etna_resource(ctx->framebuffer.zsbuf->texture)->seqno++;
262 if (info->indexed && ibuffer_saved.user_buffer)
263 pctx->set_index_buffer(pctx, &ibuffer_saved);
264 }
265
266 static void
267 etna_flush(struct pipe_context *pctx, struct pipe_fence_handle **fence,
268 enum pipe_flush_flags flags)
269 {
270 struct etna_context *ctx = etna_context(pctx);
271
272 etna_cmd_stream_flush(ctx->stream);
273
274 if (fence)
275 *fence = etna_fence_create(pctx);
276 }
277
278 static void
279 etna_cmd_stream_reset_notify(struct etna_cmd_stream *stream, void *priv)
280 {
281 struct etna_context *ctx = priv;
282 struct etna_resource *rsc, *rsc_tmp;
283
284 etna_set_state(stream, VIVS_GL_API_MODE, VIVS_GL_API_MODE_OPENGL);
285 etna_set_state(stream, VIVS_GL_VERTEX_ELEMENT_CONFIG, 0x00000001);
286 etna_set_state(stream, VIVS_RA_EARLY_DEPTH, 0x00000031);
287 etna_set_state(stream, VIVS_PA_W_CLIP_LIMIT, 0x34000001);
288
289 ctx->dirty = ~0L;
290
291 /* go through all the used resources and clear their status flag */
292 LIST_FOR_EACH_ENTRY_SAFE(rsc, rsc_tmp, &ctx->used_resources, list)
293 {
294 debug_assert(rsc->status != 0);
295 rsc->status = 0;
296 rsc->pending_ctx = NULL;
297 list_delinit(&rsc->list);
298 }
299
300 assert(LIST_IS_EMPTY(&ctx->used_resources));
301 }
302
303 static void
304 etna_set_debug_callback(struct pipe_context *pctx,
305 const struct pipe_debug_callback *cb)
306 {
307 struct etna_context *ctx = etna_context(pctx);
308
309 if (cb)
310 ctx->debug = *cb;
311 else
312 memset(&ctx->debug, 0, sizeof(ctx->debug));
313 }
314
315 struct pipe_context *
316 etna_context_create(struct pipe_screen *pscreen, void *priv, unsigned flags)
317 {
318 struct etna_context *ctx = CALLOC_STRUCT(etna_context);
319 struct etna_screen *screen;
320 struct pipe_context *pctx;
321
322 if (ctx == NULL)
323 return NULL;
324
325 pctx = &ctx->base;
326 pctx->priv = ctx;
327 pctx->screen = pscreen;
328 pctx->stream_uploader = u_upload_create_default(pctx);
329 if (!pctx->stream_uploader)
330 goto fail;
331 pctx->const_uploader = pctx->stream_uploader;
332
333 screen = etna_screen(pscreen);
334 ctx->stream = etna_cmd_stream_new(screen->pipe, 0x2000, &etna_cmd_stream_reset_notify, ctx);
335 if (ctx->stream == NULL)
336 goto fail;
337
338 /* context ctxate setup */
339 ctx->specs = screen->specs;
340 ctx->screen = screen;
341 /* need some sane default in case state tracker doesn't set some state: */
342 ctx->sample_mask = 0xffff;
343
344 list_inithead(&ctx->used_resources);
345
346 /* Set sensible defaults for state */
347 etna_cmd_stream_reset_notify(ctx->stream, ctx);
348
349 pctx->destroy = etna_context_destroy;
350 pctx->draw_vbo = etna_draw_vbo;
351 pctx->flush = etna_flush;
352 pctx->set_debug_callback = etna_set_debug_callback;
353
354 /* creation of compile states */
355 pctx->create_blend_state = etna_blend_state_create;
356 pctx->create_rasterizer_state = etna_rasterizer_state_create;
357 pctx->create_depth_stencil_alpha_state = etna_zsa_state_create;
358
359 etna_clear_blit_init(pctx);
360 etna_query_context_init(pctx);
361 etna_state_init(pctx);
362 etna_surface_init(pctx);
363 etna_shader_init(pctx);
364 etna_texture_init(pctx);
365 etna_transfer_init(pctx);
366
367 ctx->blitter = util_blitter_create(pctx);
368 if (!ctx->blitter)
369 goto fail;
370
371 /* Generate the bitmask of supported draw primitives. */
372 ctx->prim_hwsupport = 1 << PIPE_PRIM_POINTS |
373 1 << PIPE_PRIM_LINES |
374 1 << PIPE_PRIM_LINE_STRIP |
375 1 << PIPE_PRIM_TRIANGLES |
376 1 << PIPE_PRIM_TRIANGLE_STRIP |
377 1 << PIPE_PRIM_TRIANGLE_FAN;
378
379 if (VIV_FEATURE(ctx->screen, chipMinorFeatures2, LINE_LOOP))
380 ctx->prim_hwsupport |= 1 << PIPE_PRIM_LINE_LOOP;
381
382 ctx->primconvert = util_primconvert_create(pctx, ctx->prim_hwsupport);
383 if (!ctx->primconvert)
384 goto fail;
385
386 slab_create_child(&ctx->transfer_pool, &screen->transfer_pool);
387
388 return pctx;
389
390 fail:
391 pctx->destroy(pctx);
392
393 return NULL;
394 }