1/*
2 * Copyright (C) 2009 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "rsComponent.h"
18
19#include <GLES/gl.h>
20
21using namespace android;
22using namespace android::renderscript;
23
24Component::Component()
25{
26    set(RS_TYPE_NONE, RS_KIND_USER, false, 1);
27}
28
29Component::~Component()
30{
31}
32
33void Component::set(RsDataType dt, RsDataKind dk, bool norm, uint32_t vecSize)
34{
35    mType = dt;
36    mKind = dk;
37    mNormalized = norm;
38    mVectorSize = vecSize;
39    rsAssert(vecSize <= 4);
40
41    mBits = 0;
42    mTypeBits = 0;
43    mIsFloat = false;
44    mIsSigned = false;
45    mIsPixel = false;
46
47    switch(mKind) {
48    case RS_KIND_PIXEL_L:
49    case RS_KIND_PIXEL_A:
50        mIsPixel = true;
51        rsAssert(mVectorSize == 1);
52        rsAssert(mNormalized == true);
53        break;
54    case RS_KIND_PIXEL_LA:
55        mIsPixel = true;
56        rsAssert(mVectorSize == 2);
57        rsAssert(mNormalized == true);
58        break;
59    case RS_KIND_PIXEL_RGB:
60        mIsPixel = true;
61        rsAssert(mVectorSize == 3);
62        rsAssert(mNormalized == true);
63        break;
64    case RS_KIND_PIXEL_RGBA:
65        mIsPixel = true;
66        rsAssert(mVectorSize == 4);
67        rsAssert(mNormalized == true);
68        break;
69    default:
70        break;
71    }
72
73    switch(mType) {
74    case RS_TYPE_NONE:
75        return;
76    case RS_TYPE_UNSIGNED_5_6_5:
77        mVectorSize = 3;
78        mBits = 16;
79        mNormalized = true;
80        rsAssert(mKind == RS_KIND_PIXEL_RGB);
81        return;
82    case RS_TYPE_UNSIGNED_5_5_5_1:
83        mVectorSize = 4;
84        mBits = 16;
85        mNormalized = true;
86        rsAssert(mKind == RS_KIND_PIXEL_RGBA);
87        return;
88    case RS_TYPE_UNSIGNED_4_4_4_4:
89        mVectorSize = 4;
90        mBits = 16;
91        mNormalized = true;
92        rsAssert(mKind == RS_KIND_PIXEL_RGBA);
93        return;
94    case RS_TYPE_ELEMENT:
95    case RS_TYPE_TYPE:
96    case RS_TYPE_ALLOCATION:
97    case RS_TYPE_SAMPLER:
98    case RS_TYPE_SCRIPT:
99    case RS_TYPE_MESH:
100    case RS_TYPE_PROGRAM_FRAGMENT:
101    case RS_TYPE_PROGRAM_VERTEX:
102    case RS_TYPE_PROGRAM_RASTER:
103    case RS_TYPE_PROGRAM_STORE:
104        rsAssert(mVectorSize == 1);
105        rsAssert(mNormalized == false);
106        rsAssert(mKind == RS_KIND_USER);
107        mBits = 32;
108        mTypeBits = 32;
109        return;
110
111    case RS_TYPE_FLOAT_16:
112        mTypeBits = 16;
113        mIsFloat = true;
114        break;
115    case RS_TYPE_FLOAT_32:
116        mTypeBits = 32;
117        mIsFloat = true;
118        break;
119    case RS_TYPE_FLOAT_64:
120        mTypeBits = 64;
121        mIsFloat = true;
122        break;
123    case RS_TYPE_SIGNED_8:
124        mTypeBits = 8;
125        mIsSigned = true;
126        break;
127    case RS_TYPE_SIGNED_16:
128        mTypeBits = 16;
129        mIsSigned = true;
130        break;
131    case RS_TYPE_SIGNED_32:
132        mTypeBits = 32;
133        mIsSigned = true;
134        break;
135    case RS_TYPE_SIGNED_64:
136        mTypeBits = 64;
137        mIsSigned = true;
138        break;
139    case RS_TYPE_UNSIGNED_8:
140        mTypeBits = 8;
141        break;
142    case RS_TYPE_UNSIGNED_16:
143        mTypeBits = 16;
144        break;
145    case RS_TYPE_UNSIGNED_32:
146        mTypeBits = 32;
147        break;
148    case RS_TYPE_UNSIGNED_64:
149        mTypeBits = 64;
150        break;
151    }
152
153    mBits = mTypeBits * mVectorSize;
154}
155
156
157
158
159uint32_t Component::getGLType() const
160{
161    switch (mType) {
162    case RS_TYPE_UNSIGNED_5_6_5:    return GL_UNSIGNED_SHORT_5_6_5;
163    case RS_TYPE_UNSIGNED_5_5_5_1:  return GL_UNSIGNED_SHORT_5_5_5_1;
164    case RS_TYPE_UNSIGNED_4_4_4_4:  return GL_UNSIGNED_SHORT_4_4_4_4;
165
166    //case RS_TYPE_FLOAT_16:      return GL_HALF_FLOAT;
167    case RS_TYPE_FLOAT_32:      return GL_FLOAT;
168    case RS_TYPE_UNSIGNED_8:    return GL_UNSIGNED_BYTE;
169    case RS_TYPE_UNSIGNED_16:   return GL_UNSIGNED_SHORT;
170    case RS_TYPE_SIGNED_8:      return GL_BYTE;
171    case RS_TYPE_SIGNED_16:     return GL_SHORT;
172    default:    break;
173    }
174
175    return 0;
176}
177
178uint32_t Component::getGLFormat() const
179{
180    switch (mKind) {
181    case RS_KIND_PIXEL_L: return GL_LUMINANCE;
182    case RS_KIND_PIXEL_A: return GL_ALPHA;
183    case RS_KIND_PIXEL_LA: return GL_LUMINANCE_ALPHA;
184    case RS_KIND_PIXEL_RGB: return GL_RGB;
185    case RS_KIND_PIXEL_RGBA: return GL_RGBA;
186    default: break;
187    }
188    return 0;
189}
190
191static const char * gCTypeStrings[] = {
192    0,
193    0,//"F16",
194    "float",
195    "double",
196    "char",
197    "short",
198    "int",
199    0,//"S64",
200    "char",//U8",
201    "short",//U16",
202    "int",//U32",
203    0,//"U64",
204    0,//"UP_565",
205    0,//"UP_5551",
206    0,//"UP_4444",
207    0,//"ELEMENT",
208    0,//"TYPE",
209    0,//"ALLOCATION",
210    0,//"SAMPLER",
211    0,//"SCRIPT",
212    0,//"MESH",
213    0,//"PROGRAM_FRAGMENT",
214    0,//"PROGRAM_VERTEX",
215    0,//"PROGRAM_RASTER",
216    0,//"PROGRAM_STORE",
217};
218
219static const char * gCVecTypeStrings[] = {
220    0,
221    0,//"F16",
222    "vecF32",
223    "vecF64",
224    "vecI8",
225    "vecI16",
226    "vecI32",
227    0,//"S64",
228    "vecU8",//U8",
229    "vecU16",//U16",
230    "vecU32",//U32",
231    0,//"U64",
232    0,//"UP_565",
233    0,//"UP_5551",
234    0,//"UP_4444",
235    0,//"ELEMENT",
236    0,//"TYPE",
237    0,//"ALLOCATION",
238    0,//"SAMPLER",
239    0,//"SCRIPT",
240    0,//"MESH",
241    0,//"PROGRAM_FRAGMENT",
242    0,//"PROGRAM_VERTEX",
243    0,//"PROGRAM_RASTER",
244    0,//"PROGRAM_STORE",
245};
246
247String8 Component::getCType() const
248{
249    char buf[64];
250    if (mVectorSize == 1) {
251        return String8(gCTypeStrings[mType]);
252    }
253
254    // Yuck, acc WAR
255    // Appears to have problems packing chars
256    if (mVectorSize == 4 && mType == RS_TYPE_UNSIGNED_8) {
257        return String8("int");
258    }
259
260
261    String8 s(gCVecTypeStrings[mType]);
262    sprintf(buf, "_%i_t", mVectorSize);
263    s.append(buf);
264    return s;
265}
266
267String8 Component::getGLSLType() const
268{
269    if (mType == RS_TYPE_SIGNED_32) {
270        switch(mVectorSize) {
271        case 1: return String8("int");
272        case 2: return String8("ivec2");
273        case 3: return String8("ivec3");
274        case 4: return String8("ivec4");
275        }
276    }
277    if (mType == RS_TYPE_FLOAT_32) {
278        switch(mVectorSize) {
279        case 1: return String8("float");
280        case 2: return String8("vec2");
281        case 3: return String8("vec3");
282        case 4: return String8("vec4");
283        }
284    }
285    return String8();
286}
287
288static const char * gTypeStrings[] = {
289    "NONE",
290    "F16",
291    "F32",
292    "F64",
293    "S8",
294    "S16",
295    "S32",
296    "S64",
297    "U8",
298    "U16",
299    "U32",
300    "U64",
301    "UP_565",
302    "UP_5551",
303    "UP_4444",
304    "ELEMENT",
305    "TYPE",
306    "ALLOCATION",
307    "SAMPLER",
308    "SCRIPT",
309    "MESH",
310    "PROGRAM_FRAGMENT",
311    "PROGRAM_VERTEX",
312    "PROGRAM_RASTER",
313    "PROGRAM_STORE",
314};
315
316static const char * gKindStrings[] = {
317    "USER",
318    "COLOR",
319    "POSITION",
320    "TEXTURE",
321    "NORMAL",
322    "INDEX",
323    "POINT_SIZE",
324    "PIXEL_L",
325    "PIXEL_A",
326    "PIXEL_LA",
327    "PIXEL_RGB",
328    "PIXEL_RGBA",
329};
330
331void Component::dumpLOGV(const char *prefix) const
332{
333    LOGV("%s   Component: %s, %s, vectorSize=%i, bits=%i",
334         prefix, gTypeStrings[mType], gKindStrings[mKind], mVectorSize, mBits);
335}
336
337
338