blob: 29f91bb606113f8c4e75ea4d60e9994420196355 [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
Jason Samsc460e552009-11-25 13:22:07 -080020namespace android {
21namespace renderscript {
22
Alex Sakhartchouka04e30d2011-04-29 16:49:08 -070023class Context;
24
25}
26}
27
Yang Nib8353c52015-02-14 18:00:59 -080028
29#if !defined(RS_SERVER) && !defined(RS_COMPATIBILITY_LIB)
30#include <utils/String8.h>
31#include <utils/Vector.h>
32#else
Tim Murray0b575de2013-03-15 15:56:43 -070033#include "rsUtils.h"
34#endif
Alex Sakhartchouka04e30d2011-04-29 16:49:08 -070035class RsdShader;
36
37// ---------------------------------------------------------------------------
Jason Samsc460e552009-11-25 13:22:07 -080038
39// An element is a group of Components that occupies one cell in a structure.
Alex Sakhartchouka04e30d2011-04-29 16:49:08 -070040class RsdShaderCache {
Jason Samsc460e552009-11-25 13:22:07 -080041public:
Alex Sakhartchouka04e30d2011-04-29 16:49:08 -070042 RsdShaderCache();
43 virtual ~RsdShaderCache();
Jason Samsc460e552009-11-25 13:22:07 -080044
Alex Sakhartchouka04e30d2011-04-29 16:49:08 -070045 void setActiveVertex(RsdShader *pv) {
46 mVertexDirty = true;
47 mVertex = pv;
48 }
49
50 void setActiveFragment(RsdShader *pf) {
51 mFragmentDirty = true;
52 mFragment = pf;
53 }
54
55 bool setup(const android::renderscript::Context *rsc);
Jason Samsc460e552009-11-25 13:22:07 -080056
Alex Sakhartchouk6d6e1142012-03-26 13:52:24 -070057 void cleanupVertex(RsdShader *s);
58 void cleanupFragment(RsdShader *s);
Jason Samsc460e552009-11-25 13:22:07 -080059
60 void cleanupAll();
61
Yang Nib8353c52015-02-14 18:00:59 -080062 int32_t vtxAttribSlot(const android::String8 &attrName) const;
Alex Sakhartchouk54929cc2010-11-08 15:10:52 -080063 int32_t vtxUniformSlot(uint32_t a) const {return mCurrent->vtxUniforms[a].slot;}
64 uint32_t vtxUniformSize(uint32_t a) const {return mCurrent->vtxUniforms[a].arraySize;}
65 int32_t fragUniformSlot(uint32_t a) const {return mCurrent->fragUniforms[a].slot;}
66 uint32_t fragUniformSize(uint32_t a) const {return mCurrent->fragUniforms[a].arraySize;}
Jason Samsc460e552009-11-25 13:22:07 -080067
68protected:
Alex Sakhartchouka04e30d2011-04-29 16:49:08 -070069 bool link(const android::renderscript::Context *rsc);
70 bool mFragmentDirty;
71 bool mVertexDirty;
72 RsdShader *mVertex;
73 RsdShader *mFragment;
74
Alex Sakhartchouk54929cc2010-11-08 15:10:52 -080075 struct UniformQueryData {
76 char *name;
77 uint32_t nameLength;
78 int32_t writtenLength;
79 int32_t arraySize;
80 uint32_t type;
81 UniformQueryData(uint32_t maxName) {
Chris Wailes44bef6f2014-08-12 13:51:10 -070082 name = nullptr;
Alex Sakhartchouk54929cc2010-11-08 15:10:52 -080083 nameLength = maxName;
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080084 if (nameLength > 0 ) {
Alex Sakhartchouk54929cc2010-11-08 15:10:52 -080085 name = new char[nameLength];
86 }
87 }
88 ~UniformQueryData() {
Chris Wailes44bef6f2014-08-12 13:51:10 -070089 if (name != nullptr) {
Alex Sakhartchouk54929cc2010-11-08 15:10:52 -080090 delete[] name;
Chris Wailes44bef6f2014-08-12 13:51:10 -070091 name = nullptr;
Alex Sakhartchouk54929cc2010-11-08 15:10:52 -080092 }
93 }
94 };
95 struct UniformData {
96 int32_t slot;
97 uint32_t arraySize;
98 };
99 struct AttrData {
100 int32_t slot;
101 const char* name;
102 };
103 struct ProgramEntry {
104 ProgramEntry(uint32_t numVtxAttr, uint32_t numVtxUnis,
105 uint32_t numFragUnis) : vtx(0), frag(0), program(0), vtxAttrCount(0),
Stephen Hinesebf4a142012-02-13 17:49:20 -0800106 vtxAttrs(0), vtxUniforms(0), fragUniforms(0),
107 fragUniformIsSTO(0) {
Alex Sakhartchouk54929cc2010-11-08 15:10:52 -0800108 vtxAttrCount = numVtxAttr;
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800109 if (numVtxAttr) {
Alex Sakhartchouk54929cc2010-11-08 15:10:52 -0800110 vtxAttrs = new AttrData[numVtxAttr];
111 }
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800112 if (numVtxUnis) {
Alex Sakhartchouk54929cc2010-11-08 15:10:52 -0800113 vtxUniforms = new UniformData[numVtxUnis];
114 }
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800115 if (numFragUnis) {
Alex Sakhartchouk54929cc2010-11-08 15:10:52 -0800116 fragUniforms = new UniformData[numFragUnis];
Jason Samscf27eb42012-02-10 13:24:18 -0800117 fragUniformIsSTO = new bool[numFragUnis];
Alex Sakhartchouk54929cc2010-11-08 15:10:52 -0800118 }
119 }
120 ~ProgramEntry() {
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800121 if (vtxAttrs) {
Alex Sakhartchouk54929cc2010-11-08 15:10:52 -0800122 delete[] vtxAttrs;
Chris Wailes44bef6f2014-08-12 13:51:10 -0700123 vtxAttrs = nullptr;
Alex Sakhartchouk54929cc2010-11-08 15:10:52 -0800124 }
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800125 if (vtxUniforms) {
Alex Sakhartchouk54929cc2010-11-08 15:10:52 -0800126 delete[] vtxUniforms;
Chris Wailes44bef6f2014-08-12 13:51:10 -0700127 vtxUniforms = nullptr;
Alex Sakhartchouk54929cc2010-11-08 15:10:52 -0800128 }
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800129 if (fragUniforms) {
Alex Sakhartchouk54929cc2010-11-08 15:10:52 -0800130 delete[] fragUniforms;
Chris Wailes44bef6f2014-08-12 13:51:10 -0700131 fragUniforms = nullptr;
Alex Sakhartchouk54929cc2010-11-08 15:10:52 -0800132 }
Jason Samscf27eb42012-02-10 13:24:18 -0800133 if (fragUniformIsSTO) {
134 delete[] fragUniformIsSTO;
Chris Wailes44bef6f2014-08-12 13:51:10 -0700135 fragUniformIsSTO = nullptr;
Jason Samscf27eb42012-02-10 13:24:18 -0800136 }
Alex Sakhartchouk54929cc2010-11-08 15:10:52 -0800137 }
Jason Samsc460e552009-11-25 13:22:07 -0800138 uint32_t vtx;
139 uint32_t frag;
140 uint32_t program;
Alex Sakhartchouk886f11a2010-09-29 09:49:13 -0700141 uint32_t vtxAttrCount;
Alex Sakhartchouk54929cc2010-11-08 15:10:52 -0800142 AttrData *vtxAttrs;
143 UniformData *vtxUniforms;
144 UniformData *fragUniforms;
Jason Samscf27eb42012-02-10 13:24:18 -0800145 bool *fragUniformIsSTO;
Alex Sakhartchouk54929cc2010-11-08 15:10:52 -0800146 };
Yang Nib8353c52015-02-14 18:00:59 -0800147 android::Vector<ProgramEntry*> mEntries;
Alex Sakhartchouk54929cc2010-11-08 15:10:52 -0800148 ProgramEntry *mCurrent;
Jason Samsc460e552009-11-25 13:22:07 -0800149
Alex Sakhartchouka04e30d2011-04-29 16:49:08 -0700150 bool hasArrayUniforms(RsdShader *vtx, RsdShader *frag);
151 void populateUniformData(RsdShader *prog, uint32_t linkedID, UniformData *data);
152 void updateUniformArrayData(const android::renderscript::Context *rsc,
153 RsdShader *prog, uint32_t linkedID,
Alex Sakhartchouk54929cc2010-11-08 15:10:52 -0800154 UniformData *data, const char* logTag,
155 UniformQueryData **uniformList, uint32_t uniListSize);
Jason Samsc460e552009-11-25 13:22:07 -0800156};
157
158
Alex Sakhartchouka04e30d2011-04-29 16:49:08 -0700159#endif //ANDROID_RSD_SHADER_CACHE_H
Yang Nib8353c52015-02-14 18:00:59 -0800160
161
162
163