13c827367444ee418f129b2c238299f49d3264554Jarkko Poyry/*------------------------------------------------------------------------- 23c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * drawElements Quality Program OpenGL ES Utilities 33c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * ------------------------------------------------ 43c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * 53c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * Copyright 2014 The Android Open Source Project 63c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * 73c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * Licensed under the Apache License, Version 2.0 (the "License"); 83c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * you may not use this file except in compliance with the License. 93c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * You may obtain a copy of the License at 103c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * 113c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * http://www.apache.org/licenses/LICENSE-2.0 123c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * 133c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * Unless required by applicable law or agreed to in writing, software 143c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * distributed under the License is distributed on an "AS IS" BASIS, 153c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 163c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * See the License for the specific language governing permissions and 173c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * limitations under the License. 183c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * 193c827367444ee418f129b2c238299f49d3264554Jarkko Poyry *//*! 203c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * \file 213c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * \brief Buffer object wrapper. 223c827367444ee418f129b2c238299f49d3264554Jarkko Poyry *//*--------------------------------------------------------------------*/ 233c827367444ee418f129b2c238299f49d3264554Jarkko Poyry 243c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#include "gluObjectWrapper.hpp" 253c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#include "gluRenderContext.hpp" 263c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#include "gluStrUtil.hpp" 273c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#include "glwFunctions.hpp" 283c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#include "glwEnums.hpp" 29d6148171f88da1301f053e2e0236afc69416137cJarkko Pöyry#include "deArrayUtil.hpp" 303c827367444ee418f129b2c238299f49d3264554Jarkko Poyry 313c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#include <sstream> 323c827367444ee418f129b2c238299f49d3264554Jarkko Poyry 333c827367444ee418f129b2c238299f49d3264554Jarkko Poyrynamespace glu 343c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{ 353c827367444ee418f129b2c238299f49d3264554Jarkko Poyry 363c827367444ee418f129b2c238299f49d3264554Jarkko PoyryObjectWrapper::ObjectWrapper (const glw::Functions& gl, const ObjectTraits& traits) 373c827367444ee418f129b2c238299f49d3264554Jarkko Poyry : m_gl (gl) 383c827367444ee418f129b2c238299f49d3264554Jarkko Poyry , m_traits (traits) 393c827367444ee418f129b2c238299f49d3264554Jarkko Poyry , m_object (0) 403c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{ 413c827367444ee418f129b2c238299f49d3264554Jarkko Poyry (gl.*traits.genFunc)(1, &m_object); 423c827367444ee418f129b2c238299f49d3264554Jarkko Poyry 433c827367444ee418f129b2c238299f49d3264554Jarkko Poyry if (m_object == 0) 443c827367444ee418f129b2c238299f49d3264554Jarkko Poyry { 453c827367444ee418f129b2c238299f49d3264554Jarkko Poyry const deUint32 err = gl.getError(); 463c827367444ee418f129b2c238299f49d3264554Jarkko Poyry const char* objectName = traits.name; 473c827367444ee418f129b2c238299f49d3264554Jarkko Poyry std::ostringstream msg; 483c827367444ee418f129b2c238299f49d3264554Jarkko Poyry 493c827367444ee418f129b2c238299f49d3264554Jarkko Poyry msg << "Failed to create " << objectName << " object, got " << getErrorStr((int)err); 503c827367444ee418f129b2c238299f49d3264554Jarkko Poyry 513c827367444ee418f129b2c238299f49d3264554Jarkko Poyry if (err == GL_OUT_OF_MEMORY) 523c827367444ee418f129b2c238299f49d3264554Jarkko Poyry throw OutOfMemoryError(msg.str()); 533c827367444ee418f129b2c238299f49d3264554Jarkko Poyry else 543c827367444ee418f129b2c238299f49d3264554Jarkko Poyry throw Error((int)err, msg.str()); 553c827367444ee418f129b2c238299f49d3264554Jarkko Poyry } 563c827367444ee418f129b2c238299f49d3264554Jarkko Poyry} 573c827367444ee418f129b2c238299f49d3264554Jarkko Poyry 5853365959f2740e7b9ed59c51adbb73372f908a09Mika IsojärviObjectWrapper::ObjectWrapper (const glw::Functions& gl, const ObjectTraits& traits, deUint32 object) 5953365959f2740e7b9ed59c51adbb73372f908a09Mika Isojärvi : m_gl (gl) 6053365959f2740e7b9ed59c51adbb73372f908a09Mika Isojärvi , m_traits (traits) 6153365959f2740e7b9ed59c51adbb73372f908a09Mika Isojärvi , m_object (object) 6253365959f2740e7b9ed59c51adbb73372f908a09Mika Isojärvi{ 6353365959f2740e7b9ed59c51adbb73372f908a09Mika Isojärvi DE_ASSERT(object != 0); 6453365959f2740e7b9ed59c51adbb73372f908a09Mika Isojärvi} 6553365959f2740e7b9ed59c51adbb73372f908a09Mika Isojärvi 663c827367444ee418f129b2c238299f49d3264554Jarkko PoyryObjectWrapper::~ObjectWrapper (void) 673c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{ 683c827367444ee418f129b2c238299f49d3264554Jarkko Poyry (m_gl.*m_traits.deleteFunc)(1, &m_object); 693c827367444ee418f129b2c238299f49d3264554Jarkko Poyry} 703c827367444ee418f129b2c238299f49d3264554Jarkko Poyry 713c827367444ee418f129b2c238299f49d3264554Jarkko Poyrystatic const ObjectTraits s_objectTraits[OBJECTTYPE_LAST] = 723c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{ 733c827367444ee418f129b2c238299f49d3264554Jarkko Poyry { "texture", &glw::Functions::genTextures, &glw::Functions::deleteTextures }, 743c827367444ee418f129b2c238299f49d3264554Jarkko Poyry { "buffer", &glw::Functions::genBuffers, &glw::Functions::deleteBuffers }, 753c827367444ee418f129b2c238299f49d3264554Jarkko Poyry { "renderbuffer", &glw::Functions::genRenderbuffers, &glw::Functions::deleteRenderbuffers }, 763c827367444ee418f129b2c238299f49d3264554Jarkko Poyry { "framebuffer", &glw::Functions::genFramebuffers, &glw::Functions::deleteFramebuffers }, 773c827367444ee418f129b2c238299f49d3264554Jarkko Poyry { "transform feedback", &glw::Functions::genTransformFeedbacks, &glw::Functions::deleteTransformFeedbacks }, 783c827367444ee418f129b2c238299f49d3264554Jarkko Poyry { "vertex array", &glw::Functions::genVertexArrays, &glw::Functions::deleteVertexArrays }, 793c827367444ee418f129b2c238299f49d3264554Jarkko Poyry { "query", &glw::Functions::genQueries, &glw::Functions::deleteQueries }, 807af3c6f58d3b9426227e4354bf0109028560b6d9Jarkko Pöyry { "sampler", &glw::Functions::genSamplers, &glw::Functions::deleteSamplers }, 813c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}; 823c827367444ee418f129b2c238299f49d3264554Jarkko Poyry 833c827367444ee418f129b2c238299f49d3264554Jarkko Poyryconst ObjectTraits& objectTraits (ObjectType type) 843c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{ 853c827367444ee418f129b2c238299f49d3264554Jarkko Poyry return de::getSizedArrayElement<OBJECTTYPE_LAST>(s_objectTraits, type); 863c827367444ee418f129b2c238299f49d3264554Jarkko Poyry} 873c827367444ee418f129b2c238299f49d3264554Jarkko Poyry 883c827367444ee418f129b2c238299f49d3264554Jarkko PoyryObjectVector::ObjectVector (const glw::Functions& gl, const ObjectTraits& traits, size_t numObjects) 893c827367444ee418f129b2c238299f49d3264554Jarkko Poyry : m_gl (gl) 903c827367444ee418f129b2c238299f49d3264554Jarkko Poyry , m_traits (traits) 913c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{ 923c827367444ee418f129b2c238299f49d3264554Jarkko Poyry if (numObjects > 0) 933c827367444ee418f129b2c238299f49d3264554Jarkko Poyry resize(numObjects); 943c827367444ee418f129b2c238299f49d3264554Jarkko Poyry} 953c827367444ee418f129b2c238299f49d3264554Jarkko Poyry 963c827367444ee418f129b2c238299f49d3264554Jarkko PoyryObjectVector::~ObjectVector (void) 973c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{ 983c827367444ee418f129b2c238299f49d3264554Jarkko Poyry clear(); 993c827367444ee418f129b2c238299f49d3264554Jarkko Poyry} 1003c827367444ee418f129b2c238299f49d3264554Jarkko Poyry 1013c827367444ee418f129b2c238299f49d3264554Jarkko Poyryvoid ObjectVector::resize (size_t newSize) 1023c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{ 1033c827367444ee418f129b2c238299f49d3264554Jarkko Poyry const size_t oldSize = m_objects.size(); 1043c827367444ee418f129b2c238299f49d3264554Jarkko Poyry 1053c827367444ee418f129b2c238299f49d3264554Jarkko Poyry if (newSize == 0) 1063c827367444ee418f129b2c238299f49d3264554Jarkko Poyry { 1073c827367444ee418f129b2c238299f49d3264554Jarkko Poyry clear(); // Avoid size_t (unsigned) overflow issues in delete path. 1083c827367444ee418f129b2c238299f49d3264554Jarkko Poyry } 1093c827367444ee418f129b2c238299f49d3264554Jarkko Poyry if (oldSize < newSize) 1103c827367444ee418f129b2c238299f49d3264554Jarkko Poyry { 1113c827367444ee418f129b2c238299f49d3264554Jarkko Poyry m_objects.resize(newSize, 0); 1123c827367444ee418f129b2c238299f49d3264554Jarkko Poyry (m_gl.*m_traits.genFunc)(glw::GLsizei(newSize - oldSize), &m_objects[oldSize]); 1133c827367444ee418f129b2c238299f49d3264554Jarkko Poyry } 1143c827367444ee418f129b2c238299f49d3264554Jarkko Poyry else if (oldSize > newSize) 1153c827367444ee418f129b2c238299f49d3264554Jarkko Poyry { 1162e751e3e77060f699c560104e82379d3ce54f67aPyry Haulos (m_gl.*m_traits.deleteFunc)(glw::GLsizei(oldSize - newSize), &m_objects[newSize]); 1173c827367444ee418f129b2c238299f49d3264554Jarkko Poyry m_objects.resize(newSize); 1183c827367444ee418f129b2c238299f49d3264554Jarkko Poyry } 1193c827367444ee418f129b2c238299f49d3264554Jarkko Poyry} 1203c827367444ee418f129b2c238299f49d3264554Jarkko Poyry 1213c827367444ee418f129b2c238299f49d3264554Jarkko Poyryvoid ObjectVector::clear (void) 1223c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{ 1232e751e3e77060f699c560104e82379d3ce54f67aPyry Haulos (m_gl.*m_traits.deleteFunc)(glw::GLsizei(m_objects.size()), &m_objects.front()); 1243c827367444ee418f129b2c238299f49d3264554Jarkko Poyry m_objects.clear(); 1253c827367444ee418f129b2c238299f49d3264554Jarkko Poyry} 1263c827367444ee418f129b2c238299f49d3264554Jarkko Poyry 1273c827367444ee418f129b2c238299f49d3264554Jarkko Poyry} // glu 128