blob: 354036697e906a3a95f53427e1f2b53083873308 [file] [log] [blame]
Jason Samsc460e552009-11-25 13:22:07 -08001/*
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#ifndef ANDROID_SHADER_CACHE_H
18#define ANDROID_SHADER_CACHE_H
19
20
21#include "rsObjectBase.h"
22#include "rsVertexArray.h"
23
24// ---------------------------------------------------------------------------
25namespace android {
26namespace renderscript {
27
28
29// An element is a group of Components that occupies one cell in a structure.
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080030class ShaderCache {
Jason Samsc460e552009-11-25 13:22:07 -080031public:
32 ShaderCache();
33 virtual ~ShaderCache();
34
Jason Samscd506532009-12-15 19:10:11 -080035 bool lookup(Context *rsc, ProgramVertex *, ProgramFragment *);
Jason Samsc460e552009-11-25 13:22:07 -080036
37 void cleanupVertex(uint32_t id);
38 void cleanupFragment(uint32_t id);
39
40 void cleanupAll();
41
Alex Sakhartchouk886f11a2010-09-29 09:49:13 -070042 int32_t vtxAttribSlot(const String8 &attrName) const;
Alex Sakhartchouk54929cc2010-11-08 15:10:52 -080043 int32_t vtxUniformSlot(uint32_t a) const {return mCurrent->vtxUniforms[a].slot;}
44 uint32_t vtxUniformSize(uint32_t a) const {return mCurrent->vtxUniforms[a].arraySize;}
45 int32_t fragUniformSlot(uint32_t a) const {return mCurrent->fragUniforms[a].slot;}
46 uint32_t fragUniformSize(uint32_t a) const {return mCurrent->fragUniforms[a].arraySize;}
Jason Samsc460e552009-11-25 13:22:07 -080047
48protected:
Alex Sakhartchouk54929cc2010-11-08 15:10:52 -080049 struct UniformQueryData {
50 char *name;
51 uint32_t nameLength;
52 int32_t writtenLength;
53 int32_t arraySize;
54 uint32_t type;
55 UniformQueryData(uint32_t maxName) {
56 name = NULL;
57 nameLength = maxName;
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080058 if (nameLength > 0 ) {
Alex Sakhartchouk54929cc2010-11-08 15:10:52 -080059 name = new char[nameLength];
60 }
61 }
62 ~UniformQueryData() {
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080063 if (name != NULL) {
Alex Sakhartchouk54929cc2010-11-08 15:10:52 -080064 delete[] name;
65 name = NULL;
66 }
67 }
68 };
69 struct UniformData {
70 int32_t slot;
71 uint32_t arraySize;
72 };
73 struct AttrData {
74 int32_t slot;
75 const char* name;
76 };
77 struct ProgramEntry {
78 ProgramEntry(uint32_t numVtxAttr, uint32_t numVtxUnis,
79 uint32_t numFragUnis) : vtx(0), frag(0), program(0), vtxAttrCount(0),
80 vtxAttrs(0), vtxUniforms(0), fragUniforms(0) {
81 vtxAttrCount = numVtxAttr;
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080082 if (numVtxAttr) {
Alex Sakhartchouk54929cc2010-11-08 15:10:52 -080083 vtxAttrs = new AttrData[numVtxAttr];
84 }
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080085 if (numVtxUnis) {
Alex Sakhartchouk54929cc2010-11-08 15:10:52 -080086 vtxUniforms = new UniformData[numVtxUnis];
87 }
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080088 if (numFragUnis) {
Alex Sakhartchouk54929cc2010-11-08 15:10:52 -080089 fragUniforms = new UniformData[numFragUnis];
90 }
91 }
92 ~ProgramEntry() {
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080093 if (vtxAttrs) {
Alex Sakhartchouk54929cc2010-11-08 15:10:52 -080094 delete[] vtxAttrs;
95 vtxAttrs = NULL;
96 }
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080097 if (vtxUniforms) {
Alex Sakhartchouk54929cc2010-11-08 15:10:52 -080098 delete[] vtxUniforms;
99 vtxUniforms = NULL;
100 }
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800101 if (fragUniforms) {
Alex Sakhartchouk54929cc2010-11-08 15:10:52 -0800102 delete[] fragUniforms;
103 fragUniforms = NULL;
104 }
105 }
Jason Samsc460e552009-11-25 13:22:07 -0800106 uint32_t vtx;
107 uint32_t frag;
108 uint32_t program;
Alex Sakhartchouk886f11a2010-09-29 09:49:13 -0700109 uint32_t vtxAttrCount;
Alex Sakhartchouk54929cc2010-11-08 15:10:52 -0800110 AttrData *vtxAttrs;
111 UniformData *vtxUniforms;
112 UniformData *fragUniforms;
Alex Sakhartchouk54929cc2010-11-08 15:10:52 -0800113 };
114 Vector<ProgramEntry*> mEntries;
115 ProgramEntry *mCurrent;
Jason Samsc460e552009-11-25 13:22:07 -0800116
Alex Sakhartchouk54929cc2010-11-08 15:10:52 -0800117 bool hasArrayUniforms(ProgramVertex *vtx, ProgramFragment *frag);
118 void populateUniformData(Program *prog, uint32_t linkedID, UniformData *data);
119 void updateUniformArrayData(Context *rsc, Program *prog, uint32_t linkedID,
120 UniformData *data, const char* logTag,
121 UniformQueryData **uniformList, uint32_t uniListSize);
Jason Samsc460e552009-11-25 13:22:07 -0800122};
123
124
125
126}
127}
128#endif //ANDROID_SHADER_CACHE_H
129
130
131
132