1 /**************************************************************************
3 * Copyright 2008 Tungsten Graphics, Inc., Cedar Park, Texas.
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:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
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.
26 **************************************************************************/
30 * SPU functions accessed by shaders.
41 #include <transpose_matrix4x4.h>
43 #include "cell/common.h"
45 #include "spu_funcs.h"
48 /** For "return"-ing four vectors */
56 spu_cos(vector
float x
)
62 spu_sin(vector
float x
)
68 spu_pow(vector
float x
, vector
float y
)
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
};
78 spu_exp2(vector
float x
)
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
};
88 spu_log2(vector
float x
)
91 * log_base_2(x) = log(x) / log(2)
92 * 1.442695 = 1/log(2).
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
);
103 static struct vec_4x4
104 spu_txp(vector
float s
, vector
float t
, vector
float r
, vector
float q
,
107 //const uint unit = 0;
108 struct vec_4x4 colors
;
109 vector
float coords
[4];
115 _transpose_matrix4x4(coords
, coords
);
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]);
123 _transpose_matrix4x4(colors
.v
, colors
.v
);
129 * Add named function to list of "exported" functions that will be
130 * made available to the PPU-hosted code generator.
133 export_func(struct cell_spu_function_info
*spu_functions
,
134 const char *name
, void *addr
)
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);
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.
151 return_function_info(void)
153 struct cell_spu_function_info funcs ALIGN16_ATTRIB
;
156 ASSERT(sizeof(funcs
) == 256); /* must be multiple of 16 bytes */
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
);
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 */
173 wait_on_mask(1 << tag
);