blob: ef892a51d7a5c5a7bd60a7b14035d81a8b708773 [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 Samsbe504f22010-01-25 12:31:24 -080047 mCount = 0;
Jason Sams433eca32010-01-06 11:57:52 -080048}
49
50VertexArray::Attrib::Attrib()
51{
52 clear();
53}
54
55void VertexArray::Attrib::set(const Attrib &a)
56{
57 buffer = a.buffer;
58 offset = a.offset;
59 type = a.type;
60 size = a.size;
61 stride = a.stride;
62 normalized = a.normalized;
63 name.setTo(a.name);
64}
65
66void VertexArray::Attrib::clear()
67{
68 buffer = 0;
69 offset = 0;
70 type = 0;
71 size = 0;
72 stride = 0;
73 normalized = false;
74 name.setTo("");
Jason Samsc460e552009-11-25 13:22:07 -080075}
76
Jason Samsbe504f22010-01-25 12:31:24 -080077void VertexArray::clear(uint32_t n)
Jason Samsc460e552009-11-25 13:22:07 -080078{
Jason Sams433eca32010-01-06 11:57:52 -080079 mAttribs[n].clear();
Jason Samsc460e552009-11-25 13:22:07 -080080}
81
Jason Sams79f52df2010-06-01 15:47:01 -070082void VertexArray::add(const Attrib &a, uint32_t stride)
Jason Samsc460e552009-11-25 13:22:07 -080083{
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -070084 rsAssert(mCount < RS_MAX_ATTRIBS);
Jason Samsbe504f22010-01-25 12:31:24 -080085 mAttribs[mCount].set(a);
86 mAttribs[mCount].buffer = mActiveBuffer;
Jason Sams760f1f72010-06-25 12:45:41 -070087 mAttribs[mCount].ptr = mActivePointer;
Jason Samsbe504f22010-01-25 12:31:24 -080088 mAttribs[mCount].stride = stride;
Jason Samsbe504f22010-01-25 12:31:24 -080089 mCount ++;
Jason Samsc460e552009-11-25 13:22:07 -080090}
91
Jason Sams79f52df2010-06-01 15:47:01 -070092void 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 -080093{
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -070094 rsAssert(mCount < RS_MAX_ATTRIBS);
Jason Samsbe504f22010-01-25 12:31:24 -080095 mAttribs[mCount].clear();
96 mAttribs[mCount].type = type;
97 mAttribs[mCount].size = size;
98 mAttribs[mCount].offset = offset;
99 mAttribs[mCount].normalized = normalized;
Jason Samsbe504f22010-01-25 12:31:24 -0800100 mAttribs[mCount].stride = stride;
Jason Sams79f52df2010-06-01 15:47:01 -0700101 mAttribs[mCount].name.setTo(name);
Jason Sams760f1f72010-06-25 12:45:41 -0700102
103 mAttribs[mCount].buffer = mActiveBuffer;
104 mAttribs[mCount].ptr = mActivePointer;
Jason Samsbe504f22010-01-25 12:31:24 -0800105 mCount ++;
Jason Sams433eca32010-01-06 11:57:52 -0800106}
107
108void VertexArray::logAttrib(uint32_t idx, uint32_t slot) const {
Jason Sams760f1f72010-06-25 12:45:41 -0700109 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 -0800110 mAttribs[idx].name.string(),
Jason Samsc460e552009-11-25 13:22:07 -0800111 mAttribs[idx].buffer,
Jason Sams760f1f72010-06-25 12:45:41 -0700112 mAttribs[idx].ptr,
Jason Samsc460e552009-11-25 13:22:07 -0800113 mAttribs[idx].size,
114 mAttribs[idx].type,
115 mAttribs[idx].stride,
Jason Samsd01d9702009-12-23 14:35:29 -0800116 mAttribs[idx].normalized,
Jason Samsc460e552009-11-25 13:22:07 -0800117 mAttribs[idx].offset);
118}
119
Jason Samsd01d9702009-12-23 14:35:29 -0800120void VertexArray::setupGL2(const Context *rsc, class VertexArrayState *state, ShaderCache *sc) const
Jason Samsc460e552009-11-25 13:22:07 -0800121{
Jason Sams3eb28f02010-01-27 14:41:43 -0800122 rsc->checkError("VertexArray::setupGL2 start");
Jason Sams79f52df2010-06-01 15:47:01 -0700123 for (uint32_t ct=1; ct <= 0xf/*state->mLastEnableCount*/; ct++) {
Jason Samsc460e552009-11-25 13:22:07 -0800124 glDisableVertexAttribArray(ct);
125 }
126
Jason Sams3eb28f02010-01-27 14:41:43 -0800127 rsc->checkError("VertexArray::setupGL2 disabled");
Jason Samsbe504f22010-01-25 12:31:24 -0800128 for (uint32_t ct=0; ct < mCount; ct++) {
129 uint32_t slot = 0;
Jason Samsbdb04602010-06-17 18:05:38 -0700130
131 if (mAttribs[ct].name[0] == '#') {
132 continue;
133 }
134
Jason Samsbe504f22010-01-25 12:31:24 -0800135 if (sc->isUserVertexProgram()) {
136 slot = sc->vtxAttribSlot(ct);
137 } else {
Jason Sams79f52df2010-06-01 15:47:01 -0700138 if (mAttribs[ct].name == "position") {
139 slot = 0;
140 } else if (mAttribs[ct].name == "color") {
141 slot = 1;
142 } else if (mAttribs[ct].name == "normal") {
143 slot = 2;
144 } else if (mAttribs[ct].name == "pointSize") {
145 slot = 3;
146 } else if (mAttribs[ct].name == "texture0") {
147 slot = 4;
148 } else {
Jason Samsaef14582010-02-08 16:31:39 -0800149 continue;
150 }
Jason Samsc460e552009-11-25 13:22:07 -0800151 }
Jason Samsbe504f22010-01-25 12:31:24 -0800152
Jason Sams976e2832010-06-02 12:40:19 -0700153 //logAttrib(ct, slot);
Jason Samsbe504f22010-01-25 12:31:24 -0800154 glEnableVertexAttribArray(slot);
155 glBindBuffer(GL_ARRAY_BUFFER, mAttribs[ct].buffer);
Jason Samsbe504f22010-01-25 12:31:24 -0800156 glVertexAttribPointer(slot,
157 mAttribs[ct].size,
158 mAttribs[ct].type,
159 mAttribs[ct].normalized,
160 mAttribs[ct].stride,
Jason Sams760f1f72010-06-25 12:45:41 -0700161 mAttribs[ct].ptr + mAttribs[ct].offset);
Jason Samsc460e552009-11-25 13:22:07 -0800162 }
Jason Sams3eb28f02010-01-27 14:41:43 -0800163 state->mLastEnableCount = mCount;
164 rsc->checkError("VertexArray::setupGL2 done");
Jason Samsc460e552009-11-25 13:22:07 -0800165}
166////////////////////////////////////////////
167
168void VertexArrayState::init(Context *) {
Jason Sams3eb28f02010-01-27 14:41:43 -0800169 mLastEnableCount = 0;
Jason Samsc460e552009-11-25 13:22:07 -0800170}
171