2 * Copyright (c) 2014 - 2015 Intel Corporation
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:
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
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.
24 #include "util/ralloc.h"
25 #include "brw_context.h"
29 #include "brw_shader.h"
30 #include "intel_mipmap_tree.h"
31 #include "brw_state.h"
32 #include "intel_batchbuffer.h"
34 #include "brw_program.h"
35 #include "glsl/ir_uniform.h"
38 brw_cs_fill_local_id_payload(const struct brw_cs_prog_data
*prog_data
,
39 void *buffer
, uint32_t threads
, uint32_t stride
)
41 if (prog_data
->local_invocation_id_regs
== 0)
44 /* 'stride' should be an integer number of registers, that is, a multiple
47 assert(stride
% 32 == 0);
49 unsigned x
= 0, y
= 0, z
= 0;
50 for (unsigned t
= 0; t
< threads
; t
++) {
51 uint32_t *param
= (uint32_t *) buffer
+ stride
* t
/ 4;
53 for (unsigned i
= 0; i
< prog_data
->simd_size
; i
++) {
54 param
[0 * prog_data
->simd_size
+ i
] = x
;
55 param
[1 * prog_data
->simd_size
+ i
] = y
;
56 param
[2 * prog_data
->simd_size
+ i
] = z
;
59 if (x
== prog_data
->local_size
[0]) {
62 if (y
== prog_data
->local_size
[1]) {
65 if (z
== prog_data
->local_size
[2])
74 assign_cs_binding_table_offsets(const struct brw_device_info
*devinfo
,
75 const struct gl_shader_program
*shader_prog
,
76 const struct gl_program
*prog
,
77 struct brw_cs_prog_data
*prog_data
)
79 uint32_t next_binding_table_offset
= 0;
81 /* May not be used if the gl_NumWorkGroups variable is not accessed. */
82 prog_data
->binding_table
.work_groups_start
= next_binding_table_offset
;
83 next_binding_table_offset
++;
85 brw_assign_common_binding_table_offsets(MESA_SHADER_COMPUTE
, devinfo
,
86 shader_prog
, prog
, &prog_data
->base
,
87 next_binding_table_offset
);
91 brw_codegen_cs_prog(struct brw_context
*brw
,
92 struct gl_shader_program
*prog
,
93 struct brw_compute_program
*cp
,
94 struct brw_cs_prog_key
*key
)
96 struct gl_context
*ctx
= &brw
->ctx
;
97 const GLuint
*program
;
98 void *mem_ctx
= ralloc_context(NULL
);
100 struct brw_cs_prog_data prog_data
;
101 bool start_busy
= false;
102 double start_time
= 0;
104 struct brw_shader
*cs
=
105 (struct brw_shader
*) prog
->_LinkedShaders
[MESA_SHADER_COMPUTE
];
108 memset(&prog_data
, 0, sizeof(prog_data
));
110 assign_cs_binding_table_offsets(brw
->intelScreen
->devinfo
, prog
,
111 &cp
->program
.Base
, &prog_data
);
113 /* Allocate the references to the uniforms that will end up in the
114 * prog_data associated with the compiled program, and which will be freed
115 * by the state cache.
117 int param_count
= cp
->program
.Base
.nir
->num_uniforms
;
119 /* The backend also sometimes adds params for texture size. */
120 param_count
+= 2 * ctx
->Const
.Program
[MESA_SHADER_COMPUTE
].MaxTextureImageUnits
;
121 prog_data
.base
.param
=
122 rzalloc_array(NULL
, const gl_constant_value
*, param_count
);
123 prog_data
.base
.pull_param
=
124 rzalloc_array(NULL
, const gl_constant_value
*, param_count
);
125 prog_data
.base
.image_param
=
126 rzalloc_array(NULL
, struct brw_image_param
, cs
->base
.NumImages
);
127 prog_data
.base
.nr_params
= param_count
;
128 prog_data
.base
.nr_image_params
= cs
->base
.NumImages
;
130 brw_nir_setup_glsl_uniforms(cp
->program
.Base
.nir
, prog
, &cp
->program
.Base
,
131 &prog_data
.base
, true);
133 if (unlikely(brw
->perf_debug
)) {
134 start_busy
= (brw
->batch
.last_bo
&&
135 drm_intel_bo_busy(brw
->batch
.last_bo
));
136 start_time
= get_time();
139 if (unlikely(INTEL_DEBUG
& DEBUG_CS
))
140 brw_dump_ir("compute", prog
, &cs
->base
, &cp
->program
.Base
);
143 if (INTEL_DEBUG
& DEBUG_SHADER_TIME
)
144 st_index
= brw_get_shader_time_index(brw
, prog
, &cp
->program
.Base
, ST_CS
);
147 program
= brw_compile_cs(brw
->intelScreen
->compiler
, brw
, mem_ctx
,
148 key
, &prog_data
, cp
->program
.Base
.nir
,
149 st_index
, &program_size
, &error_str
);
150 if (program
== NULL
) {
151 prog
->LinkStatus
= false;
152 ralloc_strcat(&prog
->InfoLog
, error_str
);
153 _mesa_problem(NULL
, "Failed to compile compute shader: %s\n", error_str
);
155 ralloc_free(mem_ctx
);
159 if (unlikely(brw
->perf_debug
) && cs
) {
160 if (cs
->compiled_once
) {
161 _mesa_problem(&brw
->ctx
, "CS programs shouldn't need recompiles");
163 cs
->compiled_once
= true;
165 if (start_busy
&& !drm_intel_bo_busy(brw
->batch
.last_bo
)) {
166 perf_debug("CS compile took %.03f ms and stalled the GPU\n",
167 (get_time() - start_time
) * 1000);
171 if (prog_data
.base
.total_scratch
) {
172 brw_get_scratch_bo(brw
, &brw
->cs
.base
.scratch_bo
,
173 prog_data
.base
.total_scratch
* brw
->max_cs_threads
);
176 if (unlikely(INTEL_DEBUG
& DEBUG_CS
))
177 fprintf(stderr
, "\n");
179 brw_upload_cache(&brw
->cache
, BRW_CACHE_CS_PROG
,
181 program
, program_size
,
182 &prog_data
, sizeof(prog_data
),
183 &brw
->cs
.base
.prog_offset
, &brw
->cs
.prog_data
);
184 ralloc_free(mem_ctx
);
191 brw_cs_populate_key(struct brw_context
*brw
, struct brw_cs_prog_key
*key
)
193 struct gl_context
*ctx
= &brw
->ctx
;
194 /* BRW_NEW_COMPUTE_PROGRAM */
195 const struct brw_compute_program
*cp
=
196 (struct brw_compute_program
*) brw
->compute_program
;
197 const struct gl_program
*prog
= (struct gl_program
*) cp
;
199 memset(key
, 0, sizeof(*key
));
202 brw_populate_sampler_prog_key_data(ctx
, prog
, brw
->cs
.base
.sampler_count
,
205 /* The unique compute program ID */
206 key
->program_string_id
= cp
->id
;
211 brw_upload_cs_prog(struct brw_context
*brw
)
213 struct gl_context
*ctx
= &brw
->ctx
;
214 struct brw_cs_prog_key key
;
215 struct brw_compute_program
*cp
= (struct brw_compute_program
*)
216 brw
->compute_program
;
221 if (!brw_state_dirty(brw
, _NEW_TEXTURE
, BRW_NEW_COMPUTE_PROGRAM
))
224 brw
->cs
.base
.sampler_count
=
225 _mesa_fls(ctx
->ComputeProgram
._Current
->Base
.SamplersUsed
);
227 brw_cs_populate_key(brw
, &key
);
229 if (!brw_search_cache(&brw
->cache
, BRW_CACHE_CS_PROG
,
231 &brw
->cs
.base
.prog_offset
, &brw
->cs
.prog_data
)) {
233 brw_codegen_cs_prog(brw
,
234 ctx
->Shader
.CurrentProgram
[MESA_SHADER_COMPUTE
],
239 brw
->cs
.base
.prog_data
= &brw
->cs
.prog_data
->base
;
244 brw_cs_precompile(struct gl_context
*ctx
,
245 struct gl_shader_program
*shader_prog
,
246 struct gl_program
*prog
)
248 struct brw_context
*brw
= brw_context(ctx
);
249 struct brw_cs_prog_key key
;
251 struct gl_compute_program
*cp
= (struct gl_compute_program
*) prog
;
252 struct brw_compute_program
*bcp
= brw_compute_program(cp
);
254 memset(&key
, 0, sizeof(key
));
255 key
.program_string_id
= bcp
->id
;
257 brw_setup_tex_for_precompile(brw
, &key
.tex
, prog
);
259 uint32_t old_prog_offset
= brw
->cs
.base
.prog_offset
;
260 struct brw_cs_prog_data
*old_prog_data
= brw
->cs
.prog_data
;
262 bool success
= brw_codegen_cs_prog(brw
, shader_prog
, bcp
, &key
);
264 brw
->cs
.base
.prog_offset
= old_prog_offset
;
265 brw
->cs
.prog_data
= old_prog_data
;