1 /**********************************************************
2 * Copyright 2022 VMware, Inc. All rights reserved.
3 *
4 * Permission is hereby granted, free of charge, to any person
5 * obtaining a copy of this software and associated documentation
6 * files (the "Software"), to deal in the Software without
7 * restriction, including without limitation the rights to use, copy,
8 * modify, merge, publish, distribute, sublicense, and/or sell copies
9 * of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
19 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
20 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 *
24 **********************************************************/
25
26 #include "util/u_inlines.h"
27 #include "util/u_memory.h"
28 #include "util/u_bitmask.h"
29 #include "translate/translate.h"
30 #include "tgsi/tgsi_ureg.h"
31
32 #include "svga_context.h"
33 #include "svga_cmd.h"
34 #include "svga_shader.h"
35 #include "svga_tgsi.h"
36
37
38 /**
39 * Create compute shader compile key.
40 */
41 static void
make_cs_key(struct svga_context * svga,struct svga_compile_key * key)42 make_cs_key(struct svga_context *svga,
43 struct svga_compile_key *key)
44 {
45 struct svga_compute_shader *cs = svga->curr.cs;
46
47 memset(key, 0, sizeof *key);
48
49 svga_init_shader_key_common(svga, PIPE_SHADER_COMPUTE, &cs->base, key);
50
51 key->cs.grid_size[0] = svga->curr.grid_info.size[0];
52 key->cs.grid_size[1] = svga->curr.grid_info.size[1];
53 key->cs.grid_size[2] = svga->curr.grid_info.size[2];
54 key->cs.mem_size = cs->shared_mem_size;
55
56 if (svga->curr.grid_info.indirect && cs->base.info.uses_grid_size) {
57 struct pipe_transfer *transfer = NULL;
58 const void *map = NULL;
59 map = pipe_buffer_map(&svga->pipe, svga->curr.grid_info.indirect,
60 PIPE_MAP_READ, &transfer);
61 memcpy(key->cs.grid_size, map, 3 * sizeof(uint));
62 pipe_buffer_unmap(&svga->pipe, transfer);
63 }
64 }
65
66
67 /**
68 * Emit current compute shader to device.
69 */
70 static enum pipe_error
emit_hw_cs(struct svga_context * svga,uint64_t dirty)71 emit_hw_cs(struct svga_context *svga, uint64_t dirty)
72 {
73 struct svga_shader_variant *variant;
74 struct svga_compute_shader *cs = svga->curr.cs;
75 enum pipe_error ret = PIPE_OK;
76 struct svga_compile_key key;
77
78 assert(svga_have_sm5(svga));
79
80 SVGA_STATS_TIME_PUSH(svga_sws(svga), SVGA_STATS_TIME_EMITCS);
81
82 if (!cs) {
83 if (svga->state.hw_draw.cs != NULL) {
84
85 /** The previous compute shader is made inactive.
86 * Needs to unbind the compute shader.
87 */
88 ret = svga_set_shader(svga, SVGA3D_SHADERTYPE_CS, NULL);
89 if (ret != PIPE_OK)
90 goto done;
91 svga->state.hw_draw.cs = NULL;
92 }
93 goto done;
94 }
95
96 make_cs_key(svga, &key);
97
98 /* See if we already have a CS variant that matches the key */
99 variant = svga_search_shader_key(&cs->base, &key);
100
101 if (!variant) {
102 ret = svga_compile_shader(svga, &cs->base, &key, &variant);
103 if (ret != PIPE_OK)
104 goto done;
105 }
106
107 if (variant != svga->state.hw_draw.cs) {
108 /* Bind the new variant */
109 ret = svga_set_shader(svga, SVGA3D_SHADERTYPE_CS, variant);
110 if (ret != PIPE_OK)
111 goto done;
112
113 svga->rebind.flags.cs = FALSE;
114 svga->dirty |= SVGA_NEW_CS_VARIANT;
115 svga->state.hw_draw.cs = variant;
116 }
117
118 done:
119 SVGA_STATS_TIME_POP(svga_sws(svga));
120 return ret;
121 }
122
123 struct svga_tracked_state svga_hw_cs =
124 {
125 "compute shader",
126 (SVGA_NEW_CS |
127 SVGA_NEW_TEXTURE_BINDING |
128 SVGA_NEW_SAMPLER |
129 SVGA_NEW_CS_RAW_BUFFER),
130 emit_hw_cs
131 };
132