blob: de195e6087fd4e3d75a3ad89a0742aa1fbf0b2b2 [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
Chris Wailes6847e732014-08-11 17:30:51 -070031#if defined(RS_SERVER) || defined(RS_COMPATIBILITY_LIB)
Tim Murray0b575de2013-03-15 15:56:43 -070032#include "rsUtils.h"
33#endif
Alex Sakhartchouka04e30d2011-04-29 16:49:08 -070034class RsdShader;
35
36// ---------------------------------------------------------------------------
Jason Samsc460e552009-11-25 13:22:07 -080037
38// An element is a group of Components that occupies one cell in a structure.
Alex Sakhartchouka04e30d2011-04-29 16:49:08 -070039class RsdShaderCache {
Jason Samsc460e552009-11-25 13:22:07 -080040public:
Alex Sakhartchouka04e30d2011-04-29 16:49:08 -070041 RsdShaderCache();
42 virtual ~RsdShaderCache();
Jason Samsc460e552009-11-25 13:22:07 -080043
Alex Sakhartchouka04e30d2011-04-29 16:49:08 -070044 void setActiveVertex(RsdShader *pv) {
45 mVertexDirty = true;
46 mVertex = pv;
47 }
48
49 void setActiveFragment(RsdShader *pf) {
50 mFragmentDirty = true;
51 mFragment = pf;
52 }
53
54 bool setup(const android::renderscript::Context *rsc);
Jason Samsc460e552009-11-25 13:22:07 -080055
Alex Sakhartchouk6d6e1142012-03-26 13:52:24 -070056 void cleanupVertex(RsdShader *s);
57 void cleanupFragment(RsdShader *s);
Jason Samsc460e552009-11-25 13:22:07 -080058
59 void cleanupAll();
60
Chris Wailes93d6bc82014-07-28 16:54:38 -070061 int32_t vtxAttribSlot(const std::string &attrName) const;
Alex Sakhartchouk54929cc2010-11-08 15:10:52 -080062 int32_t vtxUniformSlot(uint32_t a) const {return mCurrent->vtxUniforms[a].slot;}
63 uint32_t vtxUniformSize(uint32_t a) const {return mCurrent->vtxUniforms[a].arraySize;}
64 int32_t fragUniformSlot(uint32_t a) const {return mCurrent->fragUniforms[a].slot;}
65 uint32_t fragUniformSize(uint32_t a) const {return mCurrent->fragUniforms[a].arraySize;}
Jason Samsc460e552009-11-25 13:22:07 -080066
67protected:
Alex Sakhartchouka04e30d2011-04-29 16:49:08 -070068 bool link(const android::renderscript::Context *rsc);
69 bool mFragmentDirty;
70 bool mVertexDirty;
71 RsdShader *mVertex;
72 RsdShader *mFragment;
73
Alex Sakhartchouk54929cc2010-11-08 15:10:52 -080074 struct UniformQueryData {
75 char *name;
76 uint32_t nameLength;
77 int32_t writtenLength;
78 int32_t arraySize;
79 uint32_t type;
80 UniformQueryData(uint32_t maxName) {
Chris Wailes44bef6f2014-08-12 13:51:10 -070081 name = nullptr;
Alex Sakhartchouk54929cc2010-11-08 15:10:52 -080082 nameLength = maxName;
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080083 if (nameLength > 0 ) {
Alex Sakhartchouk54929cc2010-11-08 15:10:52 -080084 name = new char[nameLength];
85 }
86 }
87 ~UniformQueryData() {
Chris Wailes44bef6f2014-08-12 13:51:10 -070088 if (name != nullptr) {
Alex Sakhartchouk54929cc2010-11-08 15:10:52 -080089 delete[] name;
Chris Wailes44bef6f2014-08-12 13:51:10 -070090 name = nullptr;
Alex Sakhartchouk54929cc2010-11-08 15:10:52 -080091 }
92 }
93 };
94 struct UniformData {
95 int32_t slot;
96 uint32_t arraySize;
97 };
98 struct AttrData {
99 int32_t slot;
100 const char* name;
101 };
102 struct ProgramEntry {
103 ProgramEntry(uint32_t numVtxAttr, uint32_t numVtxUnis,
104 uint32_t numFragUnis) : vtx(0), frag(0), program(0), vtxAttrCount(0),
Stephen Hinesebf4a142012-02-13 17:49:20 -0800105 vtxAttrs(0), vtxUniforms(0), fragUniforms(0),
106 fragUniformIsSTO(0) {
Alex Sakhartchouk54929cc2010-11-08 15:10:52 -0800107 vtxAttrCount = numVtxAttr;
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800108 if (numVtxAttr) {
Alex Sakhartchouk54929cc2010-11-08 15:10:52 -0800109 vtxAttrs = new AttrData[numVtxAttr];
110 }
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800111 if (numVtxUnis) {
Alex Sakhartchouk54929cc2010-11-08 15:10:52 -0800112 vtxUniforms = new UniformData[numVtxUnis];
113 }
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800114 if (numFragUnis) {
Alex Sakhartchouk54929cc2010-11-08 15:10:52 -0800115 fragUniforms = new UniformData[numFragUnis];
Jason Samscf27eb42012-02-10 13:24:18 -0800116 fragUniformIsSTO = new bool[numFragUnis];
Alex Sakhartchouk54929cc2010-11-08 15:10:52 -0800117 }
118 }
119 ~ProgramEntry() {
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800120 if (vtxAttrs) {
Alex Sakhartchouk54929cc2010-11-08 15:10:52 -0800121 delete[] vtxAttrs;
Chris Wailes44bef6f2014-08-12 13:51:10 -0700122 vtxAttrs = nullptr;
Alex Sakhartchouk54929cc2010-11-08 15:10:52 -0800123 }
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800124 if (vtxUniforms) {
Alex Sakhartchouk54929cc2010-11-08 15:10:52 -0800125 delete[] vtxUniforms;
Chris Wailes44bef6f2014-08-12 13:51:10 -0700126 vtxUniforms = nullptr;
Alex Sakhartchouk54929cc2010-11-08 15:10:52 -0800127 }
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800128 if (fragUniforms) {
Alex Sakhartchouk54929cc2010-11-08 15:10:52 -0800129 delete[] fragUniforms;
Chris Wailes44bef6f2014-08-12 13:51:10 -0700130 fragUniforms = nullptr;
Alex Sakhartchouk54929cc2010-11-08 15:10:52 -0800131 }
Jason Samscf27eb42012-02-10 13:24:18 -0800132 if (fragUniformIsSTO) {
133 delete[] fragUniformIsSTO;
Chris Wailes44bef6f2014-08-12 13:51:10 -0700134 fragUniformIsSTO = nullptr;
Jason Samscf27eb42012-02-10 13:24:18 -0800135 }
Alex Sakhartchouk54929cc2010-11-08 15:10:52 -0800136 }
Jason Samsc460e552009-11-25 13:22:07 -0800137 uint32_t vtx;
138 uint32_t frag;
139 uint32_t program;
Alex Sakhartchouk886f11a2010-09-29 09:49:13 -0700140 uint32_t vtxAttrCount;
Alex Sakhartchouk54929cc2010-11-08 15:10:52 -0800141 AttrData *vtxAttrs;
142 UniformData *vtxUniforms;
143 UniformData *fragUniforms;
Jason Samscf27eb42012-02-10 13:24:18 -0800144 bool *fragUniformIsSTO;
Alex Sakhartchouk54929cc2010-11-08 15:10:52 -0800145 };
Chris Wailes93d6bc82014-07-28 16:54:38 -0700146 std::vector<ProgramEntry*> mEntries;
Alex Sakhartchouk54929cc2010-11-08 15:10:52 -0800147 ProgramEntry *mCurrent;
Jason Samsc460e552009-11-25 13:22:07 -0800148
Alex Sakhartchouka04e30d2011-04-29 16:49:08 -0700149 bool hasArrayUniforms(RsdShader *vtx, RsdShader *frag);
150 void populateUniformData(RsdShader *prog, uint32_t linkedID, UniformData *data);
151 void updateUniformArrayData(const android::renderscript::Context *rsc,
152 RsdShader *prog, uint32_t linkedID,
Alex Sakhartchouk54929cc2010-11-08 15:10:52 -0800153 UniformData *data, const char* logTag,
154 UniformQueryData **uniformList, uint32_t uniListSize);
Jason Samsc460e552009-11-25 13:22:07 -0800155};
156
157
Alex Sakhartchouka04e30d2011-04-29 16:49:08 -0700158#endif //ANDROID_RSD_SHADER_CACHE_H