blob: 5062156f1638e97b253b05233d9fef068c50a9e8 [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 -080026using namespace android;
27using namespace android::renderscript;
28
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080029VertexArray::VertexArray(const Attrib *attribs, uint32_t numAttribs) {
Alex Sakhartchouk54929cc2010-11-08 15:10:52 -080030 mAttribs = attribs;
31 mCount = numAttribs;
Jason Samsc460e552009-11-25 13:22:07 -080032}
33
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080034VertexArray::~VertexArray() {
Jason Samsc460e552009-11-25 13:22:07 -080035}
36
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080037VertexArray::Attrib::Attrib() {
Jason Sams433eca32010-01-06 11:57:52 -080038 clear();
39}
40
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080041void VertexArray::Attrib::clear() {
Jason Sams433eca32010-01-06 11:57:52 -080042 buffer = 0;
43 offset = 0;
44 type = 0;
45 size = 0;
46 stride = 0;
Jason Sams590549f2010-06-30 12:07:41 -070047 ptr = NULL;
Jason Sams433eca32010-01-06 11:57:52 -080048 normalized = false;
49 name.setTo("");
Jason Samsc460e552009-11-25 13:22:07 -080050}
51
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080052void VertexArray::Attrib::set(uint32_t type, uint32_t size, uint32_t stride,
53 bool normalized, uint32_t offset,
54 const char *name) {
Alex Sakhartchouk54929cc2010-11-08 15:10:52 -080055 clear();
56 this->type = type;
57 this->size = size;
58 this->offset = offset;
59 this->normalized = normalized;
60 this->stride = stride;
61 this->name.setTo(name);
Jason Sams433eca32010-01-06 11:57:52 -080062}
63
64void VertexArray::logAttrib(uint32_t idx, uint32_t slot) const {
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080065 if (idx == 0) {
Alex Sakhartchouk886f11a2010-09-29 09:49:13 -070066 LOGV("Starting vertex attribute binding");
67 }
68 LOGV("va %i: slot=%i name=%s buf=%i ptr=%p size=%i type=0x%x stride=0x%x norm=%i offset=0x%x",
69 idx, slot,
Jason Sams433eca32010-01-06 11:57:52 -080070 mAttribs[idx].name.string(),
Jason Samsc460e552009-11-25 13:22:07 -080071 mAttribs[idx].buffer,
Jason Sams760f1f72010-06-25 12:45:41 -070072 mAttribs[idx].ptr,
Jason Samsc460e552009-11-25 13:22:07 -080073 mAttribs[idx].size,
74 mAttribs[idx].type,
75 mAttribs[idx].stride,
Jason Samsd01d9702009-12-23 14:35:29 -080076 mAttribs[idx].normalized,
Jason Samsc460e552009-11-25 13:22:07 -080077 mAttribs[idx].offset);
78}
79
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080080void VertexArray::setupGL2(const Context *rsc,
81 class VertexArrayState *state,
82 ShaderCache *sc) const {
Jason Sams3eb28f02010-01-27 14:41:43 -080083 rsc->checkError("VertexArray::setupGL2 start");
Jason Sams79f52df2010-06-01 15:47:01 -070084 for (uint32_t ct=1; ct <= 0xf/*state->mLastEnableCount*/; ct++) {
Jason Samsc460e552009-11-25 13:22:07 -080085 glDisableVertexAttribArray(ct);
86 }
87
Jason Sams3eb28f02010-01-27 14:41:43 -080088 rsc->checkError("VertexArray::setupGL2 disabled");
Jason Samsbe504f22010-01-25 12:31:24 -080089 for (uint32_t ct=0; ct < mCount; ct++) {
Alex Sakhartchouk886f11a2010-09-29 09:49:13 -070090 int32_t slot = sc->vtxAttribSlot(mAttribs[ct].name);
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080091 if (rsc->props.mLogShadersAttr) {
Alex Sakhartchouk886f11a2010-09-29 09:49:13 -070092 logAttrib(ct, slot);
Jason Samsc460e552009-11-25 13:22:07 -080093 }
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080094 if (slot < 0) {
Alex Sakhartchoukaf83e792010-08-27 16:10:55 -070095 continue;
96 }
Jason Samsbe504f22010-01-25 12:31:24 -080097 glEnableVertexAttribArray(slot);
98 glBindBuffer(GL_ARRAY_BUFFER, mAttribs[ct].buffer);
Jason Samsbe504f22010-01-25 12:31:24 -080099 glVertexAttribPointer(slot,
100 mAttribs[ct].size,
101 mAttribs[ct].type,
102 mAttribs[ct].normalized,
103 mAttribs[ct].stride,
Jason Sams760f1f72010-06-25 12:45:41 -0700104 mAttribs[ct].ptr + mAttribs[ct].offset);
Jason Samsc460e552009-11-25 13:22:07 -0800105 }
Jason Sams3eb28f02010-01-27 14:41:43 -0800106 state->mLastEnableCount = mCount;
107 rsc->checkError("VertexArray::setupGL2 done");
Jason Samsc460e552009-11-25 13:22:07 -0800108}
109////////////////////////////////////////////
110
111void VertexArrayState::init(Context *) {
Jason Sams3eb28f02010-01-27 14:41:43 -0800112 mLastEnableCount = 0;
Jason Samsc460e552009-11-25 13:22:07 -0800113}
114