blob: 354ee89f7c5b3379501ae245c886c7c3837b1dcc [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
17#include "rsContext.h"
Alex Sakhartchouk77d9f4b2011-01-31 14:53:24 -080018#ifndef ANDROID_RS_SERIALIZE
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#endif
22
Jason Samsc460e552009-11-25 13:22:07 -080023using namespace android;
24using namespace android::renderscript;
25
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080026VertexArray::VertexArray(const Attrib *attribs, uint32_t numAttribs) {
Alex Sakhartchouk54929cc2010-11-08 15:10:52 -080027 mAttribs = attribs;
28 mCount = numAttribs;
Jason Samsc460e552009-11-25 13:22:07 -080029}
30
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080031VertexArray::~VertexArray() {
Jason Samsc460e552009-11-25 13:22:07 -080032}
33
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080034VertexArray::Attrib::Attrib() {
Jason Sams433eca32010-01-06 11:57:52 -080035 clear();
36}
37
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080038void VertexArray::Attrib::clear() {
Jason Sams433eca32010-01-06 11:57:52 -080039 buffer = 0;
40 offset = 0;
41 type = 0;
42 size = 0;
43 stride = 0;
Jason Sams590549f2010-06-30 12:07:41 -070044 ptr = NULL;
Jason Sams433eca32010-01-06 11:57:52 -080045 normalized = false;
46 name.setTo("");
Jason Samsc460e552009-11-25 13:22:07 -080047}
48
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080049void VertexArray::Attrib::set(uint32_t type, uint32_t size, uint32_t stride,
50 bool normalized, uint32_t offset,
51 const char *name) {
Alex Sakhartchouk54929cc2010-11-08 15:10:52 -080052 clear();
53 this->type = type;
54 this->size = size;
55 this->offset = offset;
56 this->normalized = normalized;
57 this->stride = stride;
58 this->name.setTo(name);
Jason Sams433eca32010-01-06 11:57:52 -080059}
60
61void VertexArray::logAttrib(uint32_t idx, uint32_t slot) const {
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080062 if (idx == 0) {
Alex Sakhartchouk886f11a2010-09-29 09:49:13 -070063 LOGV("Starting vertex attribute binding");
64 }
65 LOGV("va %i: slot=%i name=%s buf=%i ptr=%p size=%i type=0x%x stride=0x%x norm=%i offset=0x%x",
66 idx, slot,
Jason Sams433eca32010-01-06 11:57:52 -080067 mAttribs[idx].name.string(),
Jason Samsc460e552009-11-25 13:22:07 -080068 mAttribs[idx].buffer,
Jason Sams760f1f72010-06-25 12:45:41 -070069 mAttribs[idx].ptr,
Jason Samsc460e552009-11-25 13:22:07 -080070 mAttribs[idx].size,
71 mAttribs[idx].type,
72 mAttribs[idx].stride,
Jason Samsd01d9702009-12-23 14:35:29 -080073 mAttribs[idx].normalized,
Jason Samsc460e552009-11-25 13:22:07 -080074 mAttribs[idx].offset);
75}
76
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080077void VertexArray::setupGL2(const Context *rsc,
78 class VertexArrayState *state,
79 ShaderCache *sc) const {
Jason Sams3eb28f02010-01-27 14:41:43 -080080 rsc->checkError("VertexArray::setupGL2 start");
Alex Sakhartchouk2d791972010-12-13 14:48:21 -080081 uint32_t maxAttrs = state->mAttrsEnabledSize;
82 for (uint32_t ct=1; ct < maxAttrs; ct++) {
83 if(state->mAttrsEnabled[ct]) {
84 glDisableVertexAttribArray(ct);
85 state->mAttrsEnabled[ct] = false;
86 }
Jason Samsc460e552009-11-25 13:22:07 -080087 }
88
Jason Sams3eb28f02010-01-27 14:41:43 -080089 rsc->checkError("VertexArray::setupGL2 disabled");
Jason Samsbe504f22010-01-25 12:31:24 -080090 for (uint32_t ct=0; ct < mCount; ct++) {
Alex Sakhartchouk886f11a2010-09-29 09:49:13 -070091 int32_t slot = sc->vtxAttribSlot(mAttribs[ct].name);
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080092 if (rsc->props.mLogShadersAttr) {
Alex Sakhartchouk886f11a2010-09-29 09:49:13 -070093 logAttrib(ct, slot);
Jason Samsc460e552009-11-25 13:22:07 -080094 }
Alex Sakhartchouk2d791972010-12-13 14:48:21 -080095 if (slot < 0 || slot >= (int32_t)maxAttrs) {
Alex Sakhartchoukaf83e792010-08-27 16:10:55 -070096 continue;
97 }
Jason Samsbe504f22010-01-25 12:31:24 -080098 glEnableVertexAttribArray(slot);
Alex Sakhartchouk2d791972010-12-13 14:48:21 -080099 state->mAttrsEnabled[slot] = true;
Jason Samsbe504f22010-01-25 12:31:24 -0800100 glBindBuffer(GL_ARRAY_BUFFER, mAttribs[ct].buffer);
Jason Samsbe504f22010-01-25 12:31:24 -0800101 glVertexAttribPointer(slot,
102 mAttribs[ct].size,
103 mAttribs[ct].type,
104 mAttribs[ct].normalized,
105 mAttribs[ct].stride,
Jason Sams760f1f72010-06-25 12:45:41 -0700106 mAttribs[ct].ptr + mAttribs[ct].offset);
Jason Samsc460e552009-11-25 13:22:07 -0800107 }
Jason Sams3eb28f02010-01-27 14:41:43 -0800108 rsc->checkError("VertexArray::setupGL2 done");
Jason Samsc460e552009-11-25 13:22:07 -0800109}
110////////////////////////////////////////////
Alex Sakhartchouk2d791972010-12-13 14:48:21 -0800111VertexArrayState::VertexArrayState() {
112 mAttrsEnabled = NULL;
113 mAttrsEnabledSize = 0;
114}
Jason Samsc460e552009-11-25 13:22:07 -0800115
Alex Sakhartchouk2d791972010-12-13 14:48:21 -0800116VertexArrayState::~VertexArrayState() {
117 if (mAttrsEnabled) {
118 delete[] mAttrsEnabled;
119 mAttrsEnabled = NULL;
120 }
121}
122void VertexArrayState::init(Context *rsc) {
123 mAttrsEnabledSize = rsc->getMaxVertexAttributes();
124 mAttrsEnabled = new bool[mAttrsEnabledSize];
125 for (uint32_t ct = 0; ct < mAttrsEnabledSize; ct++) {
126 mAttrsEnabled[ct] = false;
127 }
Jason Samsc460e552009-11-25 13:22:07 -0800128}
129