blob: 62ec107aec3a30cbf52a74561c5826967dae9492 [file] [log] [blame]
Jason Samsc460e552009-11-25 13:22:07 -08001/*
Alex Sakhartchouka04e30d2011-04-29 16:49:08 -07002 * Copyright (C) 2011 The Android Open Source Project
Jason Samsc460e552009-11-25 13:22:07 -08003 *
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 Sakhartchouka04e30d2011-04-29 16:49:08 -070017#include <rs_hal.h>
18#include <rsContext.h>
19
Jason Samsc460e552009-11-25 13:22:07 -080020#include <GLES/gl.h>
21#include <GLES2/gl2.h>
Alex Sakhartchouka04e30d2011-04-29 16:49:08 -070022
Alex Sakhartchoukc19ff012011-05-06 14:59:45 -070023#include "rsdGL.h"
Alex Sakhartchouka04e30d2011-04-29 16:49:08 -070024#include "rsdCore.h"
25#include "rsdVertexArray.h"
26#include "rsdShaderCache.h"
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -070027
Jason Samsc460e552009-11-25 13:22:07 -080028using namespace android;
29using namespace android::renderscript;
30
Alex Sakhartchouka04e30d2011-04-29 16:49:08 -070031RsdVertexArray::RsdVertexArray(const Attrib *attribs, uint32_t numAttribs) {
Alex Sakhartchouk54929cc2010-11-08 15:10:52 -080032 mAttribs = attribs;
33 mCount = numAttribs;
Jason Samsc460e552009-11-25 13:22:07 -080034}
35
Alex Sakhartchouka04e30d2011-04-29 16:49:08 -070036RsdVertexArray::~RsdVertexArray() {
Jason Samsc460e552009-11-25 13:22:07 -080037}
38
Alex Sakhartchouka04e30d2011-04-29 16:49:08 -070039RsdVertexArray::Attrib::Attrib() {
Jason Sams433eca32010-01-06 11:57:52 -080040 clear();
41}
42
Alex Sakhartchouka04e30d2011-04-29 16:49:08 -070043void RsdVertexArray::Attrib::clear() {
Jason Sams433eca32010-01-06 11:57:52 -080044 buffer = 0;
45 offset = 0;
46 type = 0;
47 size = 0;
48 stride = 0;
Jason Sams590549f2010-06-30 12:07:41 -070049 ptr = NULL;
Jason Sams433eca32010-01-06 11:57:52 -080050 normalized = false;
51 name.setTo("");
Jason Samsc460e552009-11-25 13:22:07 -080052}
53
Alex Sakhartchouka04e30d2011-04-29 16:49:08 -070054void RsdVertexArray::Attrib::set(uint32_t type, uint32_t size, uint32_t stride,
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080055 bool normalized, uint32_t offset,
56 const char *name) {
Alex Sakhartchouk54929cc2010-11-08 15:10:52 -080057 clear();
58 this->type = type;
59 this->size = size;
60 this->offset = offset;
61 this->normalized = normalized;
62 this->stride = stride;
63 this->name.setTo(name);
Jason Sams433eca32010-01-06 11:57:52 -080064}
65
Alex Sakhartchouka04e30d2011-04-29 16:49:08 -070066void RsdVertexArray::logAttrib(uint32_t idx, uint32_t slot) const {
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080067 if (idx == 0) {
Alex Sakhartchouk886f11a2010-09-29 09:49:13 -070068 LOGV("Starting vertex attribute binding");
69 }
70 LOGV("va %i: slot=%i name=%s buf=%i ptr=%p size=%i type=0x%x stride=0x%x norm=%i offset=0x%x",
71 idx, slot,
Jason Sams433eca32010-01-06 11:57:52 -080072 mAttribs[idx].name.string(),
Jason Samsc460e552009-11-25 13:22:07 -080073 mAttribs[idx].buffer,
Jason Sams760f1f72010-06-25 12:45:41 -070074 mAttribs[idx].ptr,
Jason Samsc460e552009-11-25 13:22:07 -080075 mAttribs[idx].size,
76 mAttribs[idx].type,
77 mAttribs[idx].stride,
Jason Samsd01d9702009-12-23 14:35:29 -080078 mAttribs[idx].normalized,
Jason Samsc460e552009-11-25 13:22:07 -080079 mAttribs[idx].offset);
80}
81
Alex Sakhartchoukc19ff012011-05-06 14:59:45 -070082void RsdVertexArray::setup(const Context *rsc) const {
Alex Sakhartchouka04e30d2011-04-29 16:49:08 -070083
84 RsdHal *dc = (RsdHal *)rsc->mHal.drv;
85 RsdVertexArrayState *state = dc->gl.vertexArrayState;
86 RsdShaderCache *sc = dc->gl.shaderCache;
87
Alex Sakhartchoukc19ff012011-05-06 14:59:45 -070088 rsdGLCheckError(rsc, "RsdVertexArray::setup start");
Alex Sakhartchouk2d791972010-12-13 14:48:21 -080089 uint32_t maxAttrs = state->mAttrsEnabledSize;
Alex Sakhartchouka04e30d2011-04-29 16:49:08 -070090
Alex Sakhartchouk2d791972010-12-13 14:48:21 -080091 for (uint32_t ct=1; ct < maxAttrs; ct++) {
92 if(state->mAttrsEnabled[ct]) {
93 glDisableVertexAttribArray(ct);
94 state->mAttrsEnabled[ct] = false;
95 }
Jason Samsc460e552009-11-25 13:22:07 -080096 }
97
Alex Sakhartchoukc19ff012011-05-06 14:59:45 -070098 rsdGLCheckError(rsc, "RsdVertexArray::setup disabled");
Jason Samsbe504f22010-01-25 12:31:24 -080099 for (uint32_t ct=0; ct < mCount; ct++) {
Alex Sakhartchouk886f11a2010-09-29 09:49:13 -0700100 int32_t slot = sc->vtxAttribSlot(mAttribs[ct].name);
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800101 if (rsc->props.mLogShadersAttr) {
Alex Sakhartchouk886f11a2010-09-29 09:49:13 -0700102 logAttrib(ct, slot);
Jason Samsc460e552009-11-25 13:22:07 -0800103 }
Alex Sakhartchouk2d791972010-12-13 14:48:21 -0800104 if (slot < 0 || slot >= (int32_t)maxAttrs) {
Alex Sakhartchoukaf83e792010-08-27 16:10:55 -0700105 continue;
106 }
Jason Samsbe504f22010-01-25 12:31:24 -0800107 glEnableVertexAttribArray(slot);
Alex Sakhartchouk2d791972010-12-13 14:48:21 -0800108 state->mAttrsEnabled[slot] = true;
Jason Samsbe504f22010-01-25 12:31:24 -0800109 glBindBuffer(GL_ARRAY_BUFFER, mAttribs[ct].buffer);
Jason Samsbe504f22010-01-25 12:31:24 -0800110 glVertexAttribPointer(slot,
111 mAttribs[ct].size,
112 mAttribs[ct].type,
113 mAttribs[ct].normalized,
114 mAttribs[ct].stride,
Jason Sams760f1f72010-06-25 12:45:41 -0700115 mAttribs[ct].ptr + mAttribs[ct].offset);
Jason Samsc460e552009-11-25 13:22:07 -0800116 }
Alex Sakhartchoukc19ff012011-05-06 14:59:45 -0700117 rsdGLCheckError(rsc, "RsdVertexArray::setup done");
Jason Samsc460e552009-11-25 13:22:07 -0800118}
119////////////////////////////////////////////
Alex Sakhartchouka04e30d2011-04-29 16:49:08 -0700120RsdVertexArrayState::RsdVertexArrayState() {
Alex Sakhartchouk2d791972010-12-13 14:48:21 -0800121 mAttrsEnabled = NULL;
122 mAttrsEnabledSize = 0;
123}
Jason Samsc460e552009-11-25 13:22:07 -0800124
Alex Sakhartchouka04e30d2011-04-29 16:49:08 -0700125RsdVertexArrayState::~RsdVertexArrayState() {
Alex Sakhartchouk2d791972010-12-13 14:48:21 -0800126 if (mAttrsEnabled) {
127 delete[] mAttrsEnabled;
128 mAttrsEnabled = NULL;
129 }
130}
Alex Sakhartchouka04e30d2011-04-29 16:49:08 -0700131void RsdVertexArrayState::init(uint32_t maxAttrs) {
132 mAttrsEnabledSize = maxAttrs;
Alex Sakhartchouk2d791972010-12-13 14:48:21 -0800133 mAttrsEnabled = new bool[mAttrsEnabledSize];
134 for (uint32_t ct = 0; ct < mAttrsEnabledSize; ct++) {
135 mAttrsEnabled[ct] = false;
136 }
Jason Samsc460e552009-11-25 13:22:07 -0800137}
138