|
| 1 | +/* |
| 2 | + * cocos2d for iPhone: http://www.cocos2d-iphone.org |
| 3 | + * |
| 4 | + * Copyright (c) 2014 Cocos2D Authors |
| 5 | + * |
| 6 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | + * of this software and associated documentation files (the "Software"), to deal |
| 8 | + * in the Software without restriction, including without limitation the rights |
| 9 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | + * copies of the Software, and to permit persons to whom the Software is |
| 11 | + * furnished to do so, subject to the following conditions: |
| 12 | + * |
| 13 | + * The above copyright notice and this permission notice shall be included in |
| 14 | + * all copies or substantial portions of the Software. |
| 15 | + * |
| 16 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 22 | + * THE SOFTWARE. |
| 23 | + */ |
| 24 | + |
| 25 | +#import "CCRenderer_private.h" |
| 26 | + |
| 27 | +#import "CCRenderDispatch.h" |
| 28 | + |
| 29 | + |
| 30 | +static const CCGraphicsBufferType CCGraphicsBufferGLTypes[] = { |
| 31 | + GL_ARRAY_BUFFER, |
| 32 | + GL_ELEMENT_ARRAY_BUFFER, |
| 33 | +}; |
| 34 | + |
| 35 | + |
| 36 | +@interface CCGraphicsBufferGLBasic : CCGraphicsBuffer @end |
| 37 | +@implementation CCGraphicsBufferGLBasic { |
| 38 | + @public |
| 39 | + GLuint _buffer; |
| 40 | + GLenum _type; |
| 41 | +} |
| 42 | + |
| 43 | +-(instancetype)initWithCapacity:(NSUInteger)capacity elementSize:(size_t)elementSize type:(CCGraphicsBufferType)type; |
| 44 | +{ |
| 45 | + if((self = [super initWithCapacity:capacity elementSize:elementSize type:type])){ |
| 46 | + glGenBuffers(1, &_buffer); |
| 47 | + _type = CCGraphicsBufferGLTypes[type]; |
| 48 | + |
| 49 | + [self setup]; |
| 50 | + } |
| 51 | + |
| 52 | + return self; |
| 53 | +} |
| 54 | + |
| 55 | +-(void)setup |
| 56 | +{ |
| 57 | + _ptr = calloc(_capacity, _elementSize); |
| 58 | +} |
| 59 | + |
| 60 | +-(void)destroy |
| 61 | +{ |
| 62 | + free(_ptr); |
| 63 | + _ptr = NULL; |
| 64 | + |
| 65 | + GLuint buffer = _buffer; |
| 66 | + CCRenderDispatch(YES, ^{glDeleteBuffers(1, &buffer);}); |
| 67 | +} |
| 68 | + |
| 69 | +-(void)resize:(size_t)newCapacity; |
| 70 | +{ |
| 71 | + _ptr = realloc(_ptr, newCapacity*_elementSize); |
| 72 | + _capacity = newCapacity; |
| 73 | +} |
| 74 | + |
| 75 | +-(void)prepare; |
| 76 | +{ |
| 77 | + _count = 0; |
| 78 | +} |
| 79 | + |
| 80 | +-(void)commit; |
| 81 | +{ |
| 82 | + GLenum type = (GLenum)_type; |
| 83 | + glBindBuffer(type, _buffer); |
| 84 | + glBufferData(type, (GLsizei)(_count*_elementSize), _ptr, GL_STREAM_DRAW); |
| 85 | + glBindBuffer(type, 0); |
| 86 | +} |
| 87 | + |
| 88 | +@end |
| 89 | + |
| 90 | + |
| 91 | +#if __CC_PLATFORM_IOS |
| 92 | + |
| 93 | +@interface CCGraphicsBufferGLUnsynchronized : CCGraphicsBufferGLBasic @end |
| 94 | +@implementation CCGraphicsBufferGLUnsynchronized { |
| 95 | + GLvoid *(*_mapBufferRange)(GLenum target, GLintptr offset, GLsizeiptr length, GLbitfield access); |
| 96 | + GLvoid (*_flushMappedBufferRange)(GLenum target, GLintptr offset, GLsizeiptr length); |
| 97 | + GLboolean (*_unmapBuffer)(GLenum target); |
| 98 | +} |
| 99 | + |
| 100 | +#define BUFFER_ACCESS_WRITE (GL_MAP_WRITE_BIT_EXT | GL_MAP_UNSYNCHRONIZED_BIT_EXT | GL_MAP_INVALIDATE_BUFFER_BIT_EXT | GL_MAP_FLUSH_EXPLICIT_BIT_EXT) |
| 101 | +#define BUFFER_ACCESS_READ (GL_MAP_READ_BIT_EXT) |
| 102 | + |
| 103 | +-(instancetype)initWithCapacity:(NSUInteger)capacity elementSize:(size_t)elementSize type:(CCGraphicsBufferType)type |
| 104 | +{ |
| 105 | + if((self = [super initWithCapacity:capacity elementSize:elementSize type:type])){ |
| 106 | + // TODO Does Android look up GL functions by name like Windows/Linux? |
| 107 | + _mapBufferRange = glMapBufferRangeEXT; |
| 108 | + _flushMappedBufferRange = glFlushMappedBufferRangeEXT; |
| 109 | + _unmapBuffer = glUnmapBufferOES; |
| 110 | + } |
| 111 | + |
| 112 | + return self; |
| 113 | +} |
| 114 | + |
| 115 | +-(void)setup |
| 116 | +{ |
| 117 | + glBindBuffer(_type, _buffer); |
| 118 | + glBufferData(_type, _capacity*_elementSize, NULL, GL_STREAM_DRAW); |
| 119 | + glBindBuffer(_type, 0); |
| 120 | + CC_CHECK_GL_ERROR_DEBUG(); |
| 121 | +} |
| 122 | + |
| 123 | +-(void)destroy |
| 124 | +{ |
| 125 | + GLuint buffer = _buffer; |
| 126 | + CCRenderDispatch(YES, ^{glDeleteBuffers(1, &buffer);}); |
| 127 | +} |
| 128 | + |
| 129 | +-(void)resize:(size_t)newCapacity |
| 130 | +{ |
| 131 | + // This is a little tricky. |
| 132 | + // Need to resize the existing GL buffer object without creating a new name. |
| 133 | + |
| 134 | + // Make the buffer readable. |
| 135 | + glBindBuffer(_type, _buffer); |
| 136 | + GLsizei oldLength = (GLsizei)(_count*_elementSize); |
| 137 | + _flushMappedBufferRange(_type, 0, oldLength); |
| 138 | + _unmapBuffer(_type); |
| 139 | + void *oldBufferPtr = _mapBufferRange(_type, 0, oldLength, BUFFER_ACCESS_READ); |
| 140 | + |
| 141 | + // Copy the old contents into a temp buffer. |
| 142 | + GLsizei newLength = (GLsizei)(newCapacity*_elementSize); |
| 143 | + void *tempBufferPtr = malloc(newLength); |
| 144 | + memcpy(tempBufferPtr, oldBufferPtr, oldLength); |
| 145 | + |
| 146 | + // Copy that into a new GL buffer. |
| 147 | + _unmapBuffer(_type); |
| 148 | + glBufferData(_type, newLength, tempBufferPtr, GL_STREAM_DRAW); |
| 149 | + void *newBufferPtr = _mapBufferRange(_type, 0, newLength, BUFFER_ACCESS_WRITE); |
| 150 | + |
| 151 | + // Cleanup. |
| 152 | + free(tempBufferPtr); |
| 153 | + glBindBuffer(_type, 0); |
| 154 | + CC_CHECK_GL_ERROR_DEBUG(); |
| 155 | + |
| 156 | + // Update values. |
| 157 | + _ptr = newBufferPtr; |
| 158 | + _capacity = newCapacity; |
| 159 | +} |
| 160 | + |
| 161 | +-(void)prepare |
| 162 | +{ |
| 163 | + _count = 0; |
| 164 | + |
| 165 | + GLenum target = (GLenum)_type; |
| 166 | + glBindBuffer(_type, _buffer); |
| 167 | + _ptr = _mapBufferRange(target, 0, (GLsizei)(_capacity*_elementSize), BUFFER_ACCESS_WRITE); |
| 168 | + glBindBuffer(target, 0); |
| 169 | + CC_CHECK_GL_ERROR_DEBUG(); |
| 170 | +} |
| 171 | + |
| 172 | +-(void)commit |
| 173 | +{ |
| 174 | + glBindBuffer(_type, _buffer); |
| 175 | + _flushMappedBufferRange(_type, 0, (GLsizei)(_count*_elementSize)); |
| 176 | + _unmapBuffer(_type); |
| 177 | + glBindBuffer(_type, 0); |
| 178 | + CC_CHECK_GL_ERROR_DEBUG(); |
| 179 | + |
| 180 | + _ptr = NULL; |
| 181 | +} |
| 182 | + |
| 183 | +@end |
| 184 | + |
| 185 | +#endif |
| 186 | + |
| 187 | + |
| 188 | +@interface CCGraphicsBufferBindingsGL : NSObject <CCGraphicsBufferBindings> @end |
| 189 | +@implementation CCGraphicsBufferBindingsGL { |
| 190 | + GLuint _vao; |
| 191 | +} |
| 192 | + |
| 193 | +-(instancetype)initWithVertexBuffer:(CCGraphicsBufferGLBasic *)vertexBuffer indexBuffer:(CCGraphicsBufferGLBasic *)indexBuffer |
| 194 | +{ |
| 195 | + NSAssert([vertexBuffer isKindOfClass:[CCGraphicsBufferGLBasic class]], @"Wrong kind of buffer!"); |
| 196 | + NSAssert([indexBuffer isKindOfClass:[CCGraphicsBufferGLBasic class]], @"Wrong kind of buffer!"); |
| 197 | + |
| 198 | + if((self = [super init])){ |
| 199 | + CCGL_DEBUG_PUSH_GROUP_MARKER("CCGraphicsBufferBindingsGL: Creating VAO"); |
| 200 | + |
| 201 | + glGenVertexArrays(1, &_vao); |
| 202 | + glBindVertexArray(_vao); |
| 203 | + |
| 204 | + glEnableVertexAttribArray(CCShaderAttributePosition); |
| 205 | + glEnableVertexAttribArray(CCShaderAttributeTexCoord1); |
| 206 | + glEnableVertexAttribArray(CCShaderAttributeTexCoord2); |
| 207 | + glEnableVertexAttribArray(CCShaderAttributeColor); |
| 208 | + |
| 209 | + glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer->_buffer); |
| 210 | + glVertexAttribPointer(CCShaderAttributePosition, 4, GL_FLOAT, GL_FALSE, sizeof(CCVertex), (void *)offsetof(CCVertex, position)); |
| 211 | + glVertexAttribPointer(CCShaderAttributeTexCoord1, 2, GL_FLOAT, GL_FALSE, sizeof(CCVertex), (void *)offsetof(CCVertex, texCoord1)); |
| 212 | + glVertexAttribPointer(CCShaderAttributeTexCoord2, 2, GL_FLOAT, GL_FALSE, sizeof(CCVertex), (void *)offsetof(CCVertex, texCoord2)); |
| 213 | + glVertexAttribPointer(CCShaderAttributeColor, 4, GL_FLOAT, GL_FALSE, sizeof(CCVertex), (void *)offsetof(CCVertex, color)); |
| 214 | + |
| 215 | + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBuffer->_buffer); |
| 216 | + |
| 217 | + glBindVertexArray(0); |
| 218 | + glBindBuffer(GL_ARRAY_BUFFER, 0); |
| 219 | + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); |
| 220 | + |
| 221 | + CCGL_DEBUG_POP_GROUP_MARKER(); |
| 222 | + } |
| 223 | + |
| 224 | + return self; |
| 225 | +} |
| 226 | + |
| 227 | +-(void)dealloc |
| 228 | +{ |
| 229 | + GLuint vao = _vao; |
| 230 | + CCRenderDispatch(YES, ^{glDeleteVertexArrays(1, &vao);}); |
| 231 | +} |
| 232 | + |
| 233 | +-(void)bind:(BOOL)bind |
| 234 | +{ |
| 235 | + CCGL_DEBUG_INSERT_EVENT_MARKER("CCGraphicsBufferBindingsGL: Bind VAO"); |
| 236 | + glBindVertexArray(bind ? _vao : 0); |
| 237 | +} |
| 238 | + |
| 239 | +@end |
0 commit comments