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
31 #include "svga_context.h"
32 #include "svga_cmd.h"
33 #include "svga_shader.h"
34 #include "svga_tgsi.h"
35
36
37 /**
38 * Create compute shader compile key.
39 */
40 static void
make_cs_key(struct svga_context * svga,struct svga_compile_key * key)41 make_cs_key(struct svga_context *svga,
42 struct svga_compile_key *key)
43 {
44 struct svga_compute_shader *cs = svga->curr.cs;
45
46 memset(key, 0, sizeof *key);
47
48 svga_init_shader_key_common(svga, PIPE_SHADER_COMPUTE, &cs->base, key);
49
50 key->cs.grid_size[0] = svga->curr.grid_info.size[0];
51 key->cs.grid_size[1] = svga->curr.grid_info.size[1];
52 key->cs.grid_size[2] = svga->curr.grid_info.size[2];
53 key->cs.mem_size = cs->shared_mem_size;
54
55 if (svga->curr.grid_info.indirect && cs->base.info.uses_grid_size) {
56 struct pipe_transfer *transfer = NULL;
57 const void *map = NULL;
58 map = pipe_buffer_map(&svga->pipe, svga->curr.grid_info.indirect,
59 PIPE_MAP_READ, &transfer);
60 memcpy(key->cs.grid_size, map, 3 * sizeof(uint));
61 pipe_buffer_unmap(&svga->pipe, transfer);
62 }
63 }
64
65
66 /**
67 * Emit current compute shader to device.
68 */
69 static enum pipe_error
emit_hw_cs(struct svga_context * svga,uint64_t dirty)70 emit_hw_cs(struct svga_context *svga, uint64_t dirty)
71 {
72 struct svga_shader_variant *variant;
73 struct svga_compute_shader *cs = svga->curr.cs;
74 enum pipe_error ret = PIPE_OK;
75 struct svga_compile_key key;
76
77 assert(svga_have_sm5(svga));
78
79 SVGA_STATS_TIME_PUSH(svga_sws(svga), SVGA_STATS_TIME_EMITCS);
80
81 if (!cs) {
82 if (svga->state.hw_draw.cs != NULL) {
83
84 /** The previous compute shader is made inactive.
85 * Needs to unbind the compute shader.
86 */
87 ret = svga_set_shader(svga, SVGA3D_SHADERTYPE_CS, NULL);
88 if (ret != PIPE_OK)
89 goto done;
90 svga->state.hw_draw.cs = NULL;
91 }
92 goto done;
93 }
94
95 make_cs_key(svga, &key);
96
97 /* See if we already have a CS variant that matches the key */
98 variant = svga_search_shader_key(&cs->base, &key);
99
100 if (!variant) {
101 ret = svga_compile_shader(svga, &cs->base, &key, &variant);
102 if (ret != PIPE_OK)
103 goto done;
104 }
105
106 if (variant != svga->state.hw_draw.cs) {
107 /* Bind the new variant */
108 ret = svga_set_shader(svga, SVGA3D_SHADERTYPE_CS, variant);
109 if (ret != PIPE_OK)
110 goto done;
111
112 svga->rebind.flags.cs = false;
113 svga->dirty |= SVGA_NEW_CS_VARIANT;
114 svga->state.hw_draw.cs = variant;
115 }
116
117 done:
118 SVGA_STATS_TIME_POP(svga_sws(svga));
119 return ret;
120 }
121
122 struct svga_tracked_state svga_hw_cs =
123 {
124 "compute shader",
125 (SVGA_NEW_CS |
126 SVGA_NEW_TEXTURE_BINDING |
127 SVGA_NEW_SAMPLER |
128 SVGA_NEW_CS_RAW_BUFFER),
129 emit_hw_cs
130 };
131