blob: 360d3ab37d8cc034a5045de7bd2ca0014bf34a4c [file] [log] [blame]
Jason Sams326e0dd2009-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 Sakhartchoukfb6b6142010-05-21 12:53:13 -070018#include "rsProgramFragment.h"
Jason Sams1aa5a4e2009-06-22 17:15:15 -070019
Ian Rogersf8852d02014-01-29 15:35:17 -080020#include <inttypes.h>
21
Jason Sams326e0dd2009-05-22 14:03:28 -070022using namespace android;
23using namespace android::renderscript;
24
Alex Sakhartchouk748eb072012-02-15 16:21:46 -080025ProgramFragment::ProgramFragment(Context *rsc, const char * shaderText, size_t shaderLength,
26 const char** textureNames, size_t textureNamesCount, const size_t *textureNamesLength,
Ian Rogersf8852d02014-01-29 15:35:17 -080027 const uintptr_t * params, size_t paramLength)
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080028 : Program(rsc, shaderText, shaderLength, params, paramLength) {
Jason Sams6445e522010-08-04 17:50:20 -070029 mConstantColor[0] = 1.f;
30 mConstantColor[1] = 1.f;
31 mConstantColor[2] = 1.f;
32 mConstantColor[3] = 1.f;
33
Jason Samsf313dc32013-07-09 14:29:39 -070034 mRSC->mHal.funcs.fragment.init(mRSC, this, mUserShader, mUserShaderLen,
Alex Sakhartchouk748eb072012-02-15 16:21:46 -080035 textureNames, textureNamesCount, textureNamesLength);
Jason Samsf2e4fa22009-12-15 13:27:04 -080036}
37
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080038ProgramFragment::~ProgramFragment() {
Alex Sakhartchouka04e30d2011-04-29 16:49:08 -070039 mRSC->mHal.funcs.fragment.destroy(mRSC, this);
Jason Sams326e0dd2009-05-22 14:03:28 -070040}
41
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080042void ProgramFragment::setConstantColor(Context *rsc, float r, float g, float b, float a) {
43 if (isUserProgram()) {
Steve Blockaf12ac62012-01-06 19:20:56 +000044 ALOGE("Attempting to set fixed function emulation color on user program");
Alex Sakhartchouk383e5b12010-09-23 16:16:33 -070045 rsc->setError(RS_ERROR_BAD_SHADER, "Cannot set fixed function emulation color on user program");
46 return;
47 }
Alex Sakhartchouk064aa7e2011-10-18 10:54:29 -070048 if (mHal.state.constants[0] == NULL) {
Steve Blockaf12ac62012-01-06 19:20:56 +000049 ALOGE("Unable to set fixed function emulation color because allocation is missing");
Alex Sakhartchouk383e5b12010-09-23 16:16:33 -070050 rsc->setError(RS_ERROR_BAD_SHADER, "Unable to set fixed function emulation color because allocation is missing");
Alex Sakhartchouke7ae69f2010-09-14 09:50:43 -070051 return;
52 }
Jason Sams6445e522010-08-04 17:50:20 -070053 mConstantColor[0] = r;
54 mConstantColor[1] = g;
55 mConstantColor[2] = b;
56 mConstantColor[3] = a;
Jason Sams807fdc42012-07-25 17:55:39 -070057 void *p = rsc->mHal.funcs.allocation.lock1D(rsc, mHal.state.constants[0]);
58 memcpy(p, mConstantColor, 4*sizeof(float));
Jason Sams6445e522010-08-04 17:50:20 -070059 mDirty = true;
Jason Sams807fdc42012-07-25 17:55:39 -070060 rsc->mHal.funcs.allocation.unlock1D(rsc, mHal.state.constants[0]);
Jason Sams6445e522010-08-04 17:50:20 -070061}
62
Alex Sakhartchoukc19ff012011-05-06 14:59:45 -070063void ProgramFragment::setup(Context *rsc, ProgramFragmentState *state) {
Jason Samsc460e552009-11-25 13:22:07 -080064 if ((state->mLast.get() == this) && !mDirty) {
Jason Samse64c2f12010-06-22 17:22:13 -070065 return;
Jason Samsc460e552009-11-25 13:22:07 -080066 }
67 state->mLast.set(this);
68
Alex Sakhartchouka04e30d2011-04-29 16:49:08 -070069 for (uint32_t ct=0; ct < mHal.state.texturesCount; ct++) {
Alex Sakhartchouk064aa7e2011-10-18 10:54:29 -070070 if (!mHal.state.textures[ct]) {
Ian Rogersf8852d02014-01-29 15:35:17 -080071 ALOGE("No texture bound for shader id %" PRIuPTR ", texture unit %u", (uintptr_t)this, ct);
Alex Sakhartchouk383e5b12010-09-23 16:16:33 -070072 rsc->setError(RS_ERROR_BAD_SHADER, "No texture bound");
Jason Samsc460e552009-11-25 13:22:07 -080073 continue;
74 }
Jason Samsc460e552009-11-25 13:22:07 -080075 }
76
Alex Sakhartchouka04e30d2011-04-29 16:49:08 -070077 rsc->mHal.funcs.fragment.setActive(rsc, this);
Jason Samsc460e552009-11-25 13:22:07 -080078}
Jason Sams326e0dd2009-05-22 14:03:28 -070079
Jason Samse3150cf2012-07-24 18:10:20 -070080void ProgramFragment::serialize(Context *rsc, OStream *stream) const {
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -070081}
82
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080083ProgramFragment *ProgramFragment::createFromStream(Context *rsc, IStream *stream) {
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -070084 return NULL;
85}
86
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080087ProgramFragmentState::ProgramFragmentState() {
Jason Sams326e0dd2009-05-22 14:03:28 -070088 mPF = NULL;
89}
90
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080091ProgramFragmentState::~ProgramFragmentState() {
Jason Sams225afd32010-10-21 14:06:55 -070092 ObjectBase::checkDelete(mPF);
93 mPF = NULL;
Jason Sams326e0dd2009-05-22 14:03:28 -070094}
95
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080096void ProgramFragmentState::init(Context *rsc) {
Jason Samsa7f5e042013-07-08 16:46:18 -070097 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";
Alex Sakhartchouke7ae69f2010-09-14 09:50:43 -0700105
Alex Sakhartchoukc700e642011-08-16 13:09:46 -0700106 ObjectBaseRef<const Element> colorElem = Element::createRef(rsc, RS_TYPE_FLOAT_32, RS_KIND_USER, false, 4);
Jason Samsf313dc32013-07-09 14:29:39 -0700107
108 const char *enames[] = { "Color" };
109 const Element *eins[] = {colorElem.get()};
110 ObjectBaseRef<const Element> constInput = Element::create(rsc, 1, eins, enames);
Alex Sakhartchouke7ae69f2010-09-14 09:50:43 -0700111
Jason Samsa572aca2013-01-09 11:52:26 -0800112 ObjectBaseRef<Type> inputType = Type::getTypeRef(rsc, constInput.get(), 1, 0, 0, false, false, 0);
Alex Sakhartchouke7ae69f2010-09-14 09:50:43 -0700113
Ian Rogersf8852d02014-01-29 15:35:17 -0800114 uintptr_t tmp[2];
Alex Sakhartchouke7ae69f2010-09-14 09:50:43 -0700115 tmp[0] = RS_PROGRAM_PARAM_CONSTANT;
Ian Rogersf8852d02014-01-29 15:35:17 -0800116 tmp[1] = (uintptr_t)inputType.get();
Alex Sakhartchouke7ae69f2010-09-14 09:50:43 -0700117
Alex Sakhartchoukc700e642011-08-16 13:09:46 -0700118 Allocation *constAlloc = Allocation::createAllocation(rsc, inputType.get(),
Jason Samseb4fe182011-05-26 16:33:01 -0700119 RS_ALLOCATION_USAGE_SCRIPT | RS_ALLOCATION_USAGE_GRAPHICS_CONSTANTS);
Jason Samsa7f5e042013-07-08 16:46:18 -0700120 ProgramFragment *pf = new ProgramFragment(rsc, shaderString, strlen(shaderString),
Alex Sakhartchouk748eb072012-02-15 16:21:46 -0800121 NULL, 0, NULL, tmp, 2);
Alex Sakhartchouk383e5b12010-09-23 16:16:33 -0700122 pf->bindAllocation(rsc, constAlloc, 0);
123 pf->setConstantColor(rsc, 1.0f, 1.0f, 1.0f, 1.0f);
Alex Sakhartchouke7ae69f2010-09-14 09:50:43 -0700124
Jason Sams8ce125b2009-06-17 16:52:59 -0700125 mDefault.set(pf);
126}
Jason Sams326e0dd2009-05-22 14:03:28 -0700127
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800128void ProgramFragmentState::deinit(Context *rsc) {
Jason Samsf2649a92009-09-25 16:37:33 -0700129 mDefault.clear();
130 mLast.clear();
131}
132
Jason Sams326e0dd2009-05-22 14:03:28 -0700133namespace android {
134namespace renderscript {
135
Alex Sakhartchouk383e5b12010-09-23 16:16:33 -0700136RsProgramFragment rsi_ProgramFragmentCreate(Context *rsc, const char * shaderText,
Alex Sakhartchouk748eb072012-02-15 16:21:46 -0800137 size_t shaderLength,
138 const char** textureNames,
139 size_t textureNamesCount,
140 const size_t *textureNamesLength,
Ian Rogersf8852d02014-01-29 15:35:17 -0800141 const uintptr_t * params, size_t paramLength) {
Alex Sakhartchouk748eb072012-02-15 16:21:46 -0800142 ProgramFragment *pf = new ProgramFragment(rsc, shaderText, shaderLength,
143 textureNames, textureNamesCount, textureNamesLength,
144 params, paramLength);
Jason Samsf2e4fa22009-12-15 13:27:04 -0800145 pf->incUserRef();
Steve Blockaf12ac62012-01-06 19:20:56 +0000146 //ALOGE("rsi_ProgramFragmentCreate %p", pf);
Jason Samsf2e4fa22009-12-15 13:27:04 -0800147 return pf;
148}
Jason Sams326e0dd2009-05-22 14:03:28 -0700149
150}
151}
152