OSDN Git Service

476b1530c03cb0f89df295704c1b36c0f6018ebc
[android-x86/external-swiftshader.git] / src / Radiance / libRAD / Buffer.cpp
1 // SwiftShader Software Renderer
2 //
3 // Copyright(c) 2005-2013 TransGaming Inc.
4 //
5 // All rights reserved. No part of this software may be copied, distributed, transmitted,
6 // transcribed, stored in a retrieval system, translated into any human or computer
7 // language by any means, or disclosed to third parties without the explicit written
8 // agreement of TransGaming Inc. Without such an agreement, no rights or licenses, express
9 // or implied, including but not limited to any patent rights, are granted to you.
10 //
11
12 // Buffer.cpp: Implements the Buffer class, representing storage of vertex and/or
13 // index data. Implements GL buffer objects and related functionality.
14 // [OpenGL ES 2.0.24] section 2.9 page 21.
15
16 #include "Buffer.h"
17
18 #include "main.h"
19 #include "VertexDataManager.h"
20 #include "IndexDataManager.h"
21
22 namespace rad
23 {
24
25 Buffer::Buffer(GLuint id) : RefCountObject(id)
26 {
27     mContents = 0;
28     mSize = 0;
29     mUsage = GL_DYNAMIC_DRAW;
30 }
31
32 Buffer::~Buffer()
33 {
34     if(mContents)
35         {
36                 mContents->destruct();
37         }
38 }
39
40 void Buffer::bufferData(const void *data, GLsizeiptr size, GLenum usage)
41 {
42         if(mContents)
43         {
44                 mContents->destruct();
45                 mContents = 0;
46         }
47
48         mSize = size;
49         mUsage = usage;
50
51         if(size > 0)
52         {
53                 const int padding = 1024;   // For SIMD processing of vertices
54                 mContents = new sw::Resource(size + padding);
55
56                 if(!mContents)
57                 {
58                         return error(GL_OUT_OF_MEMORY);
59                 }
60
61                 if(data)
62                 {
63                         memcpy((void*)mContents->getBuffer(), data, size);
64                 }
65         }
66 }
67
68 void Buffer::bufferSubData(const void *data, GLsizeiptr size, GLintptr offset)
69 {
70         if(mContents)
71         {
72                 char *buffer = (char*)mContents->lock(sw::PUBLIC);
73                 memcpy(buffer + offset, data, size);
74                 mContents->unlock();
75         }
76 }
77
78 sw::Resource *Buffer::getResource()
79 {
80         return mContents;
81 }
82
83 }