rsProgramVertex.cpp revision 992a0b70d8fd7a14f0c57bc3c7e16c1f269a6609
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 "rsProgramVertex.h"
19
20#include <GLES/gl.h>
21#include <GLES/glext.h>
22
23using namespace android;
24using namespace android::renderscript;
25
26
27ProgramVertex::ProgramVertex(Element *in, Element *out) :
28    Program(in, out)
29{
30    mTextureMatrixEnable = false;
31}
32
33ProgramVertex::~ProgramVertex()
34{
35}
36
37static void logMatrix(const char *txt, const float *f)
38{
39    LOGV("Matrix %s, %p", txt, f);
40    LOGV("%6.2f, %6.2f, %6.2f, %6.2f", f[0], f[4], f[8], f[12]);
41    LOGV("%6.2f, %6.2f, %6.2f, %6.2f", f[1], f[5], f[9], f[13]);
42    LOGV("%6.2f, %6.2f, %6.2f, %6.2f", f[2], f[6], f[10], f[14]);
43    LOGV("%6.2f, %6.2f, %6.2f, %6.2f", f[3], f[7], f[11], f[15]);
44}
45
46void ProgramVertex::setupGL()
47{
48    const float *f = static_cast<const float *>(mConstants[0]->getPtr());
49
50    glMatrixMode(GL_TEXTURE);
51    if (mTextureMatrixEnable) {
52        glLoadMatrixf(&f[RS_PROGRAM_VERTEX_TEXTURE_OFFSET]);
53    } else {
54        glLoadIdentity();
55    }
56
57    //logMatrix("prog", &f[RS_PROGRAM_VERTEX_PROJECTION_OFFSET]);
58    //logMatrix("model", &f[RS_PROGRAM_VERTEX_MODELVIEW_OFFSET]);
59
60    glMatrixMode(GL_PROJECTION);
61    glLoadMatrixf(&f[RS_PROGRAM_VERTEX_PROJECTION_OFFSET]);
62    glMatrixMode(GL_MODELVIEW);
63    glLoadMatrixf(&f[RS_PROGRAM_VERTEX_MODELVIEW_OFFSET]);
64}
65
66void ProgramVertex::setConstantType(uint32_t slot, const Type *t)
67{
68    mConstantTypes[slot].set(t);
69}
70
71void ProgramVertex::bindAllocation(uint32_t slot, Allocation *a)
72{
73    mConstants[slot].set(a);
74}
75
76
77ProgramVertexState::ProgramVertexState()
78{
79    mPV = NULL;
80}
81
82ProgramVertexState::~ProgramVertexState()
83{
84    delete mPV;
85}
86
87void ProgramVertexState::init(Context *rsc, int32_t w, int32_t h)
88{
89    ProgramVertex *pv = new ProgramVertex(NULL, NULL);
90    Allocation *alloc = (Allocation *)
91        rsi_AllocationCreatePredefSized(rsc, RS_ELEMENT_USER_FLOAT, 48);
92    mDefaultAlloc.set(alloc);
93    mDefault.set(pv);
94
95    pv->bindAllocation(0, alloc);
96
97    Matrix m;
98    m.loadOrtho(0,w, h,0, -1,1);
99    alloc->subData(RS_PROGRAM_VERTEX_PROJECTION_OFFSET, 16, &m.m[0]);
100
101    m.loadIdentity();
102    alloc->subData(RS_PROGRAM_VERTEX_MODELVIEW_OFFSET, 16, &m.m[0]);
103}
104
105
106namespace android {
107namespace renderscript {
108
109void rsi_ProgramVertexBegin(Context *rsc, RsElement in, RsElement out)
110{
111    delete rsc->mStateVertex.mPV;
112    rsc->mStateVertex.mPV = new ProgramVertex((Element *)in, (Element *)out);
113}
114
115RsProgramVertex rsi_ProgramVertexCreate(Context *rsc)
116{
117    ProgramVertex *pv = rsc->mStateVertex.mPV;
118    pv->incRef();
119    rsc->mStateVertex.mPV = 0;
120    return pv;
121}
122
123void rsi_ProgramVertexBindAllocation(Context *rsc, RsProgramVertex vpgm, uint32_t slot, RsAllocation constants)
124{
125    ProgramVertex *pv = static_cast<ProgramVertex *>(vpgm);
126    pv->bindAllocation(slot, static_cast<Allocation *>(constants));
127}
128
129void rsi_ProgramVertexSetType(Context *rsc, uint32_t slot, RsType constants)
130{
131    rsc->mStateVertex.mPV->setConstantType(slot, static_cast<const Type *>(constants));
132}
133
134void rsi_ProgramVertexSetTextureMatrixEnable(Context *rsc, bool enable)
135{
136    rsc->mStateVertex.mPV->setTextureMatrixEnable(enable);
137}
138
139
140
141}
142}
143