blob: ec666627367e0e45ada2e1c26957c1a8026e86d1 [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"
18#include "rsProgramVertex.h"
19
Jason Sams1aa5a4e2009-06-22 17:15:15 -070020#include <GLES/gl.h>
21#include <GLES/glext.h>
22
Jason Sams326e0dd2009-05-22 14:03:28 -070023using namespace android;
24using namespace android::renderscript;
25
26
27ProgramVertex::ProgramVertex(Element *in, Element *out) :
28 Program(in, out)
29{
30 mTextureMatrixEnable = false;
Jason Sams326e0dd2009-05-22 14:03:28 -070031}
32
33ProgramVertex::~ProgramVertex()
34{
35}
36
Jason Sams56bc1af2009-06-16 17:49:58 -070037static void logMatrix(const char *txt, const float *f)
38{
39 LOGE("Matrix %s, %p", txt, f);
40 LOGE("%6.2f, %6.2f, %6.2f, %6.2f", f[0], f[4], f[8], f[12]);
41 LOGE("%6.2f, %6.2f, %6.2f, %6.2f", f[1], f[5], f[9], f[13]);
42 LOGE("%6.2f, %6.2f, %6.2f, %6.2f", f[2], f[6], f[10], f[14]);
43 LOGE("%6.2f, %6.2f, %6.2f, %6.2f", f[3], f[7], f[11], f[15]);
44}
45
Jason Sams326e0dd2009-05-22 14:03:28 -070046void 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
Jason Sams56bc1af2009-06-16 17:49:58 -070057 //logMatrix("prog", &f[RS_PROGRAM_VERTEX_PROJECTION_OFFSET]);
58 //logMatrix("model", &f[RS_PROGRAM_VERTEX_MODELVIEW_OFFSET]);
Jason Sams326e0dd2009-05-22 14:03:28 -070059
60 glMatrixMode(GL_PROJECTION);
Jason Sams56bc1af2009-06-16 17:49:58 -070061 glLoadMatrixf(&f[RS_PROGRAM_VERTEX_PROJECTION_OFFSET]);
Jason Sams326e0dd2009-05-22 14:03:28 -070062 glMatrixMode(GL_MODELVIEW);
Jason Sams56bc1af2009-06-16 17:49:58 -070063 glLoadMatrixf(&f[RS_PROGRAM_VERTEX_MODELVIEW_OFFSET]);
Jason Sams326e0dd2009-05-22 14:03:28 -070064}
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
Jason Sams8ce125b2009-06-17 16:52:59 -070087void 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}
Jason Sams326e0dd2009-05-22 14:03:28 -0700104
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;
Jason Sams326e0dd2009-05-22 14:03:28 -0700120 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
Jason Sams326e0dd2009-05-22 14:03:28 -0700134void rsi_ProgramVertexSetTextureMatrixEnable(Context *rsc, bool enable)
135{
136 rsc->mStateVertex.mPV->setTextureMatrixEnable(enable);
137}
138
Jason Sams326e0dd2009-05-22 14:03:28 -0700139
140
141}
142}