1544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin/**************************************************************************
2544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin *
3544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin * Copyright 2009 VMware, Inc.  All Rights Reserved.
4544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin *
5544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin * Permission is hereby granted, free of charge, to any person obtaining a
6544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin * copy of this software and associated documentation files (the
7544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin * "Software"), to deal in the Software without restriction, including
8544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin * without limitation the rights to use, copy, modify, merge, publish,
9544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin * distribute, sub license, and/or sell copies of the Software, and to
10544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin * permit persons to whom the Software is furnished to do so, subject to
11544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin * the following conditions:
12544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin *
13544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin * The above copyright notice and this permission notice (including the
14544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin * next paragraph) shall be included in all copies or substantial portions
15544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin * of the Software.
16544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin *
17544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
20544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
21544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
22544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
23544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin *
25544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin **************************************************************************/
26544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin
27544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin#include "shaders_cache.h"
28544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin
29544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin#include "vg_context.h"
30544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin
31544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin#include "pipe/p_context.h"
32544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin#include "pipe/p_defines.h"
33544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin#include "pipe/p_shader_tokens.h"
34544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin
35544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin#include "tgsi/tgsi_build.h"
36544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin#include "tgsi/tgsi_dump.h"
37544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin#include "tgsi/tgsi_parse.h"
38544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin#include "tgsi/tgsi_util.h"
39544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin#include "tgsi/tgsi_text.h"
40544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin
41544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin#include "util/u_memory.h"
42544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin#include "util/u_math.h"
43544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin#include "util/u_debug.h"
44544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin#include "cso_cache/cso_hash.h"
45544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin#include "cso_cache/cso_context.h"
46544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin
47544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin#include "VG/openvg.h"
48544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin
49544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin#include "asm_fill.h"
50544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin
51544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin/* Essentially we construct an ubber-shader based on the state
52544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin * of the pipeline. The stages are:
53213e288e78bf5b0fb0a996cc17dfd959756c2c53Chia-I Wu * 1) Paint generation (color/gradient/pattern)
54213e288e78bf5b0fb0a996cc17dfd959756c2c53Chia-I Wu * 2) Image composition (normal/multiply/stencil)
55e360f91f152615b35857a4d008d0439a3c3285a8Chia-I Wu * 3) Color transform
56e8ff3931f801dffdfd54832c298351e933688235Chia-I Wu * 4) Per-channel alpha generation
57e8ff3931f801dffdfd54832c298351e933688235Chia-I Wu * 5) Extended blend (multiply/screen/darken/lighten)
58e8ff3931f801dffdfd54832c298351e933688235Chia-I Wu * 6) Mask
59e8ff3931f801dffdfd54832c298351e933688235Chia-I Wu * 7) Premultiply/Unpremultiply
60e8ff3931f801dffdfd54832c298351e933688235Chia-I Wu * 8) Color transform (to black and white)
61544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin */
62e8ff3931f801dffdfd54832c298351e933688235Chia-I Wu#define SHADER_STAGES 8
63544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin
64544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusinstruct cached_shader {
65544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin   void *driver_shader;
66544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin   struct pipe_shader_state state;
67544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin};
68544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin
69544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusinstruct shaders_cache {
70544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin   struct vg_context *pipe;
71544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin
72544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin   struct cso_hash *hash;
73544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin};
74544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin
75544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin
76544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusinstatic INLINE struct tgsi_token *tokens_from_assembly(const char *txt, int num_tokens)
77544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin{
78544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin   struct tgsi_token *tokens;
79544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin
80544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin   tokens = (struct tgsi_token *) MALLOC(num_tokens * sizeof(tokens[0]));
81544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin
82544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin   tgsi_text_translate(txt, tokens, num_tokens);
83544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin
84544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin#if DEBUG_SHADERS
85544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin   tgsi_dump(tokens, 0);
86544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin#endif
87544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin
88544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin   return tokens;
89544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin}
90544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin
91544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin/*
92544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusinstatic const char max_shader_preamble[] =
935285de7c0fc067dc036a5b421140a696ce2cabbfMichal Krol   "FRAG\n"
94544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin   "DCL IN[0], POSITION, LINEAR\n"
95544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin   "DCL IN[1], GENERIC[0], PERSPECTIVE\n"
96544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin   "DCL OUT[0], COLOR, CONSTANT\n"
97544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin   "DCL CONST[0..9], CONSTANT\n"
98544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin   "DCL TEMP[0..9], CONSTANT\n"
99544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin   "DCL SAMP[0..9], CONSTANT\n";
100544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin
101544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin   max_shader_preamble strlen == 175
102544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin*/
103544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin#define MAX_PREAMBLE 175
104544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin
105544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusinstatic INLINE VGint range_min(VGint min, VGint current)
106544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin{
107544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin   if (min < 0)
108544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin      min = current;
109544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin   else
110544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin      min = MIN2(min, current);
111544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin   return min;
112544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin}
113544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin
114544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusinstatic INLINE VGint range_max(VGint max, VGint current)
115544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin{
116544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin   return MAX2(max, current);
117544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin}
118544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin
119301a9437cc5d89686391e5722b952f52df274ab3Igor Oliveirastatic void *
120301a9437cc5d89686391e5722b952f52df274ab3Igor Oliveiracombine_shaders(const struct shader_asm_info *shaders[SHADER_STAGES], int num_shaders,
121301a9437cc5d89686391e5722b952f52df274ab3Igor Oliveira                struct pipe_context *pipe,
122301a9437cc5d89686391e5722b952f52df274ab3Igor Oliveira                struct pipe_shader_state *shader)
123544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin{
124544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin   VGboolean declare_input = VG_FALSE;
125544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin   VGint start_const   = -1, end_const   = 0;
126544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin   VGint start_temp    = -1, end_temp    = 0;
127544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin   VGint start_sampler = -1, end_sampler = 0;
128301a9437cc5d89686391e5722b952f52df274ab3Igor Oliveira   VGint i, current_shader = 0;
129544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin   VGint num_consts, num_temps, num_samplers;
130301a9437cc5d89686391e5722b952f52df274ab3Igor Oliveira   struct ureg_program *ureg;
131301a9437cc5d89686391e5722b952f52df274ab3Igor Oliveira   struct ureg_src in[2];
132301a9437cc5d89686391e5722b952f52df274ab3Igor Oliveira   struct ureg_src *sampler = NULL;
133301a9437cc5d89686391e5722b952f52df274ab3Igor Oliveira   struct ureg_src *constant = NULL;
134301a9437cc5d89686391e5722b952f52df274ab3Igor Oliveira   struct ureg_dst out, *temp = NULL;
135301a9437cc5d89686391e5722b952f52df274ab3Igor Oliveira   void *p = NULL;
136544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin
137544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin   for (i = 0; i < num_shaders; ++i) {
138544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin      if (shaders[i]->num_consts)
139544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin         start_const = range_min(start_const, shaders[i]->start_const);
140544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin      if (shaders[i]->num_temps)
141544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin         start_temp = range_min(start_temp, shaders[i]->start_temp);
142544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin      if (shaders[i]->num_samplers)
143544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin         start_sampler = range_min(start_sampler, shaders[i]->start_sampler);
144544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin
145544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin      end_const = range_max(end_const, shaders[i]->start_const +
146544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin                            shaders[i]->num_consts);
147544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin      end_temp = range_max(end_temp, shaders[i]->start_temp +
148544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin                            shaders[i]->num_temps);
149544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin      end_sampler = range_max(end_sampler, shaders[i]->start_sampler +
150544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin                            shaders[i]->num_samplers);
151544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin      if (shaders[i]->needs_position)
152544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin         declare_input = VG_TRUE;
153544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin   }
154544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin   /* if they're still unitialized, initialize them */
155544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin   if (start_const < 0)
156544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin      start_const = 0;
157544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin   if (start_temp < 0)
158544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin      start_temp = 0;
159544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin   if (start_sampler < 0)
160301a9437cc5d89686391e5722b952f52df274ab3Igor Oliveira       start_sampler = 0;
161544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin
162544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin   num_consts   = end_const   - start_const;
163544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin   num_temps    = end_temp    - start_temp;
164544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin   num_samplers = end_sampler - start_sampler;
165544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin
166301a9437cc5d89686391e5722b952f52df274ab3Igor Oliveira   ureg = ureg_create(TGSI_PROCESSOR_FRAGMENT);
167301a9437cc5d89686391e5722b952f52df274ab3Igor Oliveira   if (!ureg)
168301a9437cc5d89686391e5722b952f52df274ab3Igor Oliveira       return NULL;
169544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin
170544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin   if (declare_input) {
171301a9437cc5d89686391e5722b952f52df274ab3Igor Oliveira      in[0] = ureg_DECL_fs_input(ureg,
172301a9437cc5d89686391e5722b952f52df274ab3Igor Oliveira                                 TGSI_SEMANTIC_POSITION,
173301a9437cc5d89686391e5722b952f52df274ab3Igor Oliveira                                 0,
174301a9437cc5d89686391e5722b952f52df274ab3Igor Oliveira                                 TGSI_INTERPOLATE_LINEAR);
175301a9437cc5d89686391e5722b952f52df274ab3Igor Oliveira      in[1] = ureg_DECL_fs_input(ureg,
176301a9437cc5d89686391e5722b952f52df274ab3Igor Oliveira                                 TGSI_SEMANTIC_GENERIC,
177301a9437cc5d89686391e5722b952f52df274ab3Igor Oliveira                                 0,
178301a9437cc5d89686391e5722b952f52df274ab3Igor Oliveira                                 TGSI_INTERPOLATE_PERSPECTIVE);
179544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin   }
180544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin
181544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin   /* we always have a color output */
182301a9437cc5d89686391e5722b952f52df274ab3Igor Oliveira   out = ureg_DECL_output(ureg, TGSI_SEMANTIC_COLOR, 0);
183544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin
184301a9437cc5d89686391e5722b952f52df274ab3Igor Oliveira   if (num_consts >= 1) {
185301a9437cc5d89686391e5722b952f52df274ab3Igor Oliveira      constant = (struct ureg_src *) malloc(sizeof(struct ureg_src) * end_const);
186301a9437cc5d89686391e5722b952f52df274ab3Igor Oliveira      for (i = start_const; i < end_const; i++) {
187301a9437cc5d89686391e5722b952f52df274ab3Igor Oliveira         constant[i] = ureg_DECL_constant(ureg, i);
188301a9437cc5d89686391e5722b952f52df274ab3Igor Oliveira      }
189544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin
190544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin   }
191544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin
192301a9437cc5d89686391e5722b952f52df274ab3Igor Oliveira   if (num_temps >= 1) {
193301a9437cc5d89686391e5722b952f52df274ab3Igor Oliveira      temp = (struct ureg_dst *) malloc(sizeof(struct ureg_dst) * end_temp);
194301a9437cc5d89686391e5722b952f52df274ab3Igor Oliveira      for (i = start_temp; i < end_temp; i++) {
195301a9437cc5d89686391e5722b952f52df274ab3Igor Oliveira         temp[i] = ureg_DECL_temporary(ureg);
196301a9437cc5d89686391e5722b952f52df274ab3Igor Oliveira      }
197301a9437cc5d89686391e5722b952f52df274ab3Igor Oliveira   }
198544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin
199301a9437cc5d89686391e5722b952f52df274ab3Igor Oliveira   if (num_samplers >= 1) {
200301a9437cc5d89686391e5722b952f52df274ab3Igor Oliveira      sampler = (struct ureg_src *) malloc(sizeof(struct ureg_src) * end_sampler);
201301a9437cc5d89686391e5722b952f52df274ab3Igor Oliveira      for (i = start_sampler; i < end_sampler; i++) {
202301a9437cc5d89686391e5722b952f52df274ab3Igor Oliveira         sampler[i] = ureg_DECL_sampler(ureg, i);
203301a9437cc5d89686391e5722b952f52df274ab3Igor Oliveira      }
204301a9437cc5d89686391e5722b952f52df274ab3Igor Oliveira   }
205544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin
206544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin   while (current_shader < num_shaders) {
207301a9437cc5d89686391e5722b952f52df274ab3Igor Oliveira      if ((current_shader + 1) == num_shaders) {
208301a9437cc5d89686391e5722b952f52df274ab3Igor Oliveira         shaders[current_shader]->func(ureg,
209301a9437cc5d89686391e5722b952f52df274ab3Igor Oliveira                                       &out,
210301a9437cc5d89686391e5722b952f52df274ab3Igor Oliveira                                       in,
211301a9437cc5d89686391e5722b952f52df274ab3Igor Oliveira                                       sampler,
212301a9437cc5d89686391e5722b952f52df274ab3Igor Oliveira                                       temp,
213301a9437cc5d89686391e5722b952f52df274ab3Igor Oliveira                                       constant);
214301a9437cc5d89686391e5722b952f52df274ab3Igor Oliveira      } else {
215301a9437cc5d89686391e5722b952f52df274ab3Igor Oliveira         shaders[current_shader]->func(ureg,
216301a9437cc5d89686391e5722b952f52df274ab3Igor Oliveira                                      &temp[0],
217301a9437cc5d89686391e5722b952f52df274ab3Igor Oliveira                                      in,
218301a9437cc5d89686391e5722b952f52df274ab3Igor Oliveira                                      sampler,
219301a9437cc5d89686391e5722b952f52df274ab3Igor Oliveira                                      temp,
220301a9437cc5d89686391e5722b952f52df274ab3Igor Oliveira                                      constant);
221301a9437cc5d89686391e5722b952f52df274ab3Igor Oliveira      }
222301a9437cc5d89686391e5722b952f52df274ab3Igor Oliveira      current_shader++;
223544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin   }
224544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin
225301a9437cc5d89686391e5722b952f52df274ab3Igor Oliveira   ureg_END(ureg);
226544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin
227301a9437cc5d89686391e5722b952f52df274ab3Igor Oliveira   shader->tokens = ureg_finalize(ureg);
228301a9437cc5d89686391e5722b952f52df274ab3Igor Oliveira   if(!shader->tokens)
229301a9437cc5d89686391e5722b952f52df274ab3Igor Oliveira      return NULL;
230544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin
231301a9437cc5d89686391e5722b952f52df274ab3Igor Oliveira   p = pipe->create_fs_state(pipe, shader);
232544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin
233301a9437cc5d89686391e5722b952f52df274ab3Igor Oliveira   if (num_temps >= 1) {
234301a9437cc5d89686391e5722b952f52df274ab3Igor Oliveira      for (i = start_temp; i < end_temp; i++) {
235301a9437cc5d89686391e5722b952f52df274ab3Igor Oliveira         ureg_release_temporary(ureg, temp[i]);
236301a9437cc5d89686391e5722b952f52df274ab3Igor Oliveira      }
237301a9437cc5d89686391e5722b952f52df274ab3Igor Oliveira   }
238544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin
2391b800c10e4aa3a9792e221043c54bbc930b50829Brian Paul   ureg_destroy(ureg);
2401b800c10e4aa3a9792e221043c54bbc930b50829Brian Paul
241301a9437cc5d89686391e5722b952f52df274ab3Igor Oliveira   if (temp)
242301a9437cc5d89686391e5722b952f52df274ab3Igor Oliveira      free(temp);
243301a9437cc5d89686391e5722b952f52df274ab3Igor Oliveira   if (constant)
244301a9437cc5d89686391e5722b952f52df274ab3Igor Oliveira      free(constant);
245301a9437cc5d89686391e5722b952f52df274ab3Igor Oliveira   if (sampler)
246301a9437cc5d89686391e5722b952f52df274ab3Igor Oliveira      free(sampler);
247544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin
248301a9437cc5d89686391e5722b952f52df274ab3Igor Oliveira   return p;
249544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin}
250544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin
251544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusinstatic void *
252544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusincreate_shader(struct pipe_context *pipe,
253544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin              int id,
254544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin              struct pipe_shader_state *shader)
255544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin{
256213e288e78bf5b0fb0a996cc17dfd959756c2c53Chia-I Wu   int idx = 0, sh;
257544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin   const struct shader_asm_info * shaders[SHADER_STAGES];
258544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin
259544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin   /* first stage */
260213e288e78bf5b0fb0a996cc17dfd959756c2c53Chia-I Wu   sh = SHADERS_GET_PAINT_SHADER(id);
261213e288e78bf5b0fb0a996cc17dfd959756c2c53Chia-I Wu   switch (sh << SHADERS_PAINT_SHIFT) {
262213e288e78bf5b0fb0a996cc17dfd959756c2c53Chia-I Wu   case VEGA_SOLID_FILL_SHADER:
263213e288e78bf5b0fb0a996cc17dfd959756c2c53Chia-I Wu   case VEGA_LINEAR_GRADIENT_SHADER:
264213e288e78bf5b0fb0a996cc17dfd959756c2c53Chia-I Wu   case VEGA_RADIAL_GRADIENT_SHADER:
265213e288e78bf5b0fb0a996cc17dfd959756c2c53Chia-I Wu   case VEGA_PATTERN_SHADER:
266d7aa03b4feb7c30408b2ed3070e0fe33e2fd05baChia-I Wu   case VEGA_PAINT_DEGENERATE_SHADER:
267213e288e78bf5b0fb0a996cc17dfd959756c2c53Chia-I Wu      shaders[idx] = &shaders_paint_asm[(sh >> SHADERS_PAINT_SHIFT) - 1];
268213e288e78bf5b0fb0a996cc17dfd959756c2c53Chia-I Wu      assert(shaders[idx]->id == sh);
269213e288e78bf5b0fb0a996cc17dfd959756c2c53Chia-I Wu      idx++;
270213e288e78bf5b0fb0a996cc17dfd959756c2c53Chia-I Wu      break;
271213e288e78bf5b0fb0a996cc17dfd959756c2c53Chia-I Wu   default:
272213e288e78bf5b0fb0a996cc17dfd959756c2c53Chia-I Wu      break;
273544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin   }
274544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin
275544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin   /* second stage */
276213e288e78bf5b0fb0a996cc17dfd959756c2c53Chia-I Wu   sh = SHADERS_GET_IMAGE_SHADER(id);
277213e288e78bf5b0fb0a996cc17dfd959756c2c53Chia-I Wu   switch (sh) {
278213e288e78bf5b0fb0a996cc17dfd959756c2c53Chia-I Wu   case VEGA_IMAGE_NORMAL_SHADER:
279213e288e78bf5b0fb0a996cc17dfd959756c2c53Chia-I Wu   case VEGA_IMAGE_MULTIPLY_SHADER:
280213e288e78bf5b0fb0a996cc17dfd959756c2c53Chia-I Wu   case VEGA_IMAGE_STENCIL_SHADER:
281213e288e78bf5b0fb0a996cc17dfd959756c2c53Chia-I Wu      shaders[idx] = &shaders_image_asm[(sh >> SHADERS_IMAGE_SHIFT) - 1];
282213e288e78bf5b0fb0a996cc17dfd959756c2c53Chia-I Wu      assert(shaders[idx]->id == sh);
283213e288e78bf5b0fb0a996cc17dfd959756c2c53Chia-I Wu      idx++;
284213e288e78bf5b0fb0a996cc17dfd959756c2c53Chia-I Wu      break;
285213e288e78bf5b0fb0a996cc17dfd959756c2c53Chia-I Wu   default:
286213e288e78bf5b0fb0a996cc17dfd959756c2c53Chia-I Wu      break;
287544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin   }
288544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin
289213e288e78bf5b0fb0a996cc17dfd959756c2c53Chia-I Wu   /* sanity check */
290213e288e78bf5b0fb0a996cc17dfd959756c2c53Chia-I Wu   assert(idx == ((!sh || sh == VEGA_IMAGE_NORMAL_SHADER) ? 1 : 2));
291213e288e78bf5b0fb0a996cc17dfd959756c2c53Chia-I Wu
292544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin   /* third stage */
293e360f91f152615b35857a4d008d0439a3c3285a8Chia-I Wu   sh = SHADERS_GET_COLOR_TRANSFORM_SHADER(id);
294e360f91f152615b35857a4d008d0439a3c3285a8Chia-I Wu   switch (sh) {
295e360f91f152615b35857a4d008d0439a3c3285a8Chia-I Wu   case VEGA_COLOR_TRANSFORM_SHADER:
296e360f91f152615b35857a4d008d0439a3c3285a8Chia-I Wu      shaders[idx] = &shaders_color_transform_asm[
297e360f91f152615b35857a4d008d0439a3c3285a8Chia-I Wu         (sh >> SHADERS_COLOR_TRANSFORM_SHIFT) - 1];
298e360f91f152615b35857a4d008d0439a3c3285a8Chia-I Wu      assert(shaders[idx]->id == sh);
299e360f91f152615b35857a4d008d0439a3c3285a8Chia-I Wu      idx++;
300e360f91f152615b35857a4d008d0439a3c3285a8Chia-I Wu      break;
301e360f91f152615b35857a4d008d0439a3c3285a8Chia-I Wu   default:
302e360f91f152615b35857a4d008d0439a3c3285a8Chia-I Wu      break;
303e360f91f152615b35857a4d008d0439a3c3285a8Chia-I Wu   }
304e360f91f152615b35857a4d008d0439a3c3285a8Chia-I Wu
305e360f91f152615b35857a4d008d0439a3c3285a8Chia-I Wu   /* fourth stage */
306e8ff3931f801dffdfd54832c298351e933688235Chia-I Wu   sh = SHADERS_GET_ALPHA_SHADER(id);
307e8ff3931f801dffdfd54832c298351e933688235Chia-I Wu   switch (sh) {
308e8ff3931f801dffdfd54832c298351e933688235Chia-I Wu   case VEGA_ALPHA_NORMAL_SHADER:
309e8ff3931f801dffdfd54832c298351e933688235Chia-I Wu   case VEGA_ALPHA_PER_CHANNEL_SHADER:
310e8ff3931f801dffdfd54832c298351e933688235Chia-I Wu      shaders[idx] = &shaders_alpha_asm[
311e8ff3931f801dffdfd54832c298351e933688235Chia-I Wu         (sh >> SHADERS_ALPHA_SHIFT) - 1];
312e8ff3931f801dffdfd54832c298351e933688235Chia-I Wu      assert(shaders[idx]->id == sh);
313e8ff3931f801dffdfd54832c298351e933688235Chia-I Wu      idx++;
314e8ff3931f801dffdfd54832c298351e933688235Chia-I Wu      break;
315e8ff3931f801dffdfd54832c298351e933688235Chia-I Wu   default:
316e8ff3931f801dffdfd54832c298351e933688235Chia-I Wu      break;
317e8ff3931f801dffdfd54832c298351e933688235Chia-I Wu   }
318e8ff3931f801dffdfd54832c298351e933688235Chia-I Wu
319e8ff3931f801dffdfd54832c298351e933688235Chia-I Wu   /* fifth stage */
320a19eaaa6c1956add5343295af7e9f682efa08d74Chia-I Wu   sh = SHADERS_GET_BLEND_SHADER(id);
321213e288e78bf5b0fb0a996cc17dfd959756c2c53Chia-I Wu   switch (sh) {
3220ee73edeccd21034e03e9e43dd0d09fa6fbf7842Chia-I Wu   case VEGA_BLEND_SRC_SHADER:
3230ee73edeccd21034e03e9e43dd0d09fa6fbf7842Chia-I Wu   case VEGA_BLEND_SRC_OVER_SHADER:
3240ee73edeccd21034e03e9e43dd0d09fa6fbf7842Chia-I Wu   case VEGA_BLEND_DST_OVER_SHADER:
3250ee73edeccd21034e03e9e43dd0d09fa6fbf7842Chia-I Wu   case VEGA_BLEND_SRC_IN_SHADER:
3260ee73edeccd21034e03e9e43dd0d09fa6fbf7842Chia-I Wu   case VEGA_BLEND_DST_IN_SHADER:
327a19eaaa6c1956add5343295af7e9f682efa08d74Chia-I Wu   case VEGA_BLEND_MULTIPLY_SHADER:
328a19eaaa6c1956add5343295af7e9f682efa08d74Chia-I Wu   case VEGA_BLEND_SCREEN_SHADER:
329a19eaaa6c1956add5343295af7e9f682efa08d74Chia-I Wu   case VEGA_BLEND_DARKEN_SHADER:
330a19eaaa6c1956add5343295af7e9f682efa08d74Chia-I Wu   case VEGA_BLEND_LIGHTEN_SHADER:
3310ee73edeccd21034e03e9e43dd0d09fa6fbf7842Chia-I Wu   case VEGA_BLEND_ADDITIVE_SHADER:
332a19eaaa6c1956add5343295af7e9f682efa08d74Chia-I Wu      shaders[idx] = &shaders_blend_asm[(sh >> SHADERS_BLEND_SHIFT) - 1];
333213e288e78bf5b0fb0a996cc17dfd959756c2c53Chia-I Wu      assert(shaders[idx]->id == sh);
334213e288e78bf5b0fb0a996cc17dfd959756c2c53Chia-I Wu      idx++;
335213e288e78bf5b0fb0a996cc17dfd959756c2c53Chia-I Wu      break;
336213e288e78bf5b0fb0a996cc17dfd959756c2c53Chia-I Wu   default:
337213e288e78bf5b0fb0a996cc17dfd959756c2c53Chia-I Wu      break;
338544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin   }
339544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin
340e8ff3931f801dffdfd54832c298351e933688235Chia-I Wu   /* sixth stage */
341a19eaaa6c1956add5343295af7e9f682efa08d74Chia-I Wu   sh = SHADERS_GET_MASK_SHADER(id);
342213e288e78bf5b0fb0a996cc17dfd959756c2c53Chia-I Wu   switch (sh) {
343a19eaaa6c1956add5343295af7e9f682efa08d74Chia-I Wu   case VEGA_MASK_SHADER:
344a19eaaa6c1956add5343295af7e9f682efa08d74Chia-I Wu      shaders[idx] = &shaders_mask_asm[(sh >> SHADERS_MASK_SHIFT) - 1];
345213e288e78bf5b0fb0a996cc17dfd959756c2c53Chia-I Wu      assert(shaders[idx]->id == sh);
346213e288e78bf5b0fb0a996cc17dfd959756c2c53Chia-I Wu      idx++;
347213e288e78bf5b0fb0a996cc17dfd959756c2c53Chia-I Wu      break;
348213e288e78bf5b0fb0a996cc17dfd959756c2c53Chia-I Wu   default:
349213e288e78bf5b0fb0a996cc17dfd959756c2c53Chia-I Wu      break;
350544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin   }
351544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin
352e8ff3931f801dffdfd54832c298351e933688235Chia-I Wu   /* seventh stage */
353213e288e78bf5b0fb0a996cc17dfd959756c2c53Chia-I Wu   sh = SHADERS_GET_PREMULTIPLY_SHADER(id);
354213e288e78bf5b0fb0a996cc17dfd959756c2c53Chia-I Wu   switch (sh) {
355213e288e78bf5b0fb0a996cc17dfd959756c2c53Chia-I Wu   case VEGA_PREMULTIPLY_SHADER:
356213e288e78bf5b0fb0a996cc17dfd959756c2c53Chia-I Wu   case VEGA_UNPREMULTIPLY_SHADER:
357213e288e78bf5b0fb0a996cc17dfd959756c2c53Chia-I Wu      shaders[idx] = &shaders_premultiply_asm[
358213e288e78bf5b0fb0a996cc17dfd959756c2c53Chia-I Wu         (sh >> SHADERS_PREMULTIPLY_SHIFT) - 1];
359213e288e78bf5b0fb0a996cc17dfd959756c2c53Chia-I Wu      assert(shaders[idx]->id == sh);
360213e288e78bf5b0fb0a996cc17dfd959756c2c53Chia-I Wu      idx++;
361213e288e78bf5b0fb0a996cc17dfd959756c2c53Chia-I Wu      break;
362213e288e78bf5b0fb0a996cc17dfd959756c2c53Chia-I Wu   default:
363213e288e78bf5b0fb0a996cc17dfd959756c2c53Chia-I Wu      break;
364544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin   }
365544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin
366e8ff3931f801dffdfd54832c298351e933688235Chia-I Wu   /* eighth stage */
367213e288e78bf5b0fb0a996cc17dfd959756c2c53Chia-I Wu   sh = SHADERS_GET_BW_SHADER(id);
368213e288e78bf5b0fb0a996cc17dfd959756c2c53Chia-I Wu   switch (sh) {
369213e288e78bf5b0fb0a996cc17dfd959756c2c53Chia-I Wu   case VEGA_BW_SHADER:
370213e288e78bf5b0fb0a996cc17dfd959756c2c53Chia-I Wu      shaders[idx] = &shaders_bw_asm[(sh >> SHADERS_BW_SHIFT) - 1];
371213e288e78bf5b0fb0a996cc17dfd959756c2c53Chia-I Wu      assert(shaders[idx]->id == sh);
372213e288e78bf5b0fb0a996cc17dfd959756c2c53Chia-I Wu      idx++;
373213e288e78bf5b0fb0a996cc17dfd959756c2c53Chia-I Wu      break;
374213e288e78bf5b0fb0a996cc17dfd959756c2c53Chia-I Wu   default:
375213e288e78bf5b0fb0a996cc17dfd959756c2c53Chia-I Wu      break;
376544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin   }
377544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin
378544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin   return combine_shaders(shaders, idx, pipe, shader);
379544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin}
380544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin
381544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin/*************************************************/
382544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin
383544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusinstruct shaders_cache * shaders_cache_create(struct vg_context *vg)
384544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin{
385544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin   struct shaders_cache *sc = CALLOC_STRUCT(shaders_cache);
386544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin
387544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin   sc->pipe = vg;
388544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin   sc->hash = cso_hash_create();
389544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin
390544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin   return sc;
391544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin}
392544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin
393544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusinvoid shaders_cache_destroy(struct shaders_cache *sc)
394544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin{
395544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin   struct cso_hash_iter iter = cso_hash_first_node(sc->hash);
396544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin
397544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin   while (!cso_hash_iter_is_null(iter)) {
398544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin      struct cached_shader *cached =
399544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin         (struct cached_shader *)cso_hash_iter_data(iter);
400544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin      cso_delete_fragment_shader(sc->pipe->cso_context,
401544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin                                 cached->driver_shader);
402544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin      iter = cso_hash_erase(sc->hash, iter);
403544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin   }
404544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin
405544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin   cso_hash_delete(sc->hash);
406f914cd1796845164109c837a111c39ba64852ad4nobled   FREE(sc);
407544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin}
408544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin
409544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusinvoid * shaders_cache_fill(struct shaders_cache *sc,
410544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin                          int shader_key)
411544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin{
412544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin   VGint key = shader_key;
413544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin   struct cached_shader *cached;
414544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin   struct cso_hash_iter iter = cso_hash_find(sc->hash, key);
415544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin
416544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin   if (cso_hash_iter_is_null(iter)) {
417544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin      cached = CALLOC_STRUCT(cached_shader);
418544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin      cached->driver_shader = create_shader(sc->pipe->pipe, key, &cached->state);
419544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin
420544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin      cso_hash_insert(sc->hash, key, cached);
421544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin
422544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin      return cached->driver_shader;
423544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin   }
424544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin
425544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin   cached = (struct cached_shader *)cso_hash_iter_data(iter);
426544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin
427544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin   assert(cached->driver_shader);
428544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin   return cached->driver_shader;
429544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin}
430544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin
431544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusinstruct vg_shader * shader_create_from_text(struct pipe_context *pipe,
432544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin                                           const char *txt, int num_tokens,
433544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin                                           int type)
434544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin{
4352d8e70fcd57b23786e3f4196f35440ed1861a98bChia-I Wu   struct vg_shader *shader = (struct vg_shader *)MALLOC(
436544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin      sizeof(struct vg_shader));
437544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin   struct tgsi_token *tokens = tokens_from_assembly(txt, num_tokens);
438544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin   struct pipe_shader_state state;
439544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin
440544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin   debug_assert(type == PIPE_SHADER_VERTEX ||
441544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin                type == PIPE_SHADER_FRAGMENT);
442544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin
443544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin   state.tokens = tokens;
444861a029ddb31e91bb4d8e18ab708d0d172f63aadMarek Olšák   memset(&state.stream_output, 0, sizeof(state.stream_output));
445544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin   shader->type = type;
446544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin   shader->tokens = tokens;
447544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin
448544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin   if (type == PIPE_SHADER_FRAGMENT)
449544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin      shader->driver = pipe->create_fs_state(pipe, &state);
450544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin   else
451544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin      shader->driver = pipe->create_vs_state(pipe, &state);
452544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin   return shader;
453544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin}
454544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin
455544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusinvoid vg_shader_destroy(struct vg_context *ctx, struct vg_shader *shader)
456544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin{
457544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin   if (shader->type == PIPE_SHADER_FRAGMENT)
458544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin      cso_delete_fragment_shader(ctx->cso_context, shader->driver);
459544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin   else
460544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin      cso_delete_vertex_shader(ctx->cso_context, shader->driver);
461f914cd1796845164109c837a111c39ba64852ad4nobled   FREE(shader->tokens);
462f914cd1796845164109c837a111c39ba64852ad4nobled   FREE(shader);
463544dd4b11f7be76bb00fe29a60eaf2772dcc69caZack Rusin}
464