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 | |
Jason Sams | 4b962e5 | 2009-06-22 17:15:15 -0700 | [diff] [blame] | 20 | #include <GLES/gl.h> |
| 21 | #include <GLES/glext.h> |
| 22 | |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 23 | using namespace android; |
| 24 | using namespace android::renderscript; |
| 25 | |
| 26 | |
| 27 | ProgramVertex::ProgramVertex(Element *in, Element *out) : |
| 28 | Program(in, out) |
| 29 | { |
| 30 | mTextureMatrixEnable = false; |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 31 | } |
| 32 | |
| 33 | ProgramVertex::~ProgramVertex() |
| 34 | { |
| 35 | } |
| 36 | |
Jason Sams | b37c0a5 | 2009-06-16 17:49:58 -0700 | [diff] [blame] | 37 | static void logMatrix(const char *txt, const float *f) |
| 38 | { |
Jason Sams | f29ca50 | 2009-06-23 12:22:47 -0700 | [diff] [blame] | 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]); |
Jason Sams | b37c0a5 | 2009-06-16 17:49:58 -0700 | [diff] [blame] | 44 | } |
| 45 | |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 46 | void 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 Sams | b37c0a5 | 2009-06-16 17:49:58 -0700 | [diff] [blame] | 57 | //logMatrix("prog", &f[RS_PROGRAM_VERTEX_PROJECTION_OFFSET]); |
| 58 | //logMatrix("model", &f[RS_PROGRAM_VERTEX_MODELVIEW_OFFSET]); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 59 | |
| 60 | glMatrixMode(GL_PROJECTION); |
Jason Sams | b37c0a5 | 2009-06-16 17:49:58 -0700 | [diff] [blame] | 61 | glLoadMatrixf(&f[RS_PROGRAM_VERTEX_PROJECTION_OFFSET]); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 62 | glMatrixMode(GL_MODELVIEW); |
Jason Sams | b37c0a5 | 2009-06-16 17:49:58 -0700 | [diff] [blame] | 63 | glLoadMatrixf(&f[RS_PROGRAM_VERTEX_MODELVIEW_OFFSET]); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | void ProgramVertex::setConstantType(uint32_t slot, const Type *t) |
| 67 | { |
| 68 | mConstantTypes[slot].set(t); |
| 69 | } |
| 70 | |
| 71 | void ProgramVertex::bindAllocation(uint32_t slot, Allocation *a) |
| 72 | { |
| 73 | mConstants[slot].set(a); |
| 74 | } |
| 75 | |
| 76 | |
| 77 | ProgramVertexState::ProgramVertexState() |
| 78 | { |
| 79 | mPV = NULL; |
| 80 | } |
| 81 | |
| 82 | ProgramVertexState::~ProgramVertexState() |
| 83 | { |
| 84 | delete mPV; |
| 85 | } |
| 86 | |
Jason Sams | 9c54bdb | 2009-06-17 16:52:59 -0700 | [diff] [blame] | 87 | void 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 Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 104 | |
| 105 | |
| 106 | namespace android { |
| 107 | namespace renderscript { |
| 108 | |
| 109 | void 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 | |
| 115 | RsProgramVertex rsi_ProgramVertexCreate(Context *rsc) |
| 116 | { |
| 117 | ProgramVertex *pv = rsc->mStateVertex.mPV; |
| 118 | pv->incRef(); |
| 119 | rsc->mStateVertex.mPV = 0; |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 120 | return pv; |
| 121 | } |
| 122 | |
| 123 | void 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 | |
| 129 | void rsi_ProgramVertexSetType(Context *rsc, uint32_t slot, RsType constants) |
| 130 | { |
| 131 | rsc->mStateVertex.mPV->setConstantType(slot, static_cast<const Type *>(constants)); |
| 132 | } |
| 133 | |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 134 | void rsi_ProgramVertexSetTextureMatrixEnable(Context *rsc, bool enable) |
| 135 | { |
| 136 | rsc->mStateVertex.mPV->setTextureMatrixEnable(enable); |
| 137 | } |
| 138 | |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 139 | |
| 140 | |
| 141 | } |
| 142 | } |