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_cs_prog_data_compare(const void *in_a
, const void *in_b
)
37 const struct brw_cs_prog_data
*a
=
38 (const struct brw_cs_prog_data
*)in_a
;
39 const struct brw_cs_prog_data
*b
=
40 (const struct brw_cs_prog_data
*)in_b
;
42 /* Compare the base structure. */
43 if (!brw_stage_prog_data_compare(&a
->base
, &b
->base
))
46 /* Compare the rest of the structure. */
47 const unsigned offset
= sizeof(struct brw_stage_prog_data
);
48 if (memcmp(((char *) a
) + offset
, ((char *) b
) + offset
,
49 sizeof(struct brw_cs_prog_data
) - offset
))
56 brw_codegen_cs_prog(struct brw_context
*brw
,
57 struct gl_shader_program
*prog
,
58 struct brw_compute_program
*cp
,
59 struct brw_cs_prog_key
*key
)
61 struct gl_context
*ctx
= &brw
->ctx
;
62 const GLuint
*program
;
63 void *mem_ctx
= ralloc_context(NULL
);
65 struct brw_cs_prog_data prog_data
;
66 bool start_busy
= false;
67 double start_time
= 0;
69 struct brw_shader
*cs
=
70 (struct brw_shader
*) prog
->_LinkedShaders
[MESA_SHADER_COMPUTE
];
73 memset(&prog_data
, 0, sizeof(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
= cs
->base
.num_uniform_components
+
80 cs
->base
.NumImages
* BRW_IMAGE_PARAM_SIZE
;
82 /* The backend also sometimes adds params for texture size. */
83 param_count
+= 2 * ctx
->Const
.Program
[MESA_SHADER_COMPUTE
].MaxTextureImageUnits
;
84 prog_data
.base
.param
=
85 rzalloc_array(NULL
, const gl_constant_value
*, param_count
);
86 prog_data
.base
.pull_param
=
87 rzalloc_array(NULL
, const gl_constant_value
*, param_count
);
88 prog_data
.base
.image_param
=
89 rzalloc_array(NULL
, struct brw_image_param
, cs
->base
.NumImages
);
90 prog_data
.base
.nr_params
= param_count
;
91 prog_data
.base
.nr_image_params
= cs
->base
.NumImages
;
93 if (unlikely(brw
->perf_debug
)) {
94 start_busy
= (brw
->batch
.last_bo
&&
95 drm_intel_bo_busy(brw
->batch
.last_bo
));
96 start_time
= get_time();
99 program
= brw_cs_emit(brw
, mem_ctx
, key
, &prog_data
,
100 &cp
->program
, prog
, &program_size
);
101 if (program
== NULL
) {
102 ralloc_free(mem_ctx
);
106 if (unlikely(brw
->perf_debug
) && cs
) {
107 if (cs
->compiled_once
) {
108 _mesa_problem(&brw
->ctx
, "CS programs shouldn't need recompiles");
110 cs
->compiled_once
= true;
112 if (start_busy
&& !drm_intel_bo_busy(brw
->batch
.last_bo
)) {
113 perf_debug("CS compile took %.03f ms and stalled the GPU\n",
114 (get_time() - start_time
) * 1000);
118 if (prog_data
.base
.total_scratch
) {
119 brw_get_scratch_bo(brw
, &brw
->cs
.base
.scratch_bo
,
120 prog_data
.base
.total_scratch
* brw
->max_cs_threads
);
123 if (unlikely(INTEL_DEBUG
& DEBUG_CS
))
124 fprintf(stderr
, "\n");
126 brw_upload_cache(&brw
->cache
, BRW_CACHE_CS_PROG
,
128 program
, program_size
,
129 &prog_data
, sizeof(prog_data
),
130 &brw
->cs
.base
.prog_offset
, &brw
->cs
.prog_data
);
131 ralloc_free(mem_ctx
);
138 brw_cs_populate_key(struct brw_context
*brw
, struct brw_cs_prog_key
*key
)
140 struct gl_context
*ctx
= &brw
->ctx
;
141 /* BRW_NEW_COMPUTE_PROGRAM */
142 const struct brw_compute_program
*cp
=
143 (struct brw_compute_program
*) brw
->compute_program
;
144 const struct gl_program
*prog
= (struct gl_program
*) cp
;
146 memset(key
, 0, sizeof(*key
));
149 brw_populate_sampler_prog_key_data(ctx
, prog
, brw
->cs
.base
.sampler_count
,
152 /* The unique compute program ID */
153 key
->program_string_id
= cp
->id
;
158 brw_upload_cs_prog(struct brw_context
*brw
)
160 struct gl_context
*ctx
= &brw
->ctx
;
161 struct brw_cs_prog_key key
;
162 struct brw_compute_program
*cp
= (struct brw_compute_program
*)
163 brw
->compute_program
;
168 if (!brw_state_dirty(brw
, _NEW_TEXTURE
, BRW_NEW_COMPUTE_PROGRAM
))
171 brw
->cs
.base
.sampler_count
=
172 _mesa_fls(ctx
->ComputeProgram
._Current
->Base
.SamplersUsed
);
174 brw_cs_populate_key(brw
, &key
);
176 if (!brw_search_cache(&brw
->cache
, BRW_CACHE_CS_PROG
,
178 &brw
->cs
.base
.prog_offset
, &brw
->cs
.prog_data
)) {
180 brw_codegen_cs_prog(brw
,
181 ctx
->Shader
.CurrentProgram
[MESA_SHADER_COMPUTE
],
186 brw
->cs
.base
.prog_data
= &brw
->cs
.prog_data
->base
;
191 brw_cs_precompile(struct gl_context
*ctx
,
192 struct gl_shader_program
*shader_prog
,
193 struct gl_program
*prog
)
195 struct brw_context
*brw
= brw_context(ctx
);
196 struct brw_cs_prog_key key
;
198 struct gl_compute_program
*cp
= (struct gl_compute_program
*) prog
;
199 struct brw_compute_program
*bcp
= brw_compute_program(cp
);
201 memset(&key
, 0, sizeof(key
));
202 key
.program_string_id
= bcp
->id
;
204 brw_setup_tex_for_precompile(brw
, &key
.tex
, prog
);
206 uint32_t old_prog_offset
= brw
->cs
.base
.prog_offset
;
207 struct brw_cs_prog_data
*old_prog_data
= brw
->cs
.prog_data
;
209 bool success
= brw_codegen_cs_prog(brw
, shader_prog
, bcp
, &key
);
211 brw
->cs
.base
.prog_offset
= old_prog_offset
;
212 brw
->cs
.prog_data
= old_prog_data
;