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"
35 brw_codegen_cs_prog(struct brw_context
*brw
,
36 struct gl_shader_program
*prog
,
37 struct brw_compute_program
*cp
,
38 struct brw_cs_prog_key
*key
)
40 struct gl_context
*ctx
= &brw
->ctx
;
41 const GLuint
*program
;
42 void *mem_ctx
= ralloc_context(NULL
);
44 struct brw_cs_prog_data prog_data
;
45 bool start_busy
= false;
46 double start_time
= 0;
48 struct brw_shader
*cs
=
49 (struct brw_shader
*) prog
->_LinkedShaders
[MESA_SHADER_COMPUTE
];
52 memset(&prog_data
, 0, sizeof(prog_data
));
54 /* Allocate the references to the uniforms that will end up in the
55 * prog_data associated with the compiled program, and which will be freed
58 int param_count
= cs
->base
.num_uniform_components
+
59 cs
->base
.NumImages
* BRW_IMAGE_PARAM_SIZE
;
61 /* The backend also sometimes adds params for texture size. */
62 param_count
+= 2 * ctx
->Const
.Program
[MESA_SHADER_COMPUTE
].MaxTextureImageUnits
;
63 prog_data
.base
.param
=
64 rzalloc_array(NULL
, const gl_constant_value
*, param_count
);
65 prog_data
.base
.pull_param
=
66 rzalloc_array(NULL
, const gl_constant_value
*, param_count
);
67 prog_data
.base
.image_param
=
68 rzalloc_array(NULL
, struct brw_image_param
, cs
->base
.NumImages
);
69 prog_data
.base
.nr_params
= param_count
;
70 prog_data
.base
.nr_image_params
= cs
->base
.NumImages
;
72 if (unlikely(brw
->perf_debug
)) {
73 start_busy
= (brw
->batch
.last_bo
&&
74 drm_intel_bo_busy(brw
->batch
.last_bo
));
75 start_time
= get_time();
78 program
= brw_cs_emit(brw
, mem_ctx
, key
, &prog_data
,
79 &cp
->program
, prog
, &program_size
);
80 if (program
== NULL
) {
85 if (unlikely(brw
->perf_debug
) && cs
) {
86 if (cs
->compiled_once
) {
87 _mesa_problem(&brw
->ctx
, "CS programs shouldn't need recompiles");
89 cs
->compiled_once
= true;
91 if (start_busy
&& !drm_intel_bo_busy(brw
->batch
.last_bo
)) {
92 perf_debug("CS compile took %.03f ms and stalled the GPU\n",
93 (get_time() - start_time
) * 1000);
97 if (prog_data
.base
.total_scratch
) {
98 brw_get_scratch_bo(brw
, &brw
->cs
.base
.scratch_bo
,
99 prog_data
.base
.total_scratch
* brw
->max_cs_threads
);
102 if (unlikely(INTEL_DEBUG
& DEBUG_CS
))
103 fprintf(stderr
, "\n");
105 brw_upload_cache(&brw
->cache
, BRW_CACHE_CS_PROG
,
107 program
, program_size
,
108 &prog_data
, sizeof(prog_data
),
109 &brw
->cs
.base
.prog_offset
, &brw
->cs
.prog_data
);
110 ralloc_free(mem_ctx
);
117 brw_cs_populate_key(struct brw_context
*brw
, struct brw_cs_prog_key
*key
)
119 struct gl_context
*ctx
= &brw
->ctx
;
120 /* BRW_NEW_COMPUTE_PROGRAM */
121 const struct brw_compute_program
*cp
=
122 (struct brw_compute_program
*) brw
->compute_program
;
123 const struct gl_program
*prog
= (struct gl_program
*) cp
;
125 memset(key
, 0, sizeof(*key
));
128 brw_populate_sampler_prog_key_data(ctx
, prog
, brw
->cs
.base
.sampler_count
,
131 /* The unique compute program ID */
132 key
->program_string_id
= cp
->id
;
137 brw_upload_cs_prog(struct brw_context
*brw
)
139 struct gl_context
*ctx
= &brw
->ctx
;
140 struct brw_cs_prog_key key
;
141 struct brw_compute_program
*cp
= (struct brw_compute_program
*)
142 brw
->compute_program
;
147 if (!brw_state_dirty(brw
, _NEW_TEXTURE
, BRW_NEW_COMPUTE_PROGRAM
))
150 brw
->cs
.base
.sampler_count
=
151 _mesa_fls(ctx
->ComputeProgram
._Current
->Base
.SamplersUsed
);
153 brw_cs_populate_key(brw
, &key
);
155 if (!brw_search_cache(&brw
->cache
, BRW_CACHE_CS_PROG
,
157 &brw
->cs
.base
.prog_offset
, &brw
->cs
.prog_data
)) {
159 brw_codegen_cs_prog(brw
,
160 ctx
->Shader
.CurrentProgram
[MESA_SHADER_COMPUTE
],
165 brw
->cs
.base
.prog_data
= &brw
->cs
.prog_data
->base
;
170 brw_cs_precompile(struct gl_context
*ctx
,
171 struct gl_shader_program
*shader_prog
,
172 struct gl_program
*prog
)
174 struct brw_context
*brw
= brw_context(ctx
);
175 struct brw_cs_prog_key key
;
177 struct gl_compute_program
*cp
= (struct gl_compute_program
*) prog
;
178 struct brw_compute_program
*bcp
= brw_compute_program(cp
);
180 memset(&key
, 0, sizeof(key
));
181 key
.program_string_id
= bcp
->id
;
183 brw_setup_tex_for_precompile(brw
, &key
.tex
, prog
);
185 uint32_t old_prog_offset
= brw
->cs
.base
.prog_offset
;
186 struct brw_cs_prog_data
*old_prog_data
= brw
->cs
.prog_data
;
188 bool success
= brw_codegen_cs_prog(brw
, shader_prog
, bcp
, &key
);
190 brw
->cs
.base
.prog_offset
= old_prog_offset
;
191 brw
->cs
.prog_data
= old_prog_data
;