blob: 075a70da7b74ee271586de72f033d155012212df [file] [log] [blame]
Jason Samsc460e552009-11-25 13:22:07 -08001/*
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
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -070017#ifndef ANDROID_RS_BUILD_FOR_HOST
Jason Samsc460e552009-11-25 13:22:07 -080018#include "rsContext.h"
Jason Samsc460e552009-11-25 13:22:07 -080019#include <GLES/gl.h>
20#include <GLES2/gl2.h>
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -070021#else
22#include "rsContextHostStub.h"
23#include <OpenGL/gl.h>
24#endif
25
Jason Samsc460e552009-11-25 13:22:07 -080026
27using namespace android;
28using namespace android::renderscript;
29
30
31VertexArray::VertexArray()
32{
Jason Samsbe504f22010-01-25 12:31:24 -080033 clearAll();
Jason Samsc460e552009-11-25 13:22:07 -080034}
35
36VertexArray::~VertexArray()
37{
38}
39
40
41void VertexArray::clearAll()
42{
Jason Sams433eca32010-01-06 11:57:52 -080043 for (uint32_t ct=0; ct < RS_MAX_ATTRIBS; ct++) {
44 mAttribs[ct].clear();
45 }
Jason Samsc460e552009-11-25 13:22:07 -080046 mActiveBuffer = 0;
Jason Sams590549f2010-06-30 12:07:41 -070047 mActivePointer = NULL;
Jason Samsbe504f22010-01-25 12:31:24 -080048 mCount = 0;
Jason Sams433eca32010-01-06 11:57:52 -080049}
50
51VertexArray::Attrib::Attrib()
52{
53 clear();
54}
55
56void VertexArray::Attrib::set(const Attrib &a)
57{
58 buffer = a.buffer;
Jason Sams590549f2010-06-30 12:07:41 -070059 ptr = a.ptr;
Jason Sams433eca32010-01-06 11:57:52 -080060 offset = a.offset;
61 type = a.type;
62 size = a.size;
63 stride = a.stride;
64 normalized = a.normalized;
65 name.setTo(a.name);
66}
67
68void VertexArray::Attrib::clear()
69{
70 buffer = 0;
71 offset = 0;
72 type = 0;
73 size = 0;
74 stride = 0;
Jason Sams590549f2010-06-30 12:07:41 -070075 ptr = NULL;
Jason Sams433eca32010-01-06 11:57:52 -080076 normalized = false;
77 name.setTo("");
Jason Samsc460e552009-11-25 13:22:07 -080078}
79
Jason Samsbe504f22010-01-25 12:31:24 -080080void VertexArray::clear(uint32_t n)
Jason Samsc460e552009-11-25 13:22:07 -080081{
Jason Sams433eca32010-01-06 11:57:52 -080082 mAttribs[n].clear();
Jason Samsc460e552009-11-25 13:22:07 -080083}
84
Jason Sams79f52df2010-06-01 15:47:01 -070085void VertexArray::add(const Attrib &a, uint32_t stride)
Jason Samsc460e552009-11-25 13:22:07 -080086{
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -070087 rsAssert(mCount < RS_MAX_ATTRIBS);
Jason Samsbe504f22010-01-25 12:31:24 -080088 mAttribs[mCount].set(a);
89 mAttribs[mCount].buffer = mActiveBuffer;
Jason Sams760f1f72010-06-25 12:45:41 -070090 mAttribs[mCount].ptr = mActivePointer;
Jason Samsbe504f22010-01-25 12:31:24 -080091 mAttribs[mCount].stride = stride;
Jason Samsbe504f22010-01-25 12:31:24 -080092 mCount ++;
Jason Samsc460e552009-11-25 13:22:07 -080093}
94
Jason Sams79f52df2010-06-01 15:47:01 -070095void VertexArray::add(uint32_t type, uint32_t size, uint32_t stride, bool normalized, uint32_t offset, const char *name)
Jason Samsc460e552009-11-25 13:22:07 -080096{
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -070097 rsAssert(mCount < RS_MAX_ATTRIBS);
Jason Samsbe504f22010-01-25 12:31:24 -080098 mAttribs[mCount].clear();
99 mAttribs[mCount].type = type;
100 mAttribs[mCount].size = size;
101 mAttribs[mCount].offset = offset;
102 mAttribs[mCount].normalized = normalized;
Jason Samsbe504f22010-01-25 12:31:24 -0800103 mAttribs[mCount].stride = stride;
Jason Sams79f52df2010-06-01 15:47:01 -0700104 mAttribs[mCount].name.setTo(name);
Jason Sams760f1f72010-06-25 12:45:41 -0700105
106 mAttribs[mCount].buffer = mActiveBuffer;
107 mAttribs[mCount].ptr = mActivePointer;
Jason Samsbe504f22010-01-25 12:31:24 -0800108 mCount ++;
Jason Sams433eca32010-01-06 11:57:52 -0800109}
110
111void VertexArray::logAttrib(uint32_t idx, uint32_t slot) const {
Jason Sams760f1f72010-06-25 12:45:41 -0700112 LOGE("va %i: slot=%i name=%s buf=%i ptr=%p size=%i type=0x%x stride=0x%x norm=%i offset=0x%x", idx, slot,
Jason Sams433eca32010-01-06 11:57:52 -0800113 mAttribs[idx].name.string(),
Jason Samsc460e552009-11-25 13:22:07 -0800114 mAttribs[idx].buffer,
Jason Sams760f1f72010-06-25 12:45:41 -0700115 mAttribs[idx].ptr,
Jason Samsc460e552009-11-25 13:22:07 -0800116 mAttribs[idx].size,
117 mAttribs[idx].type,
118 mAttribs[idx].stride,
Jason Samsd01d9702009-12-23 14:35:29 -0800119 mAttribs[idx].normalized,
Jason Samsc460e552009-11-25 13:22:07 -0800120 mAttribs[idx].offset);
121}
122
Jason Samsd01d9702009-12-23 14:35:29 -0800123void VertexArray::setupGL2(const Context *rsc, class VertexArrayState *state, ShaderCache *sc) const
Jason Samsc460e552009-11-25 13:22:07 -0800124{
Jason Sams3eb28f02010-01-27 14:41:43 -0800125 rsc->checkError("VertexArray::setupGL2 start");
Jason Sams79f52df2010-06-01 15:47:01 -0700126 for (uint32_t ct=1; ct <= 0xf/*state->mLastEnableCount*/; ct++) {
Jason Samsc460e552009-11-25 13:22:07 -0800127 glDisableVertexAttribArray(ct);
128 }
129
Jason Sams3eb28f02010-01-27 14:41:43 -0800130 rsc->checkError("VertexArray::setupGL2 disabled");
Jason Samsbe504f22010-01-25 12:31:24 -0800131 for (uint32_t ct=0; ct < mCount; ct++) {
Alex Sakhartchoukaf83e792010-08-27 16:10:55 -0700132 int32_t slot = 0;
Jason Samsbdb04602010-06-17 18:05:38 -0700133
134 if (mAttribs[ct].name[0] == '#') {
135 continue;
136 }
137
Jason Samsbe504f22010-01-25 12:31:24 -0800138 if (sc->isUserVertexProgram()) {
139 slot = sc->vtxAttribSlot(ct);
140 } else {
Jason Sams79f52df2010-06-01 15:47:01 -0700141 if (mAttribs[ct].name == "position") {
142 slot = 0;
143 } else if (mAttribs[ct].name == "color") {
144 slot = 1;
145 } else if (mAttribs[ct].name == "normal") {
146 slot = 2;
Jason Sams79f52df2010-06-01 15:47:01 -0700147 } else if (mAttribs[ct].name == "texture0") {
Jason Sams479e2922010-07-09 15:34:32 -0700148 slot = 3;
Jason Sams79f52df2010-06-01 15:47:01 -0700149 } else {
Jason Samsaef14582010-02-08 16:31:39 -0800150 continue;
151 }
Jason Samsc460e552009-11-25 13:22:07 -0800152 }
Alex Sakhartchoukaf83e792010-08-27 16:10:55 -0700153 if(slot < 0) {
154 continue;
155 }
Jason Samsbe504f22010-01-25 12:31:24 -0800156
Jason Sams976e2832010-06-02 12:40:19 -0700157 //logAttrib(ct, slot);
Jason Samsbe504f22010-01-25 12:31:24 -0800158 glEnableVertexAttribArray(slot);
159 glBindBuffer(GL_ARRAY_BUFFER, mAttribs[ct].buffer);
Jason Samsbe504f22010-01-25 12:31:24 -0800160 glVertexAttribPointer(slot,
161 mAttribs[ct].size,
162 mAttribs[ct].type,
163 mAttribs[ct].normalized,
164 mAttribs[ct].stride,
Jason Sams760f1f72010-06-25 12:45:41 -0700165 mAttribs[ct].ptr + mAttribs[ct].offset);
Jason Samsc460e552009-11-25 13:22:07 -0800166 }
Jason Sams3eb28f02010-01-27 14:41:43 -0800167 state->mLastEnableCount = mCount;
168 rsc->checkError("VertexArray::setupGL2 done");
Jason Samsc460e552009-11-25 13:22:07 -0800169}
170////////////////////////////////////////////
171
172void VertexArrayState::init(Context *) {
Jason Sams3eb28f02010-01-27 14:41:43 -0800173 mLastEnableCount = 0;
Jason Samsc460e552009-11-25 13:22:07 -0800174}
175