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/mtypes.h"
28 #include "main/state.h"
29 #include "brw_context.h"
30 #include "brw_draw.h"
31 #include "brw_state.h"
32 #include "intel_batchbuffer.h"
33 #include "intel_buffer_objects.h"
34 #include "brw_defines.h"
35 
36 
37 static void
brw_dispatch_compute_common(struct gl_context * ctx)38 brw_dispatch_compute_common(struct gl_context *ctx)
39 {
40    struct brw_context *brw = brw_context(ctx);
41    bool fail_next;
42 
43    if (!_mesa_check_conditional_render(ctx))
44       return;
45 
46    if (ctx->NewState)
47       _mesa_update_state(ctx);
48 
49    brw_validate_textures(brw);
50 
51    brw_predraw_resolve_inputs(brw, false, NULL);
52 
53    /* Flush the batch if the batch/state buffers are nearly full.  We can
54     * grow them if needed, but this is not free, so we'd like to avoid it.
55     */
56    intel_batchbuffer_require_space(brw, 600);
57    brw_require_statebuffer_space(brw, 2500);
58    intel_batchbuffer_save_state(brw);
59    fail_next = intel_batchbuffer_saved_state_is_empty(brw);
60 
61  retry:
62    brw->batch.no_wrap = true;
63    brw_upload_compute_state(brw);
64 
65    brw->vtbl.emit_compute_walker(brw);
66 
67    brw->batch.no_wrap = false;
68 
69    if (!brw_batch_has_aperture_space(brw, 0)) {
70       if (!fail_next) {
71          intel_batchbuffer_reset_to_saved(brw);
72          intel_batchbuffer_flush(brw);
73          fail_next = true;
74          goto retry;
75       } else {
76          int ret = intel_batchbuffer_flush(brw);
77          WARN_ONCE(ret == -ENOSPC,
78                    "i965: Single compute shader dispatch "
79                    "exceeded available aperture space\n");
80       }
81    }
82 
83    /* Now that we know we haven't run out of aperture space, we can safely
84     * reset the dirty bits.
85     */
86    brw_compute_state_finished(brw);
87 
88    if (brw->always_flush_batch)
89       intel_batchbuffer_flush(brw);
90 
91    brw_program_cache_check_size(brw);
92 
93    /* Note: since compute shaders can't write to framebuffers, there's no need
94     * to call brw_postdraw_set_buffers_need_resolve().
95     */
96 }
97 
98 static void
brw_dispatch_compute(struct gl_context * ctx,const GLuint * num_groups)99 brw_dispatch_compute(struct gl_context *ctx, const GLuint *num_groups) {
100    struct brw_context *brw = brw_context(ctx);
101 
102    brw->compute.num_work_groups_bo = NULL;
103    brw->compute.num_work_groups = num_groups;
104    brw->compute.group_size = NULL;
105    ctx->NewDriverState |= BRW_NEW_CS_WORK_GROUPS;
106 
107    brw_dispatch_compute_common(ctx);
108 }
109 
110 static void
brw_dispatch_compute_indirect(struct gl_context * ctx,GLintptr indirect)111 brw_dispatch_compute_indirect(struct gl_context *ctx, GLintptr indirect)
112 {
113    struct brw_context *brw = brw_context(ctx);
114    static const GLuint indirect_group_counts[3] = { 0, 0, 0 };
115    struct gl_buffer_object *indirect_buffer = ctx->DispatchIndirectBuffer;
116    struct brw_bo *bo =
117       intel_bufferobj_buffer(brw,
118                              intel_buffer_object(indirect_buffer),
119                              indirect, 3 * sizeof(GLuint), false);
120 
121    brw->compute.num_work_groups_bo = bo;
122    brw->compute.num_work_groups_offset = indirect;
123    brw->compute.num_work_groups = indirect_group_counts;
124    brw->compute.group_size = NULL;
125    ctx->NewDriverState |= BRW_NEW_CS_WORK_GROUPS;
126 
127    brw_dispatch_compute_common(ctx);
128 }
129 
130 static void
brw_dispatch_compute_group_size(struct gl_context * ctx,const GLuint * num_groups,const GLuint * group_size)131 brw_dispatch_compute_group_size(struct gl_context *ctx,
132                                 const GLuint *num_groups,
133                                 const GLuint *group_size)
134 {
135    struct brw_context *brw = brw_context(ctx);
136 
137    brw->compute.num_work_groups_bo = NULL;
138    brw->compute.num_work_groups = num_groups;
139    brw->compute.group_size = group_size;
140    ctx->NewDriverState |= BRW_NEW_CS_WORK_GROUPS;
141 
142    brw_dispatch_compute_common(ctx);
143 }
144 
145 void
brw_init_compute_functions(struct dd_function_table * functions)146 brw_init_compute_functions(struct dd_function_table *functions)
147 {
148    functions->DispatchCompute = brw_dispatch_compute;
149    functions->DispatchComputeIndirect = brw_dispatch_compute_indirect;
150    functions->DispatchComputeGroupSize = brw_dispatch_compute_group_size;
151 }
152