blob: e782fe5d5f298f3bd2081aaf56bc9a128ac86305 [file] [log] [blame]
Jason Samsc460e552009-11-25 13:22:07 -08001/*
Jason Sams3522f402012-03-23 11:47:26 -07002 * Copyright (C) 2011-2012 The Android Open Source Project
Jason Samsc460e552009-11-25 13:22:07 -08003 *
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
Alex Sakhartchouka04e30d2011-04-29 16:49:08 -070017#ifndef ANDROID_RSD_SHADER_CACHE_H
18#define ANDROID_RSD_SHADER_CACHE_H
Jason Samsc460e552009-11-25 13:22:07 -080019
Chris Wailes93d6bc82014-07-28 16:54:38 -070020#include <string>
21#include <vector>
22
Jason Samsc460e552009-11-25 13:22:07 -080023namespace android {
24namespace renderscript {
25
Alex Sakhartchouka04e30d2011-04-29 16:49:08 -070026class Context;
27
28}
29}
30
Stephen Hinesb0934b62013-07-03 17:27:38 -070031#if !defined(RS_SERVER) && !defined(RS_COMPATIBILITY_LIB)
Alex Sakhartchouka04e30d2011-04-29 16:49:08 -070032#include <utils/String8.h>
Tim Murray0b575de2013-03-15 15:56:43 -070033#else
34#include "rsUtils.h"
35#endif
Alex Sakhartchouka04e30d2011-04-29 16:49:08 -070036class RsdShader;
37
38// ---------------------------------------------------------------------------
Jason Samsc460e552009-11-25 13:22:07 -080039
40// An element is a group of Components that occupies one cell in a structure.
Alex Sakhartchouka04e30d2011-04-29 16:49:08 -070041class RsdShaderCache {
Jason Samsc460e552009-11-25 13:22:07 -080042public:
Alex Sakhartchouka04e30d2011-04-29 16:49:08 -070043 RsdShaderCache();
44 virtual ~RsdShaderCache();
Jason Samsc460e552009-11-25 13:22:07 -080045
Alex Sakhartchouka04e30d2011-04-29 16:49:08 -070046 void setActiveVertex(RsdShader *pv) {
47 mVertexDirty = true;
48 mVertex = pv;
49 }
50
51 void setActiveFragment(RsdShader *pf) {
52 mFragmentDirty = true;
53 mFragment = pf;
54 }
55
56 bool setup(const android::renderscript::Context *rsc);
Jason Samsc460e552009-11-25 13:22:07 -080057
Alex Sakhartchouk6d6e1142012-03-26 13:52:24 -070058 void cleanupVertex(RsdShader *s);
59 void cleanupFragment(RsdShader *s);
Jason Samsc460e552009-11-25 13:22:07 -080060
61 void cleanupAll();
62
Chris Wailes93d6bc82014-07-28 16:54:38 -070063 int32_t vtxAttribSlot(const std::string &attrName) const;
Alex Sakhartchouk54929cc2010-11-08 15:10:52 -080064 int32_t vtxUniformSlot(uint32_t a) const {return mCurrent->vtxUniforms[a].slot;}
65 uint32_t vtxUniformSize(uint32_t a) const {return mCurrent->vtxUniforms[a].arraySize;}
66 int32_t fragUniformSlot(uint32_t a) const {return mCurrent->fragUniforms[a].slot;}
67 uint32_t fragUniformSize(uint32_t a) const {return mCurrent->fragUniforms[a].arraySize;}
Jason Samsc460e552009-11-25 13:22:07 -080068
69protected:
Alex Sakhartchouka04e30d2011-04-29 16:49:08 -070070 bool link(const android::renderscript::Context *rsc);
71 bool mFragmentDirty;
72 bool mVertexDirty;
73 RsdShader *mVertex;
74 RsdShader *mFragment;
75
Alex Sakhartchouk54929cc2010-11-08 15:10:52 -080076 struct UniformQueryData {
77 char *name;
78 uint32_t nameLength;
79 int32_t writtenLength;
80 int32_t arraySize;
81 uint32_t type;
82 UniformQueryData(uint32_t maxName) {
83 name = NULL;
84 nameLength = maxName;
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080085 if (nameLength > 0 ) {
Alex Sakhartchouk54929cc2010-11-08 15:10:52 -080086 name = new char[nameLength];
87 }
88 }
89 ~UniformQueryData() {
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080090 if (name != NULL) {
Alex Sakhartchouk54929cc2010-11-08 15:10:52 -080091 delete[] name;
92 name = NULL;
93 }
94 }
95 };
96 struct UniformData {
97 int32_t slot;
98 uint32_t arraySize;
99 };
100 struct AttrData {
101 int32_t slot;
102 const char* name;
103 };
104 struct ProgramEntry {
105 ProgramEntry(uint32_t numVtxAttr, uint32_t numVtxUnis,
106 uint32_t numFragUnis) : vtx(0), frag(0), program(0), vtxAttrCount(0),
Stephen Hinesebf4a142012-02-13 17:49:20 -0800107 vtxAttrs(0), vtxUniforms(0), fragUniforms(0),
108 fragUniformIsSTO(0) {
Alex Sakhartchouk54929cc2010-11-08 15:10:52 -0800109 vtxAttrCount = numVtxAttr;
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800110 if (numVtxAttr) {
Alex Sakhartchouk54929cc2010-11-08 15:10:52 -0800111 vtxAttrs = new AttrData[numVtxAttr];
112 }
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800113 if (numVtxUnis) {
Alex Sakhartchouk54929cc2010-11-08 15:10:52 -0800114 vtxUniforms = new UniformData[numVtxUnis];
115 }
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800116 if (numFragUnis) {
Alex Sakhartchouk54929cc2010-11-08 15:10:52 -0800117 fragUniforms = new UniformData[numFragUnis];
Jason Samscf27eb42012-02-10 13:24:18 -0800118 fragUniformIsSTO = new bool[numFragUnis];
Alex Sakhartchouk54929cc2010-11-08 15:10:52 -0800119 }
120 }
121 ~ProgramEntry() {
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800122 if (vtxAttrs) {
Alex Sakhartchouk54929cc2010-11-08 15:10:52 -0800123 delete[] vtxAttrs;
124 vtxAttrs = NULL;
125 }
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800126 if (vtxUniforms) {
Alex Sakhartchouk54929cc2010-11-08 15:10:52 -0800127 delete[] vtxUniforms;
128 vtxUniforms = NULL;
129 }
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800130 if (fragUniforms) {
Alex Sakhartchouk54929cc2010-11-08 15:10:52 -0800131 delete[] fragUniforms;
132 fragUniforms = NULL;
133 }
Jason Samscf27eb42012-02-10 13:24:18 -0800134 if (fragUniformIsSTO) {
135 delete[] fragUniformIsSTO;
136 fragUniformIsSTO = NULL;
137 }
Alex Sakhartchouk54929cc2010-11-08 15:10:52 -0800138 }
Jason Samsc460e552009-11-25 13:22:07 -0800139 uint32_t vtx;
140 uint32_t frag;
141 uint32_t program;
Alex Sakhartchouk886f11a2010-09-29 09:49:13 -0700142 uint32_t vtxAttrCount;
Alex Sakhartchouk54929cc2010-11-08 15:10:52 -0800143 AttrData *vtxAttrs;
144 UniformData *vtxUniforms;
145 UniformData *fragUniforms;
Jason Samscf27eb42012-02-10 13:24:18 -0800146 bool *fragUniformIsSTO;
Alex Sakhartchouk54929cc2010-11-08 15:10:52 -0800147 };
Chris Wailes93d6bc82014-07-28 16:54:38 -0700148 std::vector<ProgramEntry*> mEntries;
Alex Sakhartchouk54929cc2010-11-08 15:10:52 -0800149 ProgramEntry *mCurrent;
Jason Samsc460e552009-11-25 13:22:07 -0800150
Alex Sakhartchouka04e30d2011-04-29 16:49:08 -0700151 bool hasArrayUniforms(RsdShader *vtx, RsdShader *frag);
152 void populateUniformData(RsdShader *prog, uint32_t linkedID, UniformData *data);
153 void updateUniformArrayData(const android::renderscript::Context *rsc,
154 RsdShader *prog, uint32_t linkedID,
Alex Sakhartchouk54929cc2010-11-08 15:10:52 -0800155 UniformData *data, const char* logTag,
156 UniformQueryData **uniformList, uint32_t uniListSize);
Jason Samsc460e552009-11-25 13:22:07 -0800157};
158
159
Alex Sakhartchouka04e30d2011-04-29 16:49:08 -0700160#endif //ANDROID_RSD_SHADER_CACHE_H