1 /*
2 * Copyright (c) 2008-2024 Broadcom. All Rights Reserved.
3 * The term “Broadcom” refers to Broadcom Inc.
4 * and/or its subsidiaries.
5 * SPDX-License-Identifier: MIT
6 */
7
8 #include "nir/nir_to_tgsi.h"
9 #include "util/u_inlines.h"
10 #include "util/u_math.h"
11 #include "util/u_memory.h"
12 #include "util/u_bitmask.h"
13 #include "draw/draw_context.h"
14
15 #include "svga_context.h"
16 #include "svga_hw_reg.h"
17 #include "svga_cmd.h"
18 #include "svga_debug.h"
19 #include "svga_shader.h"
20
21
22 void *
svga_create_fs_state(struct pipe_context * pipe,const struct pipe_shader_state * templ)23 svga_create_fs_state(struct pipe_context *pipe,
24 const struct pipe_shader_state *templ)
25 {
26 struct svga_context *svga = svga_context(pipe);
27 struct svga_fragment_shader *fs;
28
29 SVGA_STATS_TIME_PUSH(svga_sws(svga), SVGA_STATS_TIME_CREATEFS);
30
31 fs = (struct svga_fragment_shader *)
32 svga_create_shader(pipe, templ, PIPE_SHADER_FRAGMENT,
33 sizeof(struct svga_fragment_shader));
34 if (!fs)
35 goto done;
36
37 /* Original shader IR could have been deleted if it is converted from
38 * NIR to TGSI. So need to explicitly set the shader state type to TGSI
39 * before passing it to draw.
40 */
41 struct pipe_shader_state tmp = *templ;
42 tmp.type = PIPE_SHADER_IR_TGSI;
43 tmp.tokens = fs->base.tokens;
44
45 fs->generic_inputs = svga_get_generic_inputs_mask(&fs->base.tgsi_info);
46
47 fs->base.get_dummy_shader = svga_get_compiled_dummy_fragment_shader;
48
49 svga_remap_generics(fs->base.info.generic_inputs_mask,
50 fs->generic_remap_table);
51
52 fs->draw_shader = draw_create_fragment_shader(svga->swtnl.draw, &tmp);
53
54 done:
55 SVGA_STATS_TIME_POP(svga_sws(svga));
56 return fs;
57 }
58
59
60 void
svga_bind_fs_state(struct pipe_context * pipe,void * shader)61 svga_bind_fs_state(struct pipe_context *pipe, void *shader)
62 {
63 struct svga_fragment_shader *fs = (struct svga_fragment_shader *) shader;
64 struct svga_context *svga = svga_context(pipe);
65
66 svga->curr.fs = fs;
67 svga->dirty |= SVGA_NEW_FS;
68
69 /* Check if shader uses samplers */
70 svga_set_curr_shader_use_samplers_flag(svga, PIPE_SHADER_FRAGMENT,
71 svga_shader_use_samplers(&fs->base));
72 }
73
74
75 static void
svga_delete_fs_state(struct pipe_context * pipe,void * shader)76 svga_delete_fs_state(struct pipe_context *pipe, void *shader)
77 {
78 struct svga_context *svga = svga_context(pipe);
79 struct svga_fragment_shader *fs = (struct svga_fragment_shader *) shader;
80 struct svga_fragment_shader *next_fs;
81 struct svga_shader_variant *variant, *tmp;
82
83 svga_hwtnl_flush_retry(svga);
84
85 assert(fs->base.parent == NULL);
86
87 while (fs) {
88 next_fs = (struct svga_fragment_shader *) fs->base.next;
89
90 draw_delete_fragment_shader(svga->swtnl.draw, fs->draw_shader);
91
92 for (variant = fs->base.variants; variant; variant = tmp) {
93 tmp = variant->next;
94
95 /* Check if deleting currently bound shader */
96 if (variant == svga->state.hw_draw.fs) {
97 SVGA_RETRY(svga, svga_set_shader(svga, SVGA3D_SHADERTYPE_PS, NULL));
98 svga->state.hw_draw.fs = NULL;
99 }
100
101 svga_destroy_shader_variant(svga, variant);
102 }
103
104 FREE((void *)fs->base.tokens);
105 FREE(fs);
106 fs = next_fs;
107 }
108 }
109
110
111 void
svga_init_fs_functions(struct svga_context * svga)112 svga_init_fs_functions(struct svga_context *svga)
113 {
114 svga->pipe.create_fs_state = svga_create_fs_state;
115 svga->pipe.bind_fs_state = svga_bind_fs_state;
116 svga->pipe.delete_fs_state = svga_delete_fs_state;
117 }
118