rsdShaderCache.h revision 41e373d91a60043afa0f9abd026218b49cbc1201
1/*
2 * Copyright (C) 2011 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#ifndef ANDROID_RSD_SHADER_CACHE_H
18#define ANDROID_RSD_SHADER_CACHE_H
19
20namespace android {
21namespace renderscript {
22
23class Context;
24
25}
26}
27
28#include <utils/String8.h>
29#include <utils/Vector.h>
30class RsdShader;
31
32// ---------------------------------------------------------------------------
33
34// An element is a group of Components that occupies one cell in a structure.
35class RsdShaderCache {
36public:
37    RsdShaderCache();
38    virtual ~RsdShaderCache();
39
40    void setActiveVertex(RsdShader *pv) {
41        mVertexDirty = true;
42        mVertex = pv;
43    }
44
45    void setActiveFragment(RsdShader *pf) {
46        mFragmentDirty = true;
47        mFragment = pf;
48    }
49
50    bool setup(const android::renderscript::Context *rsc);
51
52    void cleanupVertex(uint32_t id);
53    void cleanupFragment(uint32_t id);
54
55    void cleanupAll();
56
57    int32_t vtxAttribSlot(const android::String8 &attrName) const;
58    int32_t vtxUniformSlot(uint32_t a) const {return mCurrent->vtxUniforms[a].slot;}
59    uint32_t vtxUniformSize(uint32_t a) const {return mCurrent->vtxUniforms[a].arraySize;}
60    int32_t fragUniformSlot(uint32_t a) const {return mCurrent->fragUniforms[a].slot;}
61    uint32_t fragUniformSize(uint32_t a) const {return mCurrent->fragUniforms[a].arraySize;}
62
63protected:
64    bool link(const android::renderscript::Context *rsc);
65    bool mFragmentDirty;
66    bool mVertexDirty;
67    RsdShader *mVertex;
68    RsdShader *mFragment;
69
70    struct UniformQueryData {
71        char *name;
72        uint32_t nameLength;
73        int32_t writtenLength;
74        int32_t arraySize;
75        uint32_t type;
76        UniformQueryData(uint32_t maxName) {
77            name = NULL;
78            nameLength = maxName;
79            if (nameLength > 0 ) {
80                name = new char[nameLength];
81            }
82        }
83        ~UniformQueryData() {
84            if (name != NULL) {
85                delete[] name;
86                name = NULL;
87            }
88        }
89    };
90    struct UniformData {
91        int32_t slot;
92        uint32_t arraySize;
93    };
94    struct AttrData {
95        int32_t slot;
96        const char* name;
97    };
98    struct ProgramEntry {
99        ProgramEntry(uint32_t numVtxAttr, uint32_t numVtxUnis,
100                     uint32_t numFragUnis) : vtx(0), frag(0), program(0), vtxAttrCount(0),
101                                             vtxAttrs(0), vtxUniforms(0), fragUniforms(0) {
102            vtxAttrCount = numVtxAttr;
103            if (numVtxAttr) {
104                vtxAttrs = new AttrData[numVtxAttr];
105            }
106            if (numVtxUnis) {
107                vtxUniforms = new UniformData[numVtxUnis];
108            }
109            if (numFragUnis) {
110                fragUniforms = new UniformData[numFragUnis];
111                fragUniformIsSTO = new bool[numFragUnis];
112            }
113        }
114        ~ProgramEntry() {
115            if (vtxAttrs) {
116                delete[] vtxAttrs;
117                vtxAttrs = NULL;
118            }
119            if (vtxUniforms) {
120                delete[] vtxUniforms;
121                vtxUniforms = NULL;
122            }
123            if (fragUniforms) {
124                delete[] fragUniforms;
125                fragUniforms = NULL;
126            }
127            if (fragUniformIsSTO) {
128                delete[] fragUniformIsSTO;
129                fragUniformIsSTO = NULL;
130            }
131        }
132        uint32_t vtx;
133        uint32_t frag;
134        uint32_t program;
135        uint32_t vtxAttrCount;
136        AttrData *vtxAttrs;
137        UniformData *vtxUniforms;
138        UniformData *fragUniforms;
139        bool *fragUniformIsSTO;
140    };
141    android::Vector<ProgramEntry*> mEntries;
142    ProgramEntry *mCurrent;
143
144    bool hasArrayUniforms(RsdShader *vtx, RsdShader *frag);
145    void populateUniformData(RsdShader *prog, uint32_t linkedID, UniformData *data);
146    void updateUniformArrayData(const android::renderscript::Context *rsc,
147                                RsdShader *prog, uint32_t linkedID,
148                                UniformData *data, const char* logTag,
149                                UniformQueryData **uniformList, uint32_t uniListSize);
150};
151
152
153#endif //ANDROID_RSD_SHADER_CACHE_H
154
155
156
157
158