2009-05-22 14:03:28 -07:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2009 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "rsContext.h"
|
2010-05-21 12:53:13 -07:00
|
|
|
#include "rsProgramFragment.h"
|
2009-06-22 17:15:15 -07:00
|
|
|
|
2009-05-22 14:03:28 -07:00
|
|
|
using namespace android;
|
|
|
|
using namespace android::renderscript;
|
|
|
|
|
2009-12-15 13:27:04 -08:00
|
|
|
ProgramFragment::ProgramFragment(Context *rsc, const char * shaderText,
|
|
|
|
uint32_t shaderLength, const uint32_t * params,
|
2010-11-09 17:00:54 -08:00
|
|
|
uint32_t paramLength)
|
|
|
|
: Program(rsc, shaderText, shaderLength, params, paramLength) {
|
2010-08-04 17:50:20 -07:00
|
|
|
mConstantColor[0] = 1.f;
|
|
|
|
mConstantColor[1] = 1.f;
|
|
|
|
mConstantColor[2] = 1.f;
|
|
|
|
mConstantColor[3] = 1.f;
|
|
|
|
|
2011-04-29 16:49:08 -07:00
|
|
|
mRSC->mHal.funcs.fragment.init(mRSC, this, mUserShader.string(), mUserShader.length());
|
2009-12-15 13:27:04 -08:00
|
|
|
}
|
|
|
|
|
2010-11-09 17:00:54 -08:00
|
|
|
ProgramFragment::~ProgramFragment() {
|
2011-04-29 16:49:08 -07:00
|
|
|
mRSC->mHal.funcs.fragment.destroy(mRSC, this);
|
2009-05-22 14:03:28 -07:00
|
|
|
}
|
|
|
|
|
2010-11-09 17:00:54 -08:00
|
|
|
void ProgramFragment::setConstantColor(Context *rsc, float r, float g, float b, float a) {
|
|
|
|
if (isUserProgram()) {
|
2010-09-23 16:16:33 -07:00
|
|
|
LOGE("Attempting to set fixed function emulation color on user program");
|
|
|
|
rsc->setError(RS_ERROR_BAD_SHADER, "Cannot set fixed function emulation color on user program");
|
|
|
|
return;
|
|
|
|
}
|
2011-04-29 16:49:08 -07:00
|
|
|
if (mHal.state.constants[0].get() == NULL) {
|
2010-09-23 16:16:33 -07:00
|
|
|
LOGE("Unable to set fixed function emulation color because allocation is missing");
|
|
|
|
rsc->setError(RS_ERROR_BAD_SHADER, "Unable to set fixed function emulation color because allocation is missing");
|
2010-09-14 09:50:43 -07:00
|
|
|
return;
|
|
|
|
}
|
2010-08-04 17:50:20 -07:00
|
|
|
mConstantColor[0] = r;
|
|
|
|
mConstantColor[1] = g;
|
|
|
|
mConstantColor[2] = b;
|
|
|
|
mConstantColor[3] = a;
|
2011-04-29 16:49:08 -07:00
|
|
|
memcpy(mHal.state.constants[0]->getPtr(), mConstantColor, 4*sizeof(float));
|
2010-08-04 17:50:20 -07:00
|
|
|
mDirty = true;
|
|
|
|
}
|
2009-07-02 15:09:27 -07:00
|
|
|
|
2011-05-06 14:59:45 -07:00
|
|
|
void ProgramFragment::setup(Context *rsc, ProgramFragmentState *state) {
|
2009-11-25 13:22:07 -08:00
|
|
|
if ((state->mLast.get() == this) && !mDirty) {
|
2010-06-22 17:22:13 -07:00
|
|
|
return;
|
2009-11-25 13:22:07 -08:00
|
|
|
}
|
|
|
|
state->mLast.set(this);
|
|
|
|
|
2011-04-29 16:49:08 -07:00
|
|
|
for (uint32_t ct=0; ct < mHal.state.texturesCount; ct++) {
|
|
|
|
if (!mHal.state.textures[ct].get()) {
|
2010-09-23 16:16:33 -07:00
|
|
|
LOGE("No texture bound for shader id %u, texture unit %u", (uint)this, ct);
|
|
|
|
rsc->setError(RS_ERROR_BAD_SHADER, "No texture bound");
|
2009-11-25 13:22:07 -08:00
|
|
|
continue;
|
|
|
|
}
|
2010-09-23 16:16:33 -07:00
|
|
|
}
|
2010-08-31 12:02:01 -07:00
|
|
|
|
2011-04-29 16:49:08 -07:00
|
|
|
rsc->mHal.funcs.fragment.setActive(rsc, this);
|
2009-11-25 13:22:07 -08:00
|
|
|
}
|
2009-05-22 14:03:28 -07:00
|
|
|
|
2010-11-09 17:00:54 -08:00
|
|
|
void ProgramFragment::serialize(OStream *stream) const {
|
2010-05-21 12:53:13 -07:00
|
|
|
}
|
|
|
|
|
2010-11-09 17:00:54 -08:00
|
|
|
ProgramFragment *ProgramFragment::createFromStream(Context *rsc, IStream *stream) {
|
2010-05-21 12:53:13 -07:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2010-11-09 17:00:54 -08:00
|
|
|
ProgramFragmentState::ProgramFragmentState() {
|
2009-05-22 14:03:28 -07:00
|
|
|
mPF = NULL;
|
|
|
|
}
|
|
|
|
|
2010-11-09 17:00:54 -08:00
|
|
|
ProgramFragmentState::~ProgramFragmentState() {
|
2010-10-21 14:06:55 -07:00
|
|
|
ObjectBase::checkDelete(mPF);
|
|
|
|
mPF = NULL;
|
2009-05-22 14:03:28 -07:00
|
|
|
}
|
|
|
|
|
2010-11-09 17:00:54 -08:00
|
|
|
void ProgramFragmentState::init(Context *rsc) {
|
2010-09-14 09:50:43 -07:00
|
|
|
String8 shaderString(RS_SHADER_INTERNAL);
|
|
|
|
shaderString.append("varying lowp vec4 varColor;\n");
|
2010-10-06 11:15:01 -07:00
|
|
|
shaderString.append("varying vec2 varTex0;\n");
|
2010-09-14 09:50:43 -07:00
|
|
|
shaderString.append("void main() {\n");
|
|
|
|
shaderString.append(" lowp vec4 col = UNI_Color;\n");
|
|
|
|
shaderString.append(" gl_FragColor = col;\n");
|
|
|
|
shaderString.append("}\n");
|
|
|
|
|
2011-08-16 13:09:46 -07:00
|
|
|
ObjectBaseRef<const Element> colorElem = Element::createRef(rsc, RS_TYPE_FLOAT_32, RS_KIND_USER, false, 4);
|
|
|
|
Element::Builder builder;
|
|
|
|
builder.add(colorElem.get(), "Color", 1);
|
|
|
|
ObjectBaseRef<const Element> constInput = builder.create(rsc);
|
2010-09-14 09:50:43 -07:00
|
|
|
|
2011-08-16 13:09:46 -07:00
|
|
|
ObjectBaseRef<Type> inputType = Type::getTypeRef(rsc, constInput.get(), 1, 0, 0, false, false);
|
2010-09-14 09:50:43 -07:00
|
|
|
|
2010-11-18 15:22:43 -08:00
|
|
|
uint32_t tmp[2];
|
2010-09-14 09:50:43 -07:00
|
|
|
tmp[0] = RS_PROGRAM_PARAM_CONSTANT;
|
2011-08-16 13:09:46 -07:00
|
|
|
tmp[1] = (uint32_t)inputType.get();
|
2010-09-14 09:50:43 -07:00
|
|
|
|
2011-08-16 13:09:46 -07:00
|
|
|
Allocation *constAlloc = Allocation::createAllocation(rsc, inputType.get(),
|
2011-05-26 16:33:01 -07:00
|
|
|
RS_ALLOCATION_USAGE_SCRIPT | RS_ALLOCATION_USAGE_GRAPHICS_CONSTANTS);
|
2010-09-14 09:50:43 -07:00
|
|
|
ProgramFragment *pf = new ProgramFragment(rsc, shaderString.string(),
|
2010-11-18 15:22:43 -08:00
|
|
|
shaderString.length(), tmp, 2);
|
2010-09-23 16:16:33 -07:00
|
|
|
pf->bindAllocation(rsc, constAlloc, 0);
|
|
|
|
pf->setConstantColor(rsc, 1.0f, 1.0f, 1.0f, 1.0f);
|
2010-09-14 09:50:43 -07:00
|
|
|
|
2009-06-17 16:52:59 -07:00
|
|
|
mDefault.set(pf);
|
|
|
|
}
|
2009-05-22 14:03:28 -07:00
|
|
|
|
2010-11-09 17:00:54 -08:00
|
|
|
void ProgramFragmentState::deinit(Context *rsc) {
|
2009-09-25 16:37:33 -07:00
|
|
|
mDefault.clear();
|
|
|
|
mLast.clear();
|
|
|
|
}
|
|
|
|
|
2009-05-22 14:03:28 -07:00
|
|
|
namespace android {
|
|
|
|
namespace renderscript {
|
|
|
|
|
2010-09-23 16:16:33 -07:00
|
|
|
RsProgramFragment rsi_ProgramFragmentCreate(Context *rsc, const char * shaderText,
|
2011-06-03 10:18:01 -07:00
|
|
|
size_t shaderLength, const uint32_t * params,
|
|
|
|
size_t paramLength) {
|
2009-12-15 13:27:04 -08:00
|
|
|
ProgramFragment *pf = new ProgramFragment(rsc, shaderText, shaderLength, params, paramLength);
|
|
|
|
pf->incUserRef();
|
2010-09-23 16:16:33 -07:00
|
|
|
//LOGE("rsi_ProgramFragmentCreate %p", pf);
|
2009-12-15 13:27:04 -08:00
|
|
|
return pf;
|
|
|
|
}
|
2009-05-22 14:03:28 -07:00
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|