1/*
2 * Copyright (C) 2011 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 <rs_hal.h>
18#include <rsContext.h>
19
20#include <GLES/gl.h>
21#include <GLES2/gl2.h>
22
23#include "rsdGL.h"
24#include "rsdCore.h"
25#include "rsdVertexArray.h"
26#include "rsdShaderCache.h"
27
28using namespace android;
29using namespace android::renderscript;
30
31RsdVertexArray::RsdVertexArray(const Attrib *attribs, uint32_t numAttribs) {
32    mAttribs = attribs;
33    mCount = numAttribs;
34}
35
36RsdVertexArray::~RsdVertexArray() {
37}
38
39RsdVertexArray::Attrib::Attrib() {
40    clear();
41}
42
43void RsdVertexArray::Attrib::clear() {
44    buffer = 0;
45    offset = 0;
46    type = 0;
47    size = 0;
48    stride = 0;
49    ptr = NULL;
50    normalized = false;
51    name.setTo("");
52}
53
54void RsdVertexArray::Attrib::set(uint32_t type, uint32_t size, uint32_t stride,
55                              bool normalized, uint32_t offset,
56                              const char *name) {
57    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);
64}
65
66void RsdVertexArray::logAttrib(uint32_t idx, uint32_t slot) const {
67    if (idx == 0) {
68        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,
72         mAttribs[idx].name.string(),
73         mAttribs[idx].buffer,
74         mAttribs[idx].ptr,
75         mAttribs[idx].size,
76         mAttribs[idx].type,
77         mAttribs[idx].stride,
78         mAttribs[idx].normalized,
79         mAttribs[idx].offset);
80}
81
82void RsdVertexArray::setup(const Context *rsc) const {
83
84    RsdHal *dc = (RsdHal *)rsc->mHal.drv;
85    RsdVertexArrayState *state = dc->gl.vertexArrayState;
86    RsdShaderCache *sc = dc->gl.shaderCache;
87
88    rsdGLCheckError(rsc, "RsdVertexArray::setup start");
89    uint32_t maxAttrs = state->mAttrsEnabledSize;
90
91    for (uint32_t ct=1; ct < maxAttrs; ct++) {
92        if(state->mAttrsEnabled[ct]) {
93            glDisableVertexAttribArray(ct);
94            state->mAttrsEnabled[ct] = false;
95        }
96    }
97
98    rsdGLCheckError(rsc, "RsdVertexArray::setup disabled");
99    for (uint32_t ct=0; ct < mCount; ct++) {
100        int32_t slot = sc->vtxAttribSlot(mAttribs[ct].name);
101        if (rsc->props.mLogShadersAttr) {
102            logAttrib(ct, slot);
103        }
104        if (slot < 0 || slot >= (int32_t)maxAttrs) {
105            continue;
106        }
107        glEnableVertexAttribArray(slot);
108        state->mAttrsEnabled[slot] = true;
109        glBindBuffer(GL_ARRAY_BUFFER, mAttribs[ct].buffer);
110        glVertexAttribPointer(slot,
111                              mAttribs[ct].size,
112                              mAttribs[ct].type,
113                              mAttribs[ct].normalized,
114                              mAttribs[ct].stride,
115                              mAttribs[ct].ptr + mAttribs[ct].offset);
116    }
117    rsdGLCheckError(rsc, "RsdVertexArray::setup done");
118}
119////////////////////////////////////////////
120RsdVertexArrayState::RsdVertexArrayState() {
121    mAttrsEnabled = NULL;
122    mAttrsEnabledSize = 0;
123}
124
125RsdVertexArrayState::~RsdVertexArrayState() {
126    if (mAttrsEnabled) {
127        delete[] mAttrsEnabled;
128        mAttrsEnabled = NULL;
129    }
130}
131void RsdVertexArrayState::init(uint32_t maxAttrs) {
132    mAttrsEnabledSize = maxAttrs;
133    mAttrsEnabled = new bool[mAttrsEnabledSize];
134    for (uint32_t ct = 0; ct < mAttrsEnabledSize; ct++) {
135        mAttrsEnabled[ct] = false;
136    }
137}
138
139