llvmpipe: introduce new state dirty tracking for compute.
[mesa.git] / src / gallium / drivers / llvmpipe / lp_state_cs.c
1 /**************************************************************************
2 *
3 * Copyright 2019 Red Hat.
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 *
24 **************************************************************************/
25 #include "util/u_memory.h"
26 #include "util/simple_list.h"
27
28 #include "tgsi/tgsi_parse.h"
29 #include "gallivm/lp_bld_debug.h"
30 #include "lp_state_cs.h"
31 #include "lp_context.h"
32 #include "lp_debug.h"
33 #include "lp_state.h"
34
35 static void *
36 llvmpipe_create_compute_state(struct pipe_context *pipe,
37 const struct pipe_compute_state *templ)
38 {
39 struct lp_compute_shader *shader;
40
41 shader = CALLOC_STRUCT(lp_compute_shader);
42 if (!shader)
43 return NULL;
44
45 assert(templ->ir_type == PIPE_SHADER_IR_TGSI);
46 shader->base.tokens = tgsi_dup_tokens(templ->prog);
47
48 lp_build_tgsi_info(shader->base.tokens, &shader->info);
49 make_empty_list(&shader->variants);
50
51 return shader;
52 }
53
54 static void
55 llvmpipe_bind_compute_state(struct pipe_context *pipe,
56 void *cs)
57 {
58 struct llvmpipe_context *llvmpipe = llvmpipe_context(pipe);
59
60 if (llvmpipe->cs == cs)
61 return;
62
63 llvmpipe->cs = (struct lp_compute_shader *)cs;
64 llvmpipe->cs_dirty |= LP_CSNEW_CS;
65 }
66
67 /**
68 * Remove shader variant from two lists: the shader's variant list
69 * and the context's variant list.
70 */
71 static void
72 llvmpipe_remove_cs_shader_variant(struct llvmpipe_context *lp,
73 struct lp_compute_shader_variant *variant)
74 {
75 if ((LP_DEBUG & DEBUG_CS) || (gallivm_debug & GALLIVM_DEBUG_IR)) {
76 debug_printf("llvmpipe: del cs #%u var %u v created %u v cached %u "
77 "v total cached %u inst %u total inst %u\n",
78 variant->shader->no, variant->no,
79 variant->shader->variants_created,
80 variant->shader->variants_cached,
81 lp->nr_cs_variants, variant->nr_instrs, lp->nr_cs_instrs);
82 }
83
84 gallivm_destroy(variant->gallivm);
85
86 /* remove from shader's list */
87 remove_from_list(&variant->list_item_local);
88 variant->shader->variants_cached--;
89
90 /* remove from context's list */
91 remove_from_list(&variant->list_item_global);
92 lp->nr_fs_variants--;
93 lp->nr_fs_instrs -= variant->nr_instrs;
94
95 FREE(variant);
96 }
97
98 static void
99 llvmpipe_delete_compute_state(struct pipe_context *pipe,
100 void *cs)
101 {
102 struct llvmpipe_context *llvmpipe = llvmpipe_context(pipe);
103 struct lp_compute_shader *shader = cs;
104 struct lp_cs_variant_list_item *li;
105
106 /* Delete all the variants */
107 li = first_elem(&shader->variants);
108 while(!at_end(&shader->variants, li)) {
109 struct lp_cs_variant_list_item *next = next_elem(li);
110 llvmpipe_remove_cs_shader_variant(llvmpipe, li->base);
111 li = next;
112 }
113 tgsi_free_tokens(shader->base.tokens);
114 FREE(shader);
115 }
116
117 void
118 llvmpipe_init_compute_funcs(struct llvmpipe_context *llvmpipe)
119 {
120 llvmpipe->pipe.create_compute_state = llvmpipe_create_compute_state;
121 llvmpipe->pipe.bind_compute_state = llvmpipe_bind_compute_state;
122 llvmpipe->pipe.delete_compute_state = llvmpipe_delete_compute_state;
123 }
124
125 void
126 lp_csctx_destroy(struct lp_cs_context *csctx)
127 {
128 FREE(csctx);
129 }
130
131 struct lp_cs_context *lp_csctx_create(struct pipe_context *pipe)
132 {
133 struct lp_cs_context *csctx;
134
135 csctx = CALLOC_STRUCT(lp_cs_context);
136 if (!csctx)
137 return NULL;
138
139 csctx->pipe = pipe;
140 return csctx;
141 }