• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2014 - 2015 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 "util/ralloc.h"
25 #include "brw_context.h"
26 #include "brw_cs.h"
27 #include "brw_wm.h"
28 #include "brw_mipmap_tree.h"
29 #include "brw_state.h"
30 #include "brw_batch.h"
31 #include "compiler/brw_nir.h"
32 #include "brw_program.h"
33 #include "compiler/glsl/ir_uniform.h"
34 
35 static void
assign_cs_binding_table_offsets(const struct intel_device_info * devinfo,const struct gl_program * prog,struct brw_cs_prog_data * prog_data)36 assign_cs_binding_table_offsets(const struct intel_device_info *devinfo,
37                                 const struct gl_program *prog,
38                                 struct brw_cs_prog_data *prog_data)
39 {
40    uint32_t next_binding_table_offset = 0;
41 
42    /* May not be used if the gl_NumWorkGroups variable is not accessed. */
43    prog_data->binding_table.work_groups_start = next_binding_table_offset;
44    next_binding_table_offset++;
45 
46    brw_assign_common_binding_table_offsets(devinfo, prog, &prog_data->base,
47                                            next_binding_table_offset);
48 }
49 
50 static bool
brw_codegen_cs_prog(struct brw_context * brw,struct brw_program * cp,struct brw_cs_prog_key * key)51 brw_codegen_cs_prog(struct brw_context *brw,
52                     struct brw_program *cp,
53                     struct brw_cs_prog_key *key)
54 {
55    const struct intel_device_info *devinfo = &brw->screen->devinfo;
56    const GLuint *program;
57    void *mem_ctx = ralloc_context(NULL);
58    struct brw_cs_prog_data prog_data;
59    bool start_busy = false;
60    double start_time = 0;
61    nir_shader *nir = nir_shader_clone(mem_ctx, cp->program.nir);
62 
63    memset(&prog_data, 0, sizeof(prog_data));
64 
65    if (cp->program.info.shared_size > 64 * 1024) {
66       cp->program.sh.data->LinkStatus = LINKING_FAILURE;
67       const char *error_str =
68          "Compute shader used more than 64KB of shared variables";
69       ralloc_strcat(&cp->program.sh.data->InfoLog, error_str);
70       _mesa_problem(NULL, "Failed to link compute shader: %s\n", error_str);
71 
72       ralloc_free(mem_ctx);
73       return false;
74    }
75 
76    assign_cs_binding_table_offsets(devinfo, &cp->program, &prog_data);
77 
78    brw_nir_setup_glsl_uniforms(mem_ctx, nir,
79                                &cp->program, &prog_data.base, true);
80 
81    if (unlikely(brw->perf_debug)) {
82       start_busy = (brw->batch.last_bo &&
83                     brw_bo_busy(brw->batch.last_bo));
84       start_time = get_time();
85    }
86 
87 
88    brw_nir_lower_cs_intrinsics(nir);
89 
90    struct brw_compile_cs_params params = {
91       .nir = nir,
92       .key = key,
93       .prog_data = &prog_data,
94       .log_data = brw,
95    };
96 
97    if (INTEL_DEBUG(DEBUG_SHADER_TIME)) {
98       params.shader_time = true;
99       params.shader_time_index =
100          brw_get_shader_time_index(brw, &cp->program, ST_CS, true);
101    }
102 
103    program = brw_compile_cs(brw->screen->compiler, mem_ctx, &params);
104    if (program == NULL) {
105       cp->program.sh.data->LinkStatus = LINKING_FAILURE;
106       ralloc_strcat(&cp->program.sh.data->InfoLog, params.error_str);
107       _mesa_problem(NULL, "Failed to compile compute shader: %s\n", params.error_str);
108 
109       ralloc_free(mem_ctx);
110       return false;
111    }
112 
113    if (unlikely(brw->perf_debug)) {
114       if (cp->compiled_once) {
115          brw_debug_recompile(brw, MESA_SHADER_COMPUTE, cp->program.Id,
116                              &key->base);
117       }
118       cp->compiled_once = true;
119 
120       if (start_busy && !brw_bo_busy(brw->batch.last_bo)) {
121          perf_debug("CS compile took %.03f ms and stalled the GPU\n",
122                     (get_time() - start_time) * 1000);
123       }
124    }
125 
126    brw_alloc_stage_scratch(brw, &brw->cs.base, prog_data.base.total_scratch);
127 
128    /* The param and pull_param arrays will be freed by the shader cache. */
129    ralloc_steal(NULL, prog_data.base.param);
130    ralloc_steal(NULL, prog_data.base.pull_param);
131    brw_upload_cache(&brw->cache, BRW_CACHE_CS_PROG,
132                     key, sizeof(*key),
133                     program, prog_data.base.program_size,
134                     &prog_data, sizeof(prog_data),
135                     &brw->cs.base.prog_offset, &brw->cs.base.prog_data);
136    ralloc_free(mem_ctx);
137 
138    return true;
139 }
140 
141 
142 void
brw_cs_populate_key(struct brw_context * brw,struct brw_cs_prog_key * key)143 brw_cs_populate_key(struct brw_context *brw, struct brw_cs_prog_key *key)
144 {
145    struct gl_context *ctx = &brw->ctx;
146    /* BRW_NEW_COMPUTE_PROGRAM */
147    const struct brw_program *cp =
148       (struct brw_program *) brw->programs[MESA_SHADER_COMPUTE];
149 
150    memset(key, 0, sizeof(*key));
151 
152    /* _NEW_TEXTURE */
153    brw_populate_base_prog_key(ctx, cp, &key->base);
154 }
155 
156 
157 void
brw_upload_cs_prog(struct brw_context * brw)158 brw_upload_cs_prog(struct brw_context *brw)
159 {
160    struct gl_context *ctx = &brw->ctx;
161    struct brw_cs_prog_key key;
162    struct brw_program *cp =
163       (struct brw_program *) brw->programs[MESA_SHADER_COMPUTE];
164 
165    if (!cp)
166       return;
167 
168    if (!brw_state_dirty(brw, _NEW_TEXTURE, BRW_NEW_COMPUTE_PROGRAM))
169       return;
170 
171    brw->cs.base.sampler_count =
172       util_last_bit(ctx->ComputeProgram._Current->SamplersUsed);
173 
174    brw_cs_populate_key(brw, &key);
175 
176    if (brw_search_cache(&brw->cache, BRW_CACHE_CS_PROG, &key, sizeof(key),
177                         &brw->cs.base.prog_offset, &brw->cs.base.prog_data,
178                         true))
179       return;
180 
181    if (brw_disk_cache_upload_program(brw, MESA_SHADER_COMPUTE))
182       return;
183 
184    cp = (struct brw_program *) brw->programs[MESA_SHADER_COMPUTE];
185    cp->id = key.base.program_string_id;
186 
187    ASSERTED bool success = brw_codegen_cs_prog(brw, cp, &key);
188    assert(success);
189 }
190 
191 void
brw_cs_populate_default_key(const struct brw_compiler * compiler,struct brw_cs_prog_key * key,struct gl_program * prog)192 brw_cs_populate_default_key(const struct brw_compiler *compiler,
193                             struct brw_cs_prog_key *key,
194                             struct gl_program *prog)
195 {
196    const struct intel_device_info *devinfo = compiler->devinfo;
197    memset(key, 0, sizeof(*key));
198    brw_populate_default_base_prog_key(devinfo, brw_program(prog), &key->base);
199 }
200 
201 bool
brw_cs_precompile(struct gl_context * ctx,struct gl_program * prog)202 brw_cs_precompile(struct gl_context *ctx, struct gl_program *prog)
203 {
204    struct brw_context *brw = brw_context(ctx);
205    struct brw_cs_prog_key key;
206 
207    struct brw_program *bcp = brw_program(prog);
208 
209    brw_cs_populate_default_key(brw->screen->compiler, &key, prog);
210 
211    uint32_t old_prog_offset = brw->cs.base.prog_offset;
212    struct brw_stage_prog_data *old_prog_data = brw->cs.base.prog_data;
213 
214    bool success = brw_codegen_cs_prog(brw, bcp, &key);
215 
216    brw->cs.base.prog_offset = old_prog_offset;
217    brw->cs.base.prog_data = old_prog_data;
218 
219    return success;
220 }
221