i965: Implement DispatchCompute() back-end
[mesa.git] / src / mesa / drivers / dri / i965 / brw_compute.c
1 /*
2 * Copyright © 2014 Intel Corporation
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
21 * DEALINGS IN THE SOFTWARE.
22 */
23
24 #include <sys/errno.h>
25
26 #include "main/condrender.h"
27 #include "main/glheader.h"
28 #include "main/mtypes.h"
29 #include "main/state.h"
30 #include "brw_context.h"
31 #include "brw_draw.h"
32 #include "brw_state.h"
33 #include "intel_batchbuffer.h"
34
35
36 static void
37 brw_emit_gpgpu_walker(struct brw_context *brw, const GLuint *num_groups)
38 {
39 _mesa_problem(&brw->ctx, "TODO: implement brw_emit_gpgpu_walker");
40 }
41
42
43 static void
44 brw_dispatch_compute(struct gl_context *ctx, const GLuint *num_groups)
45 {
46 struct brw_context *brw = brw_context(ctx);
47 int estimated_buffer_space_needed;
48 bool fail_next = false;
49
50 if (!_mesa_check_conditional_render(ctx))
51 return;
52
53 if (ctx->NewState)
54 _mesa_update_state(ctx);
55
56 brw_validate_textures(brw);
57
58 const int sampler_state_size = 16; /* 16 bytes */
59 estimated_buffer_space_needed = 512; /* batchbuffer commands */
60 estimated_buffer_space_needed += (BRW_MAX_TEX_UNIT *
61 (sampler_state_size +
62 sizeof(struct gen5_sampler_default_color)));
63 estimated_buffer_space_needed += 1024; /* push constants */
64 estimated_buffer_space_needed += 512; /* misc. pad */
65
66 /* Flush the batch if it's approaching full, so that we don't wrap while
67 * we've got validated state that needs to be in the same batch as the
68 * primitives.
69 */
70 intel_batchbuffer_require_space(brw, estimated_buffer_space_needed,
71 RENDER_RING);
72 intel_batchbuffer_save_state(brw);
73
74 retry:
75 brw->no_batch_wrap = true;
76 brw_upload_compute_state(brw);
77
78 brw_emit_gpgpu_walker(brw, num_groups);
79
80 brw->no_batch_wrap = false;
81
82 if (dri_bufmgr_check_aperture_space(&brw->batch.bo, 1)) {
83 if (!fail_next) {
84 intel_batchbuffer_reset_to_saved(brw);
85 intel_batchbuffer_flush(brw);
86 fail_next = true;
87 goto retry;
88 } else {
89 if (intel_batchbuffer_flush(brw) == -ENOSPC) {
90 static bool warned = false;
91
92 if (!warned) {
93 fprintf(stderr, "i965: Single compute shader dispatch "
94 "exceeded available aperture space\n");
95 warned = true;
96 }
97 }
98 }
99 }
100
101 /* Now that we know we haven't run out of aperture space, we can safely
102 * reset the dirty bits.
103 */
104 brw_compute_state_finished(brw);
105
106 if (brw->always_flush_batch)
107 intel_batchbuffer_flush(brw);
108
109 brw_state_cache_check_size(brw);
110
111 /* Note: since compute shaders can't write to framebuffers, there's no need
112 * to call brw_postdraw_set_buffers_need_resolve().
113 */
114 }
115
116
117 void
118 brw_init_compute_functions(struct dd_function_table *functions)
119 {
120 functions->DispatchCompute = brw_dispatch_compute;
121 }