blob: fcbfbc87edd3fb9d752a17d5e47a73b4dcaa4b4e [file] [log] [blame]
Jason Samsd19f10d2009-05-22 14:03:28 -07001/*
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"
Alex Sakhartchoukaa7d2882010-05-21 12:53:13 -070018#include "rsProgramFragment.h"
Jason Sams4b962e52009-06-22 17:15:15 -070019
Jason Samsd19f10d2009-05-22 14:03:28 -070020using namespace android;
21using namespace android::renderscript;
22
Jason Sams7e5ab3b2009-12-15 13:27:04 -080023ProgramFragment::ProgramFragment(Context *rsc, const char * shaderText,
24 uint32_t shaderLength, const uint32_t * params,
Alex Sakhartchouked9f2102010-11-09 17:00:54 -080025 uint32_t paramLength)
26 : Program(rsc, shaderText, shaderLength, params, paramLength) {
Jason Sams442a6472010-08-04 17:50:20 -070027 mConstantColor[0] = 1.f;
28 mConstantColor[1] = 1.f;
29 mConstantColor[2] = 1.f;
30 mConstantColor[3] = 1.f;
31
Alex Sakhartchouk4a36b452011-04-29 16:49:08 -070032 mRSC->mHal.funcs.fragment.init(mRSC, this, mUserShader.string(), mUserShader.length());
Jason Sams7e5ab3b2009-12-15 13:27:04 -080033}
34
Alex Sakhartchouked9f2102010-11-09 17:00:54 -080035ProgramFragment::~ProgramFragment() {
Alex Sakhartchouk4a36b452011-04-29 16:49:08 -070036 mRSC->mHal.funcs.fragment.destroy(mRSC, this);
Jason Samsd19f10d2009-05-22 14:03:28 -070037}
38
Alex Sakhartchouked9f2102010-11-09 17:00:54 -080039void ProgramFragment::setConstantColor(Context *rsc, float r, float g, float b, float a) {
40 if (isUserProgram()) {
Alex Sakhartchoukb89aaac2010-09-23 16:16:33 -070041 LOGE("Attempting to set fixed function emulation color on user program");
42 rsc->setError(RS_ERROR_BAD_SHADER, "Cannot set fixed function emulation color on user program");
43 return;
44 }
Alex Sakhartchouk4a36b452011-04-29 16:49:08 -070045 if (mHal.state.constants[0].get() == NULL) {
Alex Sakhartchoukb89aaac2010-09-23 16:16:33 -070046 LOGE("Unable to set fixed function emulation color because allocation is missing");
47 rsc->setError(RS_ERROR_BAD_SHADER, "Unable to set fixed function emulation color because allocation is missing");
Alex Sakhartchoukc984dd72010-09-14 09:50:43 -070048 return;
49 }
Jason Sams442a6472010-08-04 17:50:20 -070050 mConstantColor[0] = r;
51 mConstantColor[1] = g;
52 mConstantColor[2] = b;
53 mConstantColor[3] = a;
Alex Sakhartchouk4a36b452011-04-29 16:49:08 -070054 memcpy(mHal.state.constants[0]->getPtr(), mConstantColor, 4*sizeof(float));
Jason Sams442a6472010-08-04 17:50:20 -070055 mDirty = true;
56}
57
Alex Sakhartchouk4a36b452011-04-29 16:49:08 -070058void ProgramFragment::setupGL2(Context *rsc, ProgramFragmentState *state) {
Jason Samsbb51c402009-11-25 13:22:07 -080059 if ((state->mLast.get() == this) && !mDirty) {
Jason Sams7c1f4c32010-06-22 17:22:13 -070060 return;
Jason Samsbb51c402009-11-25 13:22:07 -080061 }
62 state->mLast.set(this);
63
Jason Sams5dbfe932010-01-27 14:41:43 -080064 rsc->checkError("ProgramFragment::setupGL2 start");
Jason Sams442a6472010-08-04 17:50:20 -070065
Alex Sakhartchouk4a36b452011-04-29 16:49:08 -070066 for (uint32_t ct=0; ct < mHal.state.texturesCount; ct++) {
67 if (!mHal.state.textures[ct].get()) {
Alex Sakhartchoukb89aaac2010-09-23 16:16:33 -070068 LOGE("No texture bound for shader id %u, texture unit %u", (uint)this, ct);
69 rsc->setError(RS_ERROR_BAD_SHADER, "No texture bound");
Jason Samsbb51c402009-11-25 13:22:07 -080070 continue;
71 }
Alex Sakhartchouk4a36b452011-04-29 16:49:08 -070072 mHal.state.textures[ct]->uploadCheck(rsc);
Jason Samsbb51c402009-11-25 13:22:07 -080073 }
74
Alex Sakhartchouk4a36b452011-04-29 16:49:08 -070075 rsc->mHal.funcs.fragment.setActive(rsc, this);
Jason Samsbb51c402009-11-25 13:22:07 -080076}
Jason Samsd19f10d2009-05-22 14:03:28 -070077
Alex Sakhartchouked9f2102010-11-09 17:00:54 -080078void ProgramFragment::serialize(OStream *stream) const {
Alex Sakhartchoukaa7d2882010-05-21 12:53:13 -070079}
80
Alex Sakhartchouked9f2102010-11-09 17:00:54 -080081ProgramFragment *ProgramFragment::createFromStream(Context *rsc, IStream *stream) {
Alex Sakhartchoukaa7d2882010-05-21 12:53:13 -070082 return NULL;
83}
84
Alex Sakhartchouked9f2102010-11-09 17:00:54 -080085ProgramFragmentState::ProgramFragmentState() {
Jason Samsd19f10d2009-05-22 14:03:28 -070086 mPF = NULL;
87}
88
Alex Sakhartchouked9f2102010-11-09 17:00:54 -080089ProgramFragmentState::~ProgramFragmentState() {
Jason Samsb38d5342010-10-21 14:06:55 -070090 ObjectBase::checkDelete(mPF);
91 mPF = NULL;
Jason Samsd19f10d2009-05-22 14:03:28 -070092}
93
Alex Sakhartchouked9f2102010-11-09 17:00:54 -080094void ProgramFragmentState::init(Context *rsc) {
Alex Sakhartchoukc984dd72010-09-14 09:50:43 -070095 String8 shaderString(RS_SHADER_INTERNAL);
96 shaderString.append("varying lowp vec4 varColor;\n");
Alex Sakhartchoukd2091632010-10-06 11:15:01 -070097 shaderString.append("varying vec2 varTex0;\n");
Alex Sakhartchoukc984dd72010-09-14 09:50:43 -070098 shaderString.append("void main() {\n");
99 shaderString.append(" lowp vec4 col = UNI_Color;\n");
100 shaderString.append(" gl_FragColor = col;\n");
101 shaderString.append("}\n");
102
103 const Element *colorElem = Element::create(rsc, RS_TYPE_FLOAT_32, RS_KIND_USER, false, 4);
104 rsc->mStateElement.elementBuilderBegin();
105 rsc->mStateElement.elementBuilderAdd(colorElem, "Color", 1);
106 const Element *constInput = rsc->mStateElement.elementBuilderCreate(rsc);
107
Jason Sams31a7e422010-10-26 13:09:17 -0700108 Type *inputType = Type::getType(rsc, constInput, 1, 0, 0, false, false);
Alex Sakhartchoukc984dd72010-09-14 09:50:43 -0700109
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800110 uint32_t tmp[2];
Alex Sakhartchoukc984dd72010-09-14 09:50:43 -0700111 tmp[0] = RS_PROGRAM_PARAM_CONSTANT;
112 tmp[1] = (uint32_t)inputType;
Alex Sakhartchoukc984dd72010-09-14 09:50:43 -0700113
Jason Sams5476b452010-12-08 16:14:36 -0800114 Allocation *constAlloc = new Allocation(rsc, inputType, RS_ALLOCATION_USAGE_SCRIPT | RS_ALLOCATION_USAGE_GRAPHICS_CONSTANTS);
Alex Sakhartchoukc984dd72010-09-14 09:50:43 -0700115 ProgramFragment *pf = new ProgramFragment(rsc, shaderString.string(),
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800116 shaderString.length(), tmp, 2);
Alex Sakhartchoukb89aaac2010-09-23 16:16:33 -0700117 pf->bindAllocation(rsc, constAlloc, 0);
118 pf->setConstantColor(rsc, 1.0f, 1.0f, 1.0f, 1.0f);
Alex Sakhartchoukc984dd72010-09-14 09:50:43 -0700119
Jason Sams9c54bdb2009-06-17 16:52:59 -0700120 mDefault.set(pf);
121}
Jason Samsd19f10d2009-05-22 14:03:28 -0700122
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800123void ProgramFragmentState::deinit(Context *rsc) {
Jason Sams61f08d62009-09-25 16:37:33 -0700124 mDefault.clear();
125 mLast.clear();
126}
127
Jason Samsd19f10d2009-05-22 14:03:28 -0700128namespace android {
129namespace renderscript {
130
Alex Sakhartchoukb89aaac2010-09-23 16:16:33 -0700131RsProgramFragment rsi_ProgramFragmentCreate(Context *rsc, const char * shaderText,
Jason Sams7e5ab3b2009-12-15 13:27:04 -0800132 uint32_t shaderLength, const uint32_t * params,
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800133 uint32_t paramLength) {
Jason Sams7e5ab3b2009-12-15 13:27:04 -0800134 ProgramFragment *pf = new ProgramFragment(rsc, shaderText, shaderLength, params, paramLength);
135 pf->incUserRef();
Alex Sakhartchoukb89aaac2010-09-23 16:16:33 -0700136 //LOGE("rsi_ProgramFragmentCreate %p", pf);
Jason Sams7e5ab3b2009-12-15 13:27:04 -0800137 return pf;
138}
Jason Samsd19f10d2009-05-22 14:03:28 -0700139
140}
141}
142