1 /*
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 #include "rsContext.h"
18 #include "rsProgramFragment.h"
19
20 #include <inttypes.h>
21
22 using namespace android;
23 using namespace android::renderscript;
24
ProgramFragment(Context * rsc,const char * shaderText,size_t shaderLength,const char ** textureNames,size_t textureNamesCount,const size_t * textureNamesLength,const uintptr_t * params,size_t paramLength)25 ProgramFragment::ProgramFragment(Context *rsc, const char * shaderText, size_t shaderLength,
26 const char** textureNames, size_t textureNamesCount, const size_t *textureNamesLength,
27 const uintptr_t * params, size_t paramLength)
28 : Program(rsc, shaderText, shaderLength, params, paramLength) {
29 mConstantColor[0] = 1.f;
30 mConstantColor[1] = 1.f;
31 mConstantColor[2] = 1.f;
32 mConstantColor[3] = 1.f;
33
34 mRSC->mHal.funcs.fragment.init(mRSC, this, mUserShader, mUserShaderLen,
35 textureNames, textureNamesCount, textureNamesLength);
36 }
37
~ProgramFragment()38 ProgramFragment::~ProgramFragment() {
39 mRSC->mHal.funcs.fragment.destroy(mRSC, this);
40 }
41
setConstantColor(Context * rsc,float r,float g,float b,float a)42 void ProgramFragment::setConstantColor(Context *rsc, float r, float g, float b, float a) {
43 if (isUserProgram()) {
44 ALOGE("Attempting to set fixed function emulation color on user program");
45 rsc->setError(RS_ERROR_BAD_SHADER, "Cannot set fixed function emulation color on user program");
46 return;
47 }
48 if (mHal.state.constants[0] == NULL) {
49 ALOGE("Unable to set fixed function emulation color because allocation is missing");
50 rsc->setError(RS_ERROR_BAD_SHADER, "Unable to set fixed function emulation color because allocation is missing");
51 return;
52 }
53 mConstantColor[0] = r;
54 mConstantColor[1] = g;
55 mConstantColor[2] = b;
56 mConstantColor[3] = a;
57 void *p = rsc->mHal.funcs.allocation.lock1D(rsc, mHal.state.constants[0]);
58 memcpy(p, mConstantColor, 4*sizeof(float));
59 mDirty = true;
60 rsc->mHal.funcs.allocation.unlock1D(rsc, mHal.state.constants[0]);
61 }
62
setup(Context * rsc,ProgramFragmentState * state)63 void ProgramFragment::setup(Context *rsc, ProgramFragmentState *state) {
64 if ((state->mLast.get() == this) && !mDirty) {
65 return;
66 }
67 state->mLast.set(this);
68
69 for (uint32_t ct=0; ct < mHal.state.texturesCount; ct++) {
70 if (!mHal.state.textures[ct]) {
71 ALOGE("No texture bound for shader id %" PRIuPTR ", texture unit %u", (uintptr_t)this, ct);
72 rsc->setError(RS_ERROR_BAD_SHADER, "No texture bound");
73 continue;
74 }
75 }
76
77 rsc->mHal.funcs.fragment.setActive(rsc, this);
78 }
79
serialize(Context * rsc,OStream * stream) const80 void ProgramFragment::serialize(Context *rsc, OStream *stream) const {
81 }
82
createFromStream(Context * rsc,IStream * stream)83 ProgramFragment *ProgramFragment::createFromStream(Context *rsc, IStream *stream) {
84 return NULL;
85 }
86
ProgramFragmentState()87 ProgramFragmentState::ProgramFragmentState() {
88 mPF = NULL;
89 }
90
~ProgramFragmentState()91 ProgramFragmentState::~ProgramFragmentState() {
92 ObjectBase::checkDelete(mPF);
93 mPF = NULL;
94 }
95
init(Context * rsc)96 void ProgramFragmentState::init(Context *rsc) {
97 const char *shaderString =
98 RS_SHADER_INTERNAL
99 "varying lowp vec4 varColor;\n"
100 "varying vec2 varTex0;\n"
101 "void main() {\n"
102 " lowp vec4 col = UNI_Color;\n"
103 " gl_FragColor = col;\n"
104 "}\n";
105
106 ObjectBaseRef<const Element> colorElem = Element::createRef(rsc, RS_TYPE_FLOAT_32, RS_KIND_USER, false, 4);
107
108 const char *enames[] = { "Color" };
109 const Element *eins[] = {colorElem.get()};
110 ObjectBaseRef<const Element> constInput = Element::create(rsc, 1, eins, enames);
111
112 ObjectBaseRef<Type> inputType = Type::getTypeRef(rsc, constInput.get(), 1, 0, 0, false, false, 0);
113
114 uintptr_t tmp[2];
115 tmp[0] = RS_PROGRAM_PARAM_CONSTANT;
116 tmp[1] = (uintptr_t)inputType.get();
117
118 Allocation *constAlloc = Allocation::createAllocation(rsc, inputType.get(),
119 RS_ALLOCATION_USAGE_SCRIPT | RS_ALLOCATION_USAGE_GRAPHICS_CONSTANTS);
120 ProgramFragment *pf = new ProgramFragment(rsc, shaderString, strlen(shaderString),
121 NULL, 0, NULL, tmp, 2);
122 pf->bindAllocation(rsc, constAlloc, 0);
123 pf->setConstantColor(rsc, 1.0f, 1.0f, 1.0f, 1.0f);
124
125 mDefault.set(pf);
126 }
127
deinit(Context * rsc)128 void ProgramFragmentState::deinit(Context *rsc) {
129 mDefault.clear();
130 mLast.clear();
131 }
132
133 namespace android {
134 namespace renderscript {
135
rsi_ProgramFragmentCreate(Context * rsc,const char * shaderText,size_t shaderLength,const char ** textureNames,size_t textureNamesCount,const size_t * textureNamesLength,const uintptr_t * params,size_t paramLength)136 RsProgramFragment rsi_ProgramFragmentCreate(Context *rsc, const char * shaderText,
137 size_t shaderLength,
138 const char** textureNames,
139 size_t textureNamesCount,
140 const size_t *textureNamesLength,
141 const uintptr_t * params, size_t paramLength) {
142 ProgramFragment *pf = new ProgramFragment(rsc, shaderText, shaderLength,
143 textureNames, textureNamesCount, textureNamesLength,
144 params, paramLength);
145 pf->incUserRef();
146 //ALOGE("rsi_ProgramFragmentCreate %p", pf);
147 return pf;
148 }
149
150 }
151 }
152
153