1bf215546Sopenharmony_ci/**********************************************************
2bf215546Sopenharmony_ci * Copyright 2008-2022 VMware, Inc.  All rights reserved.
3bf215546Sopenharmony_ci *
4bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person
5bf215546Sopenharmony_ci * obtaining a copy of this software and associated documentation
6bf215546Sopenharmony_ci * files (the "Software"), to deal in the Software without
7bf215546Sopenharmony_ci * restriction, including without limitation the rights to use, copy,
8bf215546Sopenharmony_ci * modify, merge, publish, distribute, sublicense, and/or sell copies
9bf215546Sopenharmony_ci * of the Software, and to permit persons to whom the Software is
10bf215546Sopenharmony_ci * furnished to do so, subject to the following conditions:
11bf215546Sopenharmony_ci *
12bf215546Sopenharmony_ci * The above copyright notice and this permission notice shall be
13bf215546Sopenharmony_ci * included in all copies or substantial portions of the Software.
14bf215546Sopenharmony_ci *
15bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16bf215546Sopenharmony_ci * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17bf215546Sopenharmony_ci * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18bf215546Sopenharmony_ci * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
19bf215546Sopenharmony_ci * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
20bf215546Sopenharmony_ci * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21bf215546Sopenharmony_ci * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22bf215546Sopenharmony_ci * SOFTWARE.
23bf215546Sopenharmony_ci *
24bf215546Sopenharmony_ci **********************************************************/
25bf215546Sopenharmony_ci
26bf215546Sopenharmony_ci#include "nir/nir_to_tgsi.h"
27bf215546Sopenharmony_ci#include "util/u_inlines.h"
28bf215546Sopenharmony_ci#include "util/u_math.h"
29bf215546Sopenharmony_ci#include "util/u_memory.h"
30bf215546Sopenharmony_ci#include "util/u_bitmask.h"
31bf215546Sopenharmony_ci#include "tgsi/tgsi_parse.h"
32bf215546Sopenharmony_ci#include "draw/draw_context.h"
33bf215546Sopenharmony_ci
34bf215546Sopenharmony_ci#include "svga_context.h"
35bf215546Sopenharmony_ci#include "svga_hw_reg.h"
36bf215546Sopenharmony_ci#include "svga_cmd.h"
37bf215546Sopenharmony_ci#include "svga_debug.h"
38bf215546Sopenharmony_ci#include "svga_shader.h"
39bf215546Sopenharmony_ci
40bf215546Sopenharmony_ci
41bf215546Sopenharmony_civoid *
42bf215546Sopenharmony_cisvga_create_fs_state(struct pipe_context *pipe,
43bf215546Sopenharmony_ci                     const struct pipe_shader_state *templ)
44bf215546Sopenharmony_ci{
45bf215546Sopenharmony_ci   struct svga_context *svga = svga_context(pipe);
46bf215546Sopenharmony_ci   struct svga_fragment_shader *fs;
47bf215546Sopenharmony_ci
48bf215546Sopenharmony_ci   SVGA_STATS_TIME_PUSH(svga_sws(svga), SVGA_STATS_TIME_CREATEFS);
49bf215546Sopenharmony_ci
50bf215546Sopenharmony_ci   fs = (struct svga_fragment_shader *)
51bf215546Sopenharmony_ci            svga_create_shader(pipe, templ, PIPE_SHADER_FRAGMENT,
52bf215546Sopenharmony_ci                               sizeof(struct svga_fragment_shader));
53bf215546Sopenharmony_ci   if (!fs)
54bf215546Sopenharmony_ci      goto done;
55bf215546Sopenharmony_ci
56bf215546Sopenharmony_ci   /* Original shader IR could have been deleted if it is converted from
57bf215546Sopenharmony_ci    * NIR to TGSI. So need to explicitly set the shader state type to TGSI
58bf215546Sopenharmony_ci    * before passing it to draw.
59bf215546Sopenharmony_ci    */
60bf215546Sopenharmony_ci   struct pipe_shader_state tmp = *templ;
61bf215546Sopenharmony_ci   tmp.type = PIPE_SHADER_IR_TGSI;
62bf215546Sopenharmony_ci   tmp.tokens = fs->base.tokens;
63bf215546Sopenharmony_ci
64bf215546Sopenharmony_ci   fs->generic_inputs = svga_get_generic_inputs_mask(&fs->base.tgsi_info);
65bf215546Sopenharmony_ci
66bf215546Sopenharmony_ci   fs->base.get_dummy_shader = svga_get_compiled_dummy_fragment_shader;
67bf215546Sopenharmony_ci
68bf215546Sopenharmony_ci   svga_remap_generics(fs->base.info.generic_inputs_mask,
69bf215546Sopenharmony_ci                       fs->generic_remap_table);
70bf215546Sopenharmony_ci
71bf215546Sopenharmony_ci   fs->draw_shader = draw_create_fragment_shader(svga->swtnl.draw, &tmp);
72bf215546Sopenharmony_ci
73bf215546Sopenharmony_cidone:
74bf215546Sopenharmony_ci   SVGA_STATS_TIME_POP(svga_sws(svga));
75bf215546Sopenharmony_ci   return fs;
76bf215546Sopenharmony_ci}
77bf215546Sopenharmony_ci
78bf215546Sopenharmony_ci
79bf215546Sopenharmony_civoid
80bf215546Sopenharmony_cisvga_bind_fs_state(struct pipe_context *pipe, void *shader)
81bf215546Sopenharmony_ci{
82bf215546Sopenharmony_ci   struct svga_fragment_shader *fs = (struct svga_fragment_shader *) shader;
83bf215546Sopenharmony_ci   struct svga_context *svga = svga_context(pipe);
84bf215546Sopenharmony_ci
85bf215546Sopenharmony_ci   svga->curr.fs = fs;
86bf215546Sopenharmony_ci   svga->dirty |= SVGA_NEW_FS;
87bf215546Sopenharmony_ci}
88bf215546Sopenharmony_ci
89bf215546Sopenharmony_ci
90bf215546Sopenharmony_cistatic void
91bf215546Sopenharmony_cisvga_delete_fs_state(struct pipe_context *pipe, void *shader)
92bf215546Sopenharmony_ci{
93bf215546Sopenharmony_ci   struct svga_context *svga = svga_context(pipe);
94bf215546Sopenharmony_ci   struct svga_fragment_shader *fs = (struct svga_fragment_shader *) shader;
95bf215546Sopenharmony_ci   struct svga_fragment_shader *next_fs;
96bf215546Sopenharmony_ci   struct svga_shader_variant *variant, *tmp;
97bf215546Sopenharmony_ci
98bf215546Sopenharmony_ci   svga_hwtnl_flush_retry(svga);
99bf215546Sopenharmony_ci
100bf215546Sopenharmony_ci   assert(fs->base.parent == NULL);
101bf215546Sopenharmony_ci
102bf215546Sopenharmony_ci   while (fs) {
103bf215546Sopenharmony_ci      next_fs = (struct svga_fragment_shader *) fs->base.next;
104bf215546Sopenharmony_ci
105bf215546Sopenharmony_ci      draw_delete_fragment_shader(svga->swtnl.draw, fs->draw_shader);
106bf215546Sopenharmony_ci
107bf215546Sopenharmony_ci      for (variant = fs->base.variants; variant; variant = tmp) {
108bf215546Sopenharmony_ci         tmp = variant->next;
109bf215546Sopenharmony_ci
110bf215546Sopenharmony_ci         /* Check if deleting currently bound shader */
111bf215546Sopenharmony_ci         if (variant == svga->state.hw_draw.fs) {
112bf215546Sopenharmony_ci            SVGA_RETRY(svga, svga_set_shader(svga, SVGA3D_SHADERTYPE_PS, NULL));
113bf215546Sopenharmony_ci            svga->state.hw_draw.fs = NULL;
114bf215546Sopenharmony_ci         }
115bf215546Sopenharmony_ci
116bf215546Sopenharmony_ci         svga_destroy_shader_variant(svga, variant);
117bf215546Sopenharmony_ci      }
118bf215546Sopenharmony_ci
119bf215546Sopenharmony_ci      FREE((void *)fs->base.tokens);
120bf215546Sopenharmony_ci      FREE(fs);
121bf215546Sopenharmony_ci      fs = next_fs;
122bf215546Sopenharmony_ci   }
123bf215546Sopenharmony_ci}
124bf215546Sopenharmony_ci
125bf215546Sopenharmony_ci
126bf215546Sopenharmony_civoid
127bf215546Sopenharmony_cisvga_init_fs_functions(struct svga_context *svga)
128bf215546Sopenharmony_ci{
129bf215546Sopenharmony_ci   svga->pipe.create_fs_state = svga_create_fs_state;
130bf215546Sopenharmony_ci   svga->pipe.bind_fs_state = svga_bind_fs_state;
131bf215546Sopenharmony_ci   svga->pipe.delete_fs_state = svga_delete_fs_state;
132bf215546Sopenharmony_ci}
133