1 /**********************************************************
2 * Copyright 2008-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 "nir/nir_to_tgsi.h"
27 #include "util/u_inlines.h"
28 #include "util/u_math.h"
29 #include "util/u_memory.h"
30 #include "util/u_bitmask.h"
31 #include "draw/draw_context.h"
32
33 #include "svga_context.h"
34 #include "svga_hw_reg.h"
35 #include "svga_cmd.h"
36 #include "svga_debug.h"
37 #include "svga_shader.h"
38
39
40 void *
svga_create_fs_state(struct pipe_context * pipe,const struct pipe_shader_state * templ)41 svga_create_fs_state(struct pipe_context *pipe,
42 const struct pipe_shader_state *templ)
43 {
44 struct svga_context *svga = svga_context(pipe);
45 struct svga_fragment_shader *fs;
46
47 SVGA_STATS_TIME_PUSH(svga_sws(svga), SVGA_STATS_TIME_CREATEFS);
48
49 fs = (struct svga_fragment_shader *)
50 svga_create_shader(pipe, templ, PIPE_SHADER_FRAGMENT,
51 sizeof(struct svga_fragment_shader));
52 if (!fs)
53 goto done;
54
55 /* Original shader IR could have been deleted if it is converted from
56 * NIR to TGSI. So need to explicitly set the shader state type to TGSI
57 * before passing it to draw.
58 */
59 struct pipe_shader_state tmp = *templ;
60 tmp.type = PIPE_SHADER_IR_TGSI;
61 tmp.tokens = fs->base.tokens;
62
63 fs->generic_inputs = svga_get_generic_inputs_mask(&fs->base.tgsi_info);
64
65 fs->base.get_dummy_shader = svga_get_compiled_dummy_fragment_shader;
66
67 svga_remap_generics(fs->base.info.generic_inputs_mask,
68 fs->generic_remap_table);
69
70 fs->draw_shader = draw_create_fragment_shader(svga->swtnl.draw, &tmp);
71
72 done:
73 SVGA_STATS_TIME_POP(svga_sws(svga));
74 return fs;
75 }
76
77
78 void
svga_bind_fs_state(struct pipe_context * pipe,void * shader)79 svga_bind_fs_state(struct pipe_context *pipe, void *shader)
80 {
81 struct svga_fragment_shader *fs = (struct svga_fragment_shader *) shader;
82 struct svga_context *svga = svga_context(pipe);
83
84 svga->curr.fs = fs;
85 svga->dirty |= SVGA_NEW_FS;
86
87 /* Check if shader uses samplers */
88 svga_set_curr_shader_use_samplers_flag(svga, PIPE_SHADER_FRAGMENT,
89 svga_shader_use_samplers(&fs->base));
90 }
91
92
93 static void
svga_delete_fs_state(struct pipe_context * pipe,void * shader)94 svga_delete_fs_state(struct pipe_context *pipe, void *shader)
95 {
96 struct svga_context *svga = svga_context(pipe);
97 struct svga_fragment_shader *fs = (struct svga_fragment_shader *) shader;
98 struct svga_fragment_shader *next_fs;
99 struct svga_shader_variant *variant, *tmp;
100
101 svga_hwtnl_flush_retry(svga);
102
103 assert(fs->base.parent == NULL);
104
105 while (fs) {
106 next_fs = (struct svga_fragment_shader *) fs->base.next;
107
108 draw_delete_fragment_shader(svga->swtnl.draw, fs->draw_shader);
109
110 for (variant = fs->base.variants; variant; variant = tmp) {
111 tmp = variant->next;
112
113 /* Check if deleting currently bound shader */
114 if (variant == svga->state.hw_draw.fs) {
115 SVGA_RETRY(svga, svga_set_shader(svga, SVGA3D_SHADERTYPE_PS, NULL));
116 svga->state.hw_draw.fs = NULL;
117 }
118
119 svga_destroy_shader_variant(svga, variant);
120 }
121
122 FREE((void *)fs->base.tokens);
123 FREE(fs);
124 fs = next_fs;
125 }
126 }
127
128
129 void
svga_init_fs_functions(struct svga_context * svga)130 svga_init_fs_functions(struct svga_context *svga)
131 {
132 svga->pipe.create_fs_state = svga_create_fs_state;
133 svga->pipe.bind_fs_state = svga_bind_fs_state;
134 svga->pipe.delete_fs_state = svga_delete_fs_state;
135 }
136