cell: pass texture unit (sampler number) to txp() function
[mesa.git] / src / gallium / drivers / cell / spu / spu_funcs.c
1 /**************************************************************************
2 *
3 * Copyright 2008 Tungsten Graphics, Inc., Cedar Park, Texas.
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27
28
29 /**
30 * SPU functions accessed by shaders.
31 *
32 * Authors: Brian Paul
33 */
34
35
36 #include <string.h>
37 #include <libmisc.h>
38 #include <math.h>
39 #include <cos14_v.h>
40 #include <sin14_v.h>
41 #include <transpose_matrix4x4.h>
42
43 #include "cell/common.h"
44 #include "spu_main.h"
45 #include "spu_funcs.h"
46
47
48 /** For "return"-ing four vectors */
49 struct vec_4x4
50 {
51 vector float v[4];
52 };
53
54
55 static vector float
56 spu_cos(vector float x)
57 {
58 return _cos14_v(x);
59 }
60
61 static vector float
62 spu_sin(vector float x)
63 {
64 return _sin14_v(x);
65 }
66
67 static vector float
68 spu_pow(vector float x, vector float y)
69 {
70 float z0 = powf(spu_extract(x,0), spu_extract(y,0));
71 float z1 = powf(spu_extract(x,1), spu_extract(y,1));
72 float z2 = powf(spu_extract(x,2), spu_extract(y,2));
73 float z3 = powf(spu_extract(x,3), spu_extract(y,3));
74 return (vector float) {z0, z1, z2, z3};
75 }
76
77 static vector float
78 spu_exp2(vector float x)
79 {
80 float z0 = powf(2.0f, spu_extract(x,0));
81 float z1 = powf(2.0f, spu_extract(x,1));
82 float z2 = powf(2.0f, spu_extract(x,2));
83 float z3 = powf(2.0f, spu_extract(x,3));
84 return (vector float) {z0, z1, z2, z3};
85 }
86
87 static vector float
88 spu_log2(vector float x)
89 {
90 /*
91 * log_base_2(x) = log(x) / log(2)
92 * 1.442695 = 1/log(2).
93 */
94 static const vector float k = {1.442695F, 1.442695F, 1.442695F, 1.442695F};
95 float z0 = logf(spu_extract(x,0));
96 float z1 = logf(spu_extract(x,1));
97 float z2 = logf(spu_extract(x,2));
98 float z3 = logf(spu_extract(x,3));
99 vector float v = (vector float) {z0, z1, z2, z3};
100 return spu_mul(v, k);
101 }
102
103 static struct vec_4x4
104 spu_txp(vector float s, vector float t, vector float r, vector float q,
105 unsigned unit)
106 {
107 //const uint unit = 0;
108 struct vec_4x4 colors;
109 vector float coords[4];
110
111 coords[0] = s;
112 coords[1] = t;
113 coords[2] = r;
114 coords[3] = q;
115 _transpose_matrix4x4(coords, coords);
116
117 /* get four texture samples */
118 colors.v[0] = spu.sample_texture[unit](unit, coords[0]);
119 colors.v[1] = spu.sample_texture[unit](unit, coords[1]);
120 colors.v[2] = spu.sample_texture[unit](unit, coords[2]);
121 colors.v[3] = spu.sample_texture[unit](unit, coords[3]);
122
123 _transpose_matrix4x4(colors.v, colors.v);
124 return colors;
125 }
126
127
128 /**
129 * Add named function to list of "exported" functions that will be
130 * made available to the PPU-hosted code generator.
131 */
132 static void
133 export_func(struct cell_spu_function_info *spu_functions,
134 const char *name, void *addr)
135 {
136 uint n = spu_functions->num;
137 ASSERT(strlen(name) < 16);
138 strcpy(spu_functions->names[n], name);
139 spu_functions->addrs[n] = (uint) addr;
140 spu_functions->num++;
141 ASSERT(spu_functions->num <= 16);
142 }
143
144
145 /**
146 * Return info about the SPU's function to the PPU / main memory.
147 * The PPU needs to know the address of some SPU-side functions so
148 * that we can generate shader code with function calls.
149 */
150 void
151 return_function_info(void)
152 {
153 struct cell_spu_function_info funcs ALIGN16_ATTRIB;
154 int tag = TAG_MISC;
155
156 ASSERT(sizeof(funcs) == 256); /* must be multiple of 16 bytes */
157
158 funcs.num = 0;
159 export_func(&funcs, "spu_cos", &spu_cos);
160 export_func(&funcs, "spu_sin", &spu_sin);
161 export_func(&funcs, "spu_pow", &spu_pow);
162 export_func(&funcs, "spu_exp2", &spu_exp2);
163 export_func(&funcs, "spu_log2", &spu_log2);
164 export_func(&funcs, "spu_txp", &spu_txp);
165
166 /* Send the function info back to the PPU / main memory */
167 mfc_put((void *) &funcs, /* src in local store */
168 (unsigned int) spu.init.spu_functions, /* dst in main memory */
169 sizeof(funcs), /* bytes */
170 tag,
171 0, /* tid */
172 0 /* rid */);
173 wait_on_mask(1 << tag);
174 }
175
176
177