Tizen 2.0 Release
[profile/ivi/osmesa.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 <simdmath/exp2f4.h>
42 #include <simdmath/log2f4.h>
43 #include <simdmath/powf4.h>
44
45 #include "cell/common.h"
46 #include "spu_main.h"
47 #include "spu_funcs.h"
48 #include "spu_texture.h"
49
50
51 /** For "return"-ing four vectors */
52 struct vec_4x4
53 {
54    vector float v[4];
55 };
56
57
58 static vector float
59 spu_cos(vector float x)
60 {
61    return _cos14_v(x);
62 }
63
64 static vector float
65 spu_sin(vector float x)
66 {
67    return _sin14_v(x);
68 }
69
70 static vector float
71 spu_pow(vector float x, vector float y)
72 {
73    return _powf4(x, y);
74 }
75
76 static vector float
77 spu_exp2(vector float x)
78 {
79    return _exp2f4(x);
80 }
81
82 static vector float
83 spu_log2(vector float x)
84 {
85    return _log2f4(x);
86 }
87
88
89 static struct vec_4x4
90 spu_tex_2d(vector float s, vector float t, vector float r, vector float q,
91            unsigned unit)
92 {
93    struct vec_4x4 colors;
94    (void) r;
95    (void) q;
96    spu.sample_texture_2d[unit](s, t, unit, 0, 0, colors.v);
97    return colors;
98 }
99
100 static struct vec_4x4
101 spu_tex_3d(vector float s, vector float t, vector float r, vector float q,
102            unsigned unit)
103 {
104    struct vec_4x4 colors;
105    (void) r;
106    (void) q;
107    spu.sample_texture_2d[unit](s, t, unit, 0, 0, colors.v);
108    return colors;
109 }
110
111 static struct vec_4x4
112 spu_tex_cube(vector float s, vector float t, vector float r, vector float q,
113            unsigned unit)
114 {
115    struct vec_4x4 colors;
116    (void) q;
117    sample_texture_cube(s, t, r, unit, colors.v);
118    return colors;
119 }
120
121
122 /**
123  * Add named function to list of "exported" functions that will be
124  * made available to the PPU-hosted code generator.
125  */
126 static void
127 export_func(struct cell_spu_function_info *spu_functions,
128             const char *name, void *addr)
129 {
130    uint n = spu_functions->num;
131    ASSERT(strlen(name) < 16);
132    strcpy(spu_functions->names[n], name);
133    spu_functions->addrs[n] = (uint) addr;
134    spu_functions->num++;
135    ASSERT(spu_functions->num <= 16);
136 }
137
138
139 /**
140  * Return info about the SPU's function to the PPU / main memory.
141  * The PPU needs to know the address of some SPU-side functions so
142  * that we can generate shader code with function calls.
143  */
144 void
145 return_function_info(void)
146 {
147    PIPE_ALIGN_VAR(16) struct cell_spu_function_info funcs;
148    int tag = TAG_MISC;
149
150    ASSERT(sizeof(funcs) == 256); /* must be multiple of 16 bytes */
151
152    funcs.num = 0;
153    export_func(&funcs, "spu_cos", &spu_cos);
154    export_func(&funcs, "spu_sin", &spu_sin);
155    export_func(&funcs, "spu_pow", &spu_pow);
156    export_func(&funcs, "spu_exp2", &spu_exp2);
157    export_func(&funcs, "spu_log2", &spu_log2);
158    export_func(&funcs, "spu_tex_2d", &spu_tex_2d);
159    export_func(&funcs, "spu_tex_3d", &spu_tex_3d);
160    export_func(&funcs, "spu_tex_cube", &spu_tex_cube);
161
162    /* Send the function info back to the PPU / main memory */
163    mfc_put((void *) &funcs,  /* src in local store */
164            (unsigned int) spu.init.spu_functions, /* dst in main memory */
165            sizeof(funcs),  /* bytes */
166            tag,
167            0, /* tid */
168            0  /* rid */);
169    wait_on_mask(1 << tag);
170 }
171
172
173