rsProgramFragment.cpp revision b81a0eb8180791e4eaab1253b59fa8bd562b046b
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
20using namespace android;
21using namespace android::renderscript;
22
23ProgramFragment::ProgramFragment(Context *rsc, const char * shaderText,
24                                 uint32_t shaderLength, const uint32_t * params,
25                                 uint32_t paramLength)
26    : Program(rsc, shaderText, shaderLength, params, paramLength) {
27    mConstantColor[0] = 1.f;
28    mConstantColor[1] = 1.f;
29    mConstantColor[2] = 1.f;
30    mConstantColor[3] = 1.f;
31
32    mRSC->mHal.funcs.fragment.init(mRSC, this, mUserShader.string(), mUserShader.length());
33}
34
35ProgramFragment::~ProgramFragment() {
36    mRSC->mHal.funcs.fragment.destroy(mRSC, this);
37}
38
39void ProgramFragment::setConstantColor(Context *rsc, float r, float g, float b, float a) {
40    if (isUserProgram()) {
41        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    }
45    if (mHal.state.constants[0].get() == NULL) {
46        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");
48        return;
49    }
50    mConstantColor[0] = r;
51    mConstantColor[1] = g;
52    mConstantColor[2] = b;
53    mConstantColor[3] = a;
54    memcpy(mHal.state.constants[0]->getPtr(), mConstantColor, 4*sizeof(float));
55    mDirty = true;
56}
57
58void ProgramFragment::setup(Context *rsc, ProgramFragmentState *state) {
59    if ((state->mLast.get() == this) && !mDirty) {
60        return;
61    }
62    state->mLast.set(this);
63
64    for (uint32_t ct=0; ct < mHal.state.texturesCount; ct++) {
65        if (!mHal.state.textures[ct].get()) {
66            LOGE("No texture bound for shader id %u, texture unit %u", (uint)this, ct);
67            rsc->setError(RS_ERROR_BAD_SHADER, "No texture bound");
68            continue;
69        }
70    }
71
72    rsc->mHal.funcs.fragment.setActive(rsc, this);
73}
74
75void ProgramFragment::serialize(OStream *stream) const {
76}
77
78ProgramFragment *ProgramFragment::createFromStream(Context *rsc, IStream *stream) {
79    return NULL;
80}
81
82ProgramFragmentState::ProgramFragmentState() {
83    mPF = NULL;
84}
85
86ProgramFragmentState::~ProgramFragmentState() {
87    ObjectBase::checkDelete(mPF);
88    mPF = NULL;
89}
90
91void ProgramFragmentState::init(Context *rsc) {
92    String8 shaderString(RS_SHADER_INTERNAL);
93    shaderString.append("varying lowp vec4 varColor;\n");
94    shaderString.append("varying vec2 varTex0;\n");
95    shaderString.append("void main() {\n");
96    shaderString.append("  lowp vec4 col = UNI_Color;\n");
97    shaderString.append("  gl_FragColor = col;\n");
98    shaderString.append("}\n");
99
100    const Element *colorElem = Element::create(rsc, RS_TYPE_FLOAT_32, RS_KIND_USER, false, 4);
101    rsc->mStateElement.elementBuilderBegin();
102    rsc->mStateElement.elementBuilderAdd(colorElem, "Color", 1);
103    const Element *constInput = rsc->mStateElement.elementBuilderCreate(rsc);
104
105    Type *inputType = Type::getType(rsc, constInput, 1, 0, 0, false, false);
106
107    uint32_t tmp[2];
108    tmp[0] = RS_PROGRAM_PARAM_CONSTANT;
109    tmp[1] = (uint32_t)inputType;
110
111    Allocation *constAlloc = Allocation::createAllocation(rsc, inputType,
112                              RS_ALLOCATION_USAGE_SCRIPT | RS_ALLOCATION_USAGE_GRAPHICS_CONSTANTS);
113    ProgramFragment *pf = new ProgramFragment(rsc, shaderString.string(),
114                                              shaderString.length(), tmp, 2);
115    pf->bindAllocation(rsc, constAlloc, 0);
116    pf->setConstantColor(rsc, 1.0f, 1.0f, 1.0f, 1.0f);
117
118    mDefault.set(pf);
119}
120
121void ProgramFragmentState::deinit(Context *rsc) {
122    mDefault.clear();
123    mLast.clear();
124}
125
126namespace android {
127namespace renderscript {
128
129RsProgramFragment rsi_ProgramFragmentCreate(Context *rsc, const char * shaderText,
130                             size_t shaderLength, const uint32_t * params,
131                             size_t paramLength) {
132    ProgramFragment *pf = new ProgramFragment(rsc, shaderText, shaderLength, params, paramLength);
133    pf->incUserRef();
134    //LOGE("rsi_ProgramFragmentCreate %p", pf);
135    return pf;
136}
137
138}
139}
140
141