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"
36 assign_cs_binding_table_offsets(const struct brw_device_info
*devinfo
,
37 const struct gl_shader_program
*shader_prog
,
38 const struct gl_program
*prog
,
39 struct brw_cs_prog_data
*prog_data
)
41 uint32_t next_binding_table_offset
= 0;
43 /* May not be used if the gl_NumWorkGroups variable is not accessed. */
44 prog_data
->binding_table
.work_groups_start
= next_binding_table_offset
;
45 next_binding_table_offset
++;
47 brw_assign_common_binding_table_offsets(MESA_SHADER_COMPUTE
, devinfo
,
48 shader_prog
, prog
, &prog_data
->base
,
49 next_binding_table_offset
);
53 brw_codegen_cs_prog(struct brw_context
*brw
,
54 struct gl_shader_program
*prog
,
55 struct brw_compute_program
*cp
,
56 struct brw_cs_prog_key
*key
)
58 struct gl_context
*ctx
= &brw
->ctx
;
59 const GLuint
*program
;
60 void *mem_ctx
= ralloc_context(NULL
);
62 struct brw_cs_prog_data prog_data
;
63 bool start_busy
= false;
64 double start_time
= 0;
66 struct brw_shader
*cs
=
67 (struct brw_shader
*) prog
->_LinkedShaders
[MESA_SHADER_COMPUTE
];
70 memset(&prog_data
, 0, sizeof(prog_data
));
72 assign_cs_binding_table_offsets(brw
->intelScreen
->devinfo
, prog
,
73 &cp
->program
.Base
, &prog_data
);
75 /* Allocate the references to the uniforms that will end up in the
76 * prog_data associated with the compiled program, and which will be freed
79 int param_count
= cp
->program
.Base
.nir
->num_uniforms
;
81 /* The backend also sometimes adds params for texture size. */
82 param_count
+= 2 * ctx
->Const
.Program
[MESA_SHADER_COMPUTE
].MaxTextureImageUnits
;
83 prog_data
.base
.param
=
84 rzalloc_array(NULL
, const gl_constant_value
*, param_count
);
85 prog_data
.base
.pull_param
=
86 rzalloc_array(NULL
, const gl_constant_value
*, param_count
);
87 prog_data
.base
.image_param
=
88 rzalloc_array(NULL
, struct brw_image_param
, cs
->base
.NumImages
);
89 prog_data
.base
.nr_params
= param_count
;
90 prog_data
.base
.nr_image_params
= cs
->base
.NumImages
;
92 brw_nir_setup_glsl_uniforms(cp
->program
.Base
.nir
, prog
, &cp
->program
.Base
,
93 &prog_data
.base
, true);
95 if (unlikely(brw
->perf_debug
)) {
96 start_busy
= (brw
->batch
.last_bo
&&
97 drm_intel_bo_busy(brw
->batch
.last_bo
));
98 start_time
= get_time();
101 program
= brw_cs_emit(brw
, mem_ctx
, key
, &prog_data
,
102 &cp
->program
, prog
, &program_size
);
103 if (program
== NULL
) {
104 ralloc_free(mem_ctx
);
108 if (unlikely(brw
->perf_debug
) && cs
) {
109 if (cs
->compiled_once
) {
110 _mesa_problem(&brw
->ctx
, "CS programs shouldn't need recompiles");
112 cs
->compiled_once
= true;
114 if (start_busy
&& !drm_intel_bo_busy(brw
->batch
.last_bo
)) {
115 perf_debug("CS compile took %.03f ms and stalled the GPU\n",
116 (get_time() - start_time
) * 1000);
120 if (prog_data
.base
.total_scratch
) {
121 brw_get_scratch_bo(brw
, &brw
->cs
.base
.scratch_bo
,
122 prog_data
.base
.total_scratch
* brw
->max_cs_threads
);
125 if (unlikely(INTEL_DEBUG
& DEBUG_CS
))
126 fprintf(stderr
, "\n");
128 brw_upload_cache(&brw
->cache
, BRW_CACHE_CS_PROG
,
130 program
, program_size
,
131 &prog_data
, sizeof(prog_data
),
132 &brw
->cs
.base
.prog_offset
, &brw
->cs
.prog_data
);
133 ralloc_free(mem_ctx
);
140 brw_cs_populate_key(struct brw_context
*brw
, struct brw_cs_prog_key
*key
)
142 struct gl_context
*ctx
= &brw
->ctx
;
143 /* BRW_NEW_COMPUTE_PROGRAM */
144 const struct brw_compute_program
*cp
=
145 (struct brw_compute_program
*) brw
->compute_program
;
146 const struct gl_program
*prog
= (struct gl_program
*) cp
;
148 memset(key
, 0, sizeof(*key
));
151 brw_populate_sampler_prog_key_data(ctx
, prog
, brw
->cs
.base
.sampler_count
,
154 /* The unique compute program ID */
155 key
->program_string_id
= cp
->id
;
160 brw_upload_cs_prog(struct brw_context
*brw
)
162 struct gl_context
*ctx
= &brw
->ctx
;
163 struct brw_cs_prog_key key
;
164 struct brw_compute_program
*cp
= (struct brw_compute_program
*)
165 brw
->compute_program
;
170 if (!brw_state_dirty(brw
, _NEW_TEXTURE
, BRW_NEW_COMPUTE_PROGRAM
))
173 brw
->cs
.base
.sampler_count
=
174 _mesa_fls(ctx
->ComputeProgram
._Current
->Base
.SamplersUsed
);
176 brw_cs_populate_key(brw
, &key
);
178 if (!brw_search_cache(&brw
->cache
, BRW_CACHE_CS_PROG
,
180 &brw
->cs
.base
.prog_offset
, &brw
->cs
.prog_data
)) {
182 brw_codegen_cs_prog(brw
,
183 ctx
->Shader
.CurrentProgram
[MESA_SHADER_COMPUTE
],
188 brw
->cs
.base
.prog_data
= &brw
->cs
.prog_data
->base
;
193 brw_cs_precompile(struct gl_context
*ctx
,
194 struct gl_shader_program
*shader_prog
,
195 struct gl_program
*prog
)
197 struct brw_context
*brw
= brw_context(ctx
);
198 struct brw_cs_prog_key key
;
200 struct gl_compute_program
*cp
= (struct gl_compute_program
*) prog
;
201 struct brw_compute_program
*bcp
= brw_compute_program(cp
);
203 memset(&key
, 0, sizeof(key
));
204 key
.program_string_id
= bcp
->id
;
206 brw_setup_tex_for_precompile(brw
, &key
.tex
, prog
);
208 uint32_t old_prog_offset
= brw
->cs
.base
.prog_offset
;
209 struct brw_cs_prog_data
*old_prog_data
= brw
->cs
.prog_data
;
211 bool success
= brw_codegen_cs_prog(brw
, shader_prog
, bcp
, &key
);
213 brw
->cs
.base
.prog_offset
= old_prog_offset
;
214 brw
->cs
.prog_data
= old_prog_data
;