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