Commit 53fae3e9 by Nicolas Capens Committed by Nicolas Capens

Eliminate GL buffers.

Bug 18591036 Change-Id: Ieb77935e0afd41c78dff2bb892185bc2d31505b8 Reviewed-on: https://swiftshader-review.googlesource.com/1540Reviewed-by: 's avatarNicolas Capens <nicolascapens@google.com> Tested-by: 's avatarNicolas Capens <nicolascapens@google.com>
parent ae17152b
...@@ -15,7 +15,7 @@ ...@@ -15,7 +15,7 @@
namespace sw namespace sw
{ {
Resource::Resource(int bytes) Resource::Resource(size_t bytes) : size(bytes)
{ {
blocked = 0; blocked = 0;
...@@ -171,7 +171,7 @@ namespace sw ...@@ -171,7 +171,7 @@ namespace sw
criticalSection.unlock(); criticalSection.unlock();
} }
const void *Resource::getBuffer() const const void *Resource::data() const
{ {
return buffer; return buffer;
} }
......
...@@ -27,7 +27,7 @@ namespace sw ...@@ -27,7 +27,7 @@ namespace sw
class Resource class Resource
{ {
public: public:
Resource(int bytes); Resource(size_t bytes);
void destruct(); // Asynchronous destructor void destruct(); // Asynchronous destructor
...@@ -36,7 +36,8 @@ namespace sw ...@@ -36,7 +36,8 @@ namespace sw
void unlock(); void unlock();
void unlock(Accessor relinquisher); void unlock(Accessor relinquisher);
const void *getBuffer() const; // FIXME const void *data() const;
const size_t size;
private: private:
~Resource(); // Always call destruct() instead ~Resource(); // Always call destruct() instead
......
...@@ -60,7 +60,7 @@ void Buffer::bufferData(const void *data, GLsizeiptr size, GLenum usage) ...@@ -60,7 +60,7 @@ void Buffer::bufferData(const void *data, GLsizeiptr size, GLenum usage)
if(data) if(data)
{ {
memcpy((void*)mContents->getBuffer(), data, size); memcpy((void*)mContents->data(), data, size);
} }
} }
} }
......
...@@ -37,7 +37,7 @@ class Buffer : public RefCountObject ...@@ -37,7 +37,7 @@ class Buffer : public RefCountObject
void bufferData(const void *data, GLsizeiptr size, GLenum usage); void bufferData(const void *data, GLsizeiptr size, GLenum usage);
void bufferSubData(const void *data, GLsizeiptr size, GLintptr offset); void bufferSubData(const void *data, GLsizeiptr size, GLintptr offset);
const void *data() { return mContents ? mContents->getBuffer() : 0; } const void *data() { return mContents ? mContents->data() : 0; }
size_t size() const { return mSize; } size_t size() const { return mSize; }
GLenum usage() const { return mUsage; } GLenum usage() const { return mUsage; }
......
...@@ -1671,7 +1671,7 @@ GLenum Context::applyVertexBuffer(GLint base, GLint first, GLsizei count) ...@@ -1671,7 +1671,7 @@ GLenum Context::applyVertexBuffer(GLint base, GLint first, GLsizei count)
for(int i = 0; i < MAX_VERTEX_ATTRIBS; i++) for(int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
{ {
sw::Resource *resource = attributes[i].vertexBuffer; sw::Resource *resource = attributes[i].vertexBuffer;
const void *buffer = (char*)resource->getBuffer() + attributes[i].offset; const void *buffer = (char*)resource->data() + attributes[i].offset;
int stride = attributes[i].stride; int stride = attributes[i].stride;
......
...@@ -60,7 +60,7 @@ void Buffer::bufferData(const void *data, GLsizeiptr size, GLenum usage) ...@@ -60,7 +60,7 @@ void Buffer::bufferData(const void *data, GLsizeiptr size, GLenum usage)
if(data) if(data)
{ {
memcpy((void*)mContents->getBuffer(), data, size); memcpy((void*)mContents->data(), data, size);
} }
} }
} }
......
...@@ -37,7 +37,7 @@ class Buffer : public RefCountObject ...@@ -37,7 +37,7 @@ class Buffer : public RefCountObject
void bufferData(const void *data, GLsizeiptr size, GLenum usage); void bufferData(const void *data, GLsizeiptr size, GLenum usage);
void bufferSubData(const void *data, GLsizeiptr size, GLintptr offset); void bufferSubData(const void *data, GLsizeiptr size, GLintptr offset);
const void *data() { return mContents ? mContents->getBuffer() : 0; } const void *data() { return mContents ? mContents->data() : 0; }
size_t size() const { return mSize; } size_t size() const { return mSize; }
GLenum usage() const { return mUsage; } GLenum usage() const { return mUsage; }
......
...@@ -1941,7 +1941,7 @@ GLenum Context::applyVertexBuffer(GLint base, GLint first, GLsizei count) ...@@ -1941,7 +1941,7 @@ GLenum Context::applyVertexBuffer(GLint base, GLint first, GLsizei count)
} }
sw::Resource *resource = attributes[i].vertexBuffer; sw::Resource *resource = attributes[i].vertexBuffer;
const void *buffer = (char*)resource->getBuffer() + attributes[i].offset; const void *buffer = (char*)resource->data() + attributes[i].offset;
int stride = attributes[i].stride; int stride = attributes[i].stride;
......
...@@ -22,62 +22,4 @@ ...@@ -22,62 +22,4 @@
namespace es2 namespace es2
{ {
Buffer::Buffer(GLuint id) : RefCountObject(id)
{
mContents = 0;
mSize = 0;
mUsage = GL_DYNAMIC_DRAW;
}
Buffer::~Buffer()
{
if(mContents)
{
mContents->destruct();
}
}
void Buffer::bufferData(const void *data, GLsizeiptr size, GLenum usage)
{
if(mContents)
{
mContents->destruct();
mContents = 0;
}
mSize = size;
mUsage = usage;
if(size > 0)
{
const int padding = 1024; // For SIMD processing of vertices
mContents = new sw::Resource(size + padding);
if(!mContents)
{
return error(GL_OUT_OF_MEMORY);
}
if(data)
{
memcpy((void*)mContents->getBuffer(), data, size);
}
}
}
void Buffer::bufferSubData(const void *data, GLsizeiptr size, GLintptr offset)
{
if(mContents)
{
char *buffer = (char*)mContents->lock(sw::PUBLIC);
memcpy(buffer + offset, data, size);
mContents->unlock();
}
}
sw::Resource *Buffer::getResource()
{
return mContents;
}
} }
...@@ -27,27 +27,7 @@ ...@@ -27,27 +27,7 @@
namespace es2 namespace es2
{ {
class Buffer : public RefCountObject
{
public:
explicit Buffer(GLuint id);
virtual ~Buffer();
void bufferData(const void *data, GLsizeiptr size, GLenum usage);
void bufferSubData(const void *data, GLsizeiptr size, GLintptr offset);
const void *data() { return mContents ? mContents->getBuffer() : 0; }
size_t size() const { return mSize; }
GLenum usage() const { return mUsage; }
sw::Resource *getResource();
public:
sw::Resource *mContents;
size_t mSize;
GLenum mUsage;
};
} }
......
...@@ -106,12 +106,8 @@ struct Color ...@@ -106,12 +106,8 @@ struct Color
class VertexAttribute class VertexAttribute
{ {
public: public:
VertexAttribute() : mType(GL_FLOAT), mSize(0), mNormalized(false), mStride(0), mPointer(NULL), mArrayEnabled(false) VertexAttribute() : mType(GL_FLOAT), mSize(0), mNormalized(false), mStride(0), mOffset(0), mArrayEnabled(false)
{ {
mCurrentValue[0] = 0.0f;
mCurrentValue[1] = 0.0f;
mCurrentValue[2] = 0.0f;
mCurrentValue[3] = 1.0f;
} }
int typeSize() const int typeSize() const
...@@ -138,17 +134,11 @@ class VertexAttribute ...@@ -138,17 +134,11 @@ class VertexAttribute
GLint mSize; GLint mSize;
bool mNormalized; bool mNormalized;
GLsizei mStride; // 0 means natural stride GLsizei mStride; // 0 means natural stride
union
{
const void *mPointer;
intptr_t mOffset; intptr_t mOffset;
};
BindingPointer<Buffer> mBoundBuffer; // Captured when glVertexAttribPointer is called. sw::Resource *buffer;
bool mArrayEnabled; // From glEnable/DisableVertexAttribArray bool mArrayEnabled; // From glEnable/DisableVertexAttribArray
float mCurrentValue[4]; // From glVertexAttrib
}; };
typedef VertexAttribute VertexAttributeArray[MAX_VERTEX_ATTRIBS]; typedef VertexAttribute VertexAttributeArray[MAX_VERTEX_ATTRIBS];
...@@ -222,8 +212,7 @@ struct State ...@@ -222,8 +212,7 @@ struct State
bool depthMask; bool depthMask;
unsigned int activeSampler; // Active texture unit selector - GL_TEXTURE0 unsigned int activeSampler; // Active texture unit selector - GL_TEXTURE0
BindingPointer<Buffer> arrayBuffer; sw::Resource *elementArrayBuffer;
BindingPointer<Buffer> elementArrayBuffer;
GLuint readFramebuffer; GLuint readFramebuffer;
GLuint drawFramebuffer; GLuint drawFramebuffer;
BindingPointer<Renderbuffer> renderbuffer; BindingPointer<Renderbuffer> renderbuffer;
...@@ -317,13 +306,10 @@ public: ...@@ -317,13 +306,10 @@ public:
GLuint getActiveQuery(GLenum target) const; GLuint getActiveQuery(GLenum target) const;
GLuint getArrayBufferHandle() const;
void setEnableVertexAttribArray(unsigned int attribNum, bool enabled); void setEnableVertexAttribArray(unsigned int attribNum, bool enabled);
const VertexAttribute &getVertexAttribState(unsigned int attribNum); const VertexAttribute &getVertexAttribState(unsigned int attribNum);
void setVertexAttribState(unsigned int attribNum, Buffer *boundBuffer, GLint size, GLenum type, void setVertexAttribState(unsigned int attribNum, sw::Resource *buffer, GLint size, GLenum type,
bool normalized, GLsizei stride, const void *pointer); bool normalized, GLsizei stride, intptr_t offset);
const void *getVertexAttribPointer(unsigned int attribNum) const;
const VertexAttributeArray &getVertexAttributes(); const VertexAttributeArray &getVertexAttributes();
...@@ -335,13 +321,11 @@ public: ...@@ -335,13 +321,11 @@ public:
// These create and destroy methods are merely pass-throughs to // These create and destroy methods are merely pass-throughs to
// ResourceManager, which owns these object types // ResourceManager, which owns these object types
GLuint createBuffer();
GLuint createShader(GLenum type); GLuint createShader(GLenum type);
GLuint createProgram(); GLuint createProgram();
GLuint createTexture(); GLuint createTexture();
GLuint createRenderbuffer(); GLuint createRenderbuffer();
void deleteBuffer(GLuint buffer);
void deleteShader(GLuint shader); void deleteShader(GLuint shader);
void deleteProgram(GLuint program); void deleteProgram(GLuint program);
void deleteTexture(GLuint texture); void deleteTexture(GLuint texture);
...@@ -359,8 +343,6 @@ public: ...@@ -359,8 +343,6 @@ public:
GLuint createQuery(); GLuint createQuery();
void deleteQuery(GLuint query); void deleteQuery(GLuint query);
void bindArrayBuffer(GLuint buffer);
void bindElementArrayBuffer(GLuint buffer);
void bindTexture2D(GLuint texture); void bindTexture2D(GLuint texture);
void bindTextureCubeMap(GLuint texture); void bindTextureCubeMap(GLuint texture);
void bindTextureExternal(GLuint texture); void bindTextureExternal(GLuint texture);
...@@ -376,9 +358,6 @@ public: ...@@ -376,9 +358,6 @@ public:
void setRenderbufferStorage(RenderbufferStorage *renderbuffer); void setRenderbufferStorage(RenderbufferStorage *renderbuffer);
void setVertexAttrib(GLuint index, const GLfloat *values);
Buffer *getBuffer(GLuint handle);
Fence *getFence(GLuint handle); Fence *getFence(GLuint handle);
Shader *getShader(GLuint handle); Shader *getShader(GLuint handle);
Program *getProgram(GLuint handle); Program *getProgram(GLuint handle);
...@@ -387,8 +366,6 @@ public: ...@@ -387,8 +366,6 @@ public:
virtual Renderbuffer *getRenderbuffer(GLuint handle); virtual Renderbuffer *getRenderbuffer(GLuint handle);
Query *getQuery(GLuint handle, bool create, GLenum type); Query *getQuery(GLuint handle, bool create, GLenum type);
Buffer *getArrayBuffer();
Buffer *getElementArrayBuffer();
Program *getCurrentProgram(); Program *getCurrentProgram();
Texture2D *getTexture2D(); Texture2D *getTexture2D();
TextureCubeMap *getTextureCubeMap(); TextureCubeMap *getTextureCubeMap();
...@@ -397,12 +374,6 @@ public: ...@@ -397,12 +374,6 @@ public:
Framebuffer *getReadFramebuffer(); Framebuffer *getReadFramebuffer();
Framebuffer *getDrawFramebuffer(); Framebuffer *getDrawFramebuffer();
bool getFloatv(GLenum pname, GLfloat *params);
bool getIntegerv(GLenum pname, GLint *params);
bool getBooleanv(GLenum pname, GLboolean *params);
bool getQueryParameterInfo(GLenum pname, GLenum *type, unsigned int *numParams);
void readPixels(GLint x, GLint y, GLsizei width, GLsizei height, GLenum format, GLenum type, GLsizei *bufSize, void* pixels); void readPixels(GLint x, GLint y, GLsizei width, GLsizei height, GLenum format, GLenum type, GLsizei *bufSize, void* pixels);
void clear(GLbitfield mask); void clear(GLbitfield mask);
void drawArrays(GLenum mode, GLint first, GLsizei count); void drawArrays(GLenum mode, GLint first, GLsizei count);
...@@ -442,7 +413,6 @@ public: ...@@ -442,7 +413,6 @@ public:
void applyTextures(sw::SamplerType type); void applyTextures(sw::SamplerType type);
void applyTexture(sw::SamplerType type, int sampler, Texture *texture); void applyTexture(sw::SamplerType type, int sampler, Texture *texture);
void detachBuffer(GLuint buffer);
void detachTexture(GLuint texture); void detachTexture(GLuint texture);
void detachFramebuffer(GLuint framebuffer); void detachFramebuffer(GLuint framebuffer);
void detachRenderbuffer(GLuint renderbuffer); void detachRenderbuffer(GLuint renderbuffer);
......
...@@ -20,27 +20,15 @@ ...@@ -20,27 +20,15 @@
#include <string.h> #include <string.h>
#include <algorithm> #include <algorithm>
namespace
{
enum { INITIAL_INDEX_BUFFER_SIZE = 4096 * sizeof(GLuint) };
}
namespace es2 namespace es2
{ {
IndexDataManager::IndexDataManager() IndexDataManager::IndexDataManager()
{ {
mStreamingBuffer = new StreamingIndexBuffer(INITIAL_INDEX_BUFFER_SIZE);
if(!mStreamingBuffer)
{
ERR("Failed to allocate the streaming index buffer.");
}
} }
IndexDataManager::~IndexDataManager() IndexDataManager::~IndexDataManager()
{ {
delete mStreamingBuffer;
} }
void copyIndices(GLenum type, const void *input, GLsizei count, void *output) void copyIndices(GLenum type, const void *input, GLsizei count, void *output)
...@@ -90,18 +78,11 @@ void computeRange(GLenum type, const void *indices, GLsizei count, GLuint *minIn ...@@ -90,18 +78,11 @@ void computeRange(GLenum type, const void *indices, GLsizei count, GLuint *minIn
else UNREACHABLE(); else UNREACHABLE();
} }
GLenum IndexDataManager::prepareIndexData(GLenum type, GLsizei count, Buffer *buffer, const void *indices, TranslatedIndexData *translated) GLenum IndexDataManager::prepareIndexData(GLenum type, GLsizei count, sw::Resource *buffer, const void *indices, TranslatedIndexData *translated)
{ {
if(!mStreamingBuffer)
{
return GL_OUT_OF_MEMORY;
}
intptr_t offset = reinterpret_cast<intptr_t>(indices); intptr_t offset = reinterpret_cast<intptr_t>(indices);
bool alignedOffset = false; bool alignedOffset = false;
if(buffer != NULL)
{
switch(type) switch(type)
{ {
case GL_UNSIGNED_BYTE: alignedOffset = (offset % sizeof(GLubyte) == 0); break; case GL_UNSIGNED_BYTE: alignedOffset = (offset % sizeof(GLubyte) == 0); break;
...@@ -110,47 +91,17 @@ GLenum IndexDataManager::prepareIndexData(GLenum type, GLsizei count, Buffer *bu ...@@ -110,47 +91,17 @@ GLenum IndexDataManager::prepareIndexData(GLenum type, GLsizei count, Buffer *bu
default: UNREACHABLE(); alignedOffset = false; default: UNREACHABLE(); alignedOffset = false;
} }
if(typeSize(type) * count + offset > static_cast<std::size_t>(buffer->size())) if(typeSize(type) * count + offset > static_cast<std::size_t>(buffer->size))
{ {
return GL_INVALID_OPERATION; return GL_INVALID_OPERATION;
} }
indices = static_cast<const GLubyte*>(buffer->data()) + offset; indices = static_cast<const GLubyte*>(buffer->data()) + offset;
}
StreamingIndexBuffer *streamingBuffer = mStreamingBuffer;
sw::Resource *staticBuffer = buffer ? buffer->getResource() : NULL;
if(staticBuffer)
{
computeRange(type, indices, count, &translated->minIndex, &translated->maxIndex); computeRange(type, indices, count, &translated->minIndex, &translated->maxIndex);
translated->indexBuffer = staticBuffer; translated->indexBuffer = buffer;
translated->indexOffset = offset; translated->indexOffset = offset;
}
else
{
unsigned int streamOffset = 0;
int convertCount = count;
streamingBuffer->reserveSpace(convertCount * typeSize(type), type);
void *output = streamingBuffer->map(typeSize(type) * convertCount, &streamOffset);
if(output == NULL)
{
ERR("Failed to map index buffer.");
return GL_OUT_OF_MEMORY;
}
copyIndices(type, staticBuffer ? buffer->data() : indices, convertCount, output);
streamingBuffer->unmap();
computeRange(type, indices, count, &translated->minIndex, &translated->maxIndex);
translated->indexBuffer = streamingBuffer->getResource();
translated->indexOffset = streamOffset;
}
return GL_NO_ERROR; return GL_NO_ERROR;
} }
...@@ -166,94 +117,4 @@ std::size_t IndexDataManager::typeSize(GLenum type) ...@@ -166,94 +117,4 @@ std::size_t IndexDataManager::typeSize(GLenum type)
} }
} }
StreamingIndexBuffer::StreamingIndexBuffer(unsigned int initialSize) : mBufferSize(initialSize), mIndexBuffer(NULL)
{
if(initialSize > 0)
{
mIndexBuffer = new sw::Resource(initialSize + 16);
if(!mIndexBuffer)
{
ERR("Out of memory allocating an index buffer of size %lu.", initialSize);
}
}
mWritePosition = 0;
}
StreamingIndexBuffer::~StreamingIndexBuffer()
{
if(mIndexBuffer)
{
mIndexBuffer->destruct();
}
}
void *StreamingIndexBuffer::map(unsigned int requiredSpace, unsigned int *offset)
{
void *mapPtr = NULL;
if(mIndexBuffer)
{
mapPtr = (char*)mIndexBuffer->lock(sw::PUBLIC) + mWritePosition;
if(!mapPtr)
{
ERR(" Lock failed");
return NULL;
}
*offset = mWritePosition;
mWritePosition += requiredSpace;
}
return mapPtr;
}
void StreamingIndexBuffer::unmap()
{
if(mIndexBuffer)
{
mIndexBuffer->unlock();
}
}
void StreamingIndexBuffer::reserveSpace(unsigned int requiredSpace, GLenum type)
{
if(requiredSpace > mBufferSize)
{
if(mIndexBuffer)
{
mIndexBuffer->destruct();
mIndexBuffer = 0;
}
mBufferSize = std::max(requiredSpace, 2 * mBufferSize);
mIndexBuffer = new sw::Resource(mBufferSize + 16);
if(!mIndexBuffer)
{
ERR("Out of memory allocating an index buffer of size %lu.", mBufferSize);
}
mWritePosition = 0;
}
else if(mWritePosition + requiredSpace > mBufferSize) // Recycle
{
if(mIndexBuffer)
{
mIndexBuffer->destruct();
mIndexBuffer = new sw::Resource(mBufferSize + 16);
}
mWritePosition = 0;
}
}
sw::Resource *StreamingIndexBuffer::getResource() const
{
return mIndexBuffer;
}
} }
...@@ -32,36 +32,15 @@ struct TranslatedIndexData ...@@ -32,36 +32,15 @@ struct TranslatedIndexData
sw::Resource *indexBuffer; sw::Resource *indexBuffer;
}; };
class StreamingIndexBuffer
{
public:
StreamingIndexBuffer(unsigned int initialSize);
virtual ~StreamingIndexBuffer();
void *map(unsigned int requiredSpace, unsigned int *offset);
void unmap();
void reserveSpace(unsigned int requiredSpace, GLenum type);
sw::Resource *getResource() const;
private:
sw::Resource *mIndexBuffer;
unsigned int mBufferSize;
unsigned int mWritePosition;
};
class IndexDataManager class IndexDataManager
{ {
public: public:
IndexDataManager(); IndexDataManager();
virtual ~IndexDataManager(); virtual ~IndexDataManager();
GLenum prepareIndexData(GLenum type, GLsizei count, Buffer *arrayElementBuffer, const void *indices, TranslatedIndexData *translated); GLenum prepareIndexData(GLenum type, GLsizei count, sw::Resource *arrayElementBuffer, const void *indices, TranslatedIndexData *translated);
static std::size_t typeSize(GLenum type); static std::size_t typeSize(GLenum type);
private:
StreamingIndexBuffer *mStreamingBuffer;
}; };
} }
......
...@@ -29,11 +29,6 @@ ResourceManager::ResourceManager() ...@@ -29,11 +29,6 @@ ResourceManager::ResourceManager()
ResourceManager::~ResourceManager() ResourceManager::~ResourceManager()
{ {
while(!mBufferMap.empty())
{
deleteBuffer(mBufferMap.begin()->first);
}
while(!mProgramMap.empty()) while(!mProgramMap.empty())
{ {
deleteProgram(mProgramMap.begin()->first); deleteProgram(mProgramMap.begin()->first);
...@@ -68,16 +63,6 @@ void ResourceManager::release() ...@@ -68,16 +63,6 @@ void ResourceManager::release()
} }
} }
// Returns an unused buffer name
GLuint ResourceManager::createBuffer()
{
GLuint handle = mBufferHandleAllocator.allocate();
mBufferMap[handle] = NULL;
return handle;
}
// Returns an unused shader/program name // Returns an unused shader/program name
GLuint ResourceManager::createShader(GLenum type) GLuint ResourceManager::createShader(GLenum type)
{ {
...@@ -126,18 +111,6 @@ GLuint ResourceManager::createRenderbuffer() ...@@ -126,18 +111,6 @@ GLuint ResourceManager::createRenderbuffer()
return handle; return handle;
} }
void ResourceManager::deleteBuffer(GLuint buffer)
{
BufferMap::iterator bufferObject = mBufferMap.find(buffer);
if(bufferObject != mBufferMap.end())
{
mBufferHandleAllocator.release(bufferObject->first);
if(bufferObject->second) bufferObject->second->release();
mBufferMap.erase(bufferObject);
}
}
void ResourceManager::deleteShader(GLuint shader) void ResourceManager::deleteShader(GLuint shader)
{ {
ShaderMap::iterator shaderObject = mShaderMap.find(shader); ShaderMap::iterator shaderObject = mShaderMap.find(shader);
...@@ -200,20 +173,6 @@ void ResourceManager::deleteRenderbuffer(GLuint renderbuffer) ...@@ -200,20 +173,6 @@ void ResourceManager::deleteRenderbuffer(GLuint renderbuffer)
} }
} }
Buffer *ResourceManager::getBuffer(unsigned int handle)
{
BufferMap::iterator buffer = mBufferMap.find(handle);
if(buffer == mBufferMap.end())
{
return NULL;
}
else
{
return buffer->second;
}
}
Shader *ResourceManager::getShader(unsigned int handle) Shader *ResourceManager::getShader(unsigned int handle)
{ {
ShaderMap::iterator shader = mShaderMap.find(handle); ShaderMap::iterator shader = mShaderMap.find(handle);
...@@ -283,16 +242,6 @@ void ResourceManager::setRenderbuffer(GLuint handle, Renderbuffer *buffer) ...@@ -283,16 +242,6 @@ void ResourceManager::setRenderbuffer(GLuint handle, Renderbuffer *buffer)
mRenderbufferMap[handle] = buffer; mRenderbufferMap[handle] = buffer;
} }
void ResourceManager::checkBufferAllocation(unsigned int buffer)
{
if(buffer != 0 && !getBuffer(buffer))
{
Buffer *bufferObject = new Buffer(buffer);
mBufferMap[buffer] = bufferObject;
bufferObject->addRef();
}
}
void ResourceManager::checkTextureAllocation(GLuint texture, TextureType type) void ResourceManager::checkTextureAllocation(GLuint texture, TextureType type)
{ {
if(!getTexture(texture) && texture != 0) if(!getTexture(texture) && texture != 0)
......
...@@ -24,7 +24,6 @@ ...@@ -24,7 +24,6 @@
namespace es2 namespace es2
{ {
class Buffer;
class Shader; class Shader;
class Program; class Program;
class Texture; class Texture;
...@@ -55,13 +54,11 @@ class ResourceManager ...@@ -55,13 +54,11 @@ class ResourceManager
GLuint createTexture(); GLuint createTexture();
GLuint createRenderbuffer(); GLuint createRenderbuffer();
void deleteBuffer(GLuint buffer);
void deleteShader(GLuint shader); void deleteShader(GLuint shader);
void deleteProgram(GLuint program); void deleteProgram(GLuint program);
void deleteTexture(GLuint texture); void deleteTexture(GLuint texture);
void deleteRenderbuffer(GLuint renderbuffer); void deleteRenderbuffer(GLuint renderbuffer);
Buffer *getBuffer(GLuint handle);
Shader *getShader(GLuint handle); Shader *getShader(GLuint handle);
Program *getProgram(GLuint handle); Program *getProgram(GLuint handle);
Texture *getTexture(GLuint handle); Texture *getTexture(GLuint handle);
...@@ -69,16 +66,11 @@ class ResourceManager ...@@ -69,16 +66,11 @@ class ResourceManager
void setRenderbuffer(GLuint handle, Renderbuffer *renderbuffer); void setRenderbuffer(GLuint handle, Renderbuffer *renderbuffer);
void checkBufferAllocation(unsigned int buffer);
void checkTextureAllocation(GLuint texture, TextureType type); void checkTextureAllocation(GLuint texture, TextureType type);
private: private:
std::size_t mRefCount; std::size_t mRefCount;
typedef std::map<GLint, Buffer*> BufferMap;
BufferMap mBufferMap;
HandleAllocator mBufferHandleAllocator;
typedef std::map<GLint, Shader*> ShaderMap; typedef std::map<GLint, Shader*> ShaderMap;
ShaderMap mShaderMap; ShaderMap mShaderMap;
......
...@@ -19,119 +19,22 @@ ...@@ -19,119 +19,22 @@
#include "IndexDataManager.h" #include "IndexDataManager.h"
#include "common/debug.h" #include "common/debug.h"
namespace
{
enum {INITIAL_STREAM_BUFFER_SIZE = 1024 * 1024};
}
namespace es2 namespace es2
{ {
VertexDataManager::VertexDataManager(Context *context) : mContext(context) VertexDataManager::VertexDataManager(Context *context) : mContext(context)
{ {
for(int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
{
mDirtyCurrentValue[i] = true;
mCurrentValueBuffer[i] = NULL;
}
mStreamingBuffer = new StreamingVertexBuffer(INITIAL_STREAM_BUFFER_SIZE);
if(!mStreamingBuffer)
{
ERR("Failed to allocate the streaming vertex buffer.");
}
} }
VertexDataManager::~VertexDataManager() VertexDataManager::~VertexDataManager()
{ {
delete mStreamingBuffer;
for(int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
{
delete mCurrentValueBuffer[i];
}
}
unsigned int VertexDataManager::writeAttributeData(StreamingVertexBuffer *vertexBuffer, GLint start, GLsizei count, const VertexAttribute &attribute)
{
Buffer *buffer = attribute.mBoundBuffer.get();
int inputStride = attribute.stride();
int elementSize = attribute.typeSize();
unsigned int streamOffset = 0;
char *output = NULL;
if(vertexBuffer)
{
output = (char*)vertexBuffer->map(attribute, attribute.typeSize() * count, &streamOffset);
}
if(output == NULL)
{
ERR("Failed to map vertex buffer.");
return -1;
}
const char *input = NULL;
if(buffer)
{
int offset = attribute.mOffset;
input = static_cast<const char*>(buffer->data()) + offset;
}
else
{
input = static_cast<const char*>(attribute.mPointer);
}
input += inputStride * start;
if(inputStride == elementSize)
{
memcpy(output, input, count * inputStride);
}
else
{
for(int i = 0; i < count; i++)
{
memcpy(output, input, elementSize);
output += elementSize;
input += inputStride;
}
}
vertexBuffer->unmap();
return streamOffset;
} }
GLenum VertexDataManager::prepareVertexData(GLint start, GLsizei count, TranslatedAttribute *translated) GLenum VertexDataManager::prepareVertexData(GLint start, GLsizei count, TranslatedAttribute *translated)
{ {
if(!mStreamingBuffer)
{
return GL_OUT_OF_MEMORY;
}
const VertexAttributeArray &attribs = mContext->getVertexAttributes(); const VertexAttributeArray &attribs = mContext->getVertexAttributes();
Program *program = mContext->getCurrentProgram(); Program *program = mContext->getCurrentProgram();
// Determine the required storage size per used buffer
for(int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
{
if(program->getAttributeStream(i) != -1 && attribs[i].mArrayEnabled)
{
if(!attribs[i].mBoundBuffer)
{
mStreamingBuffer->addRequiredSpace(attribs[i].typeSize() * count);
}
}
}
mStreamingBuffer->reserveRequiredSpace();
// Perform the vertex data translations // Perform the vertex data translations
for(int i = 0; i < MAX_VERTEX_ATTRIBS; i++) for(int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
{ {
...@@ -139,36 +42,9 @@ GLenum VertexDataManager::prepareVertexData(GLint start, GLsizei count, Translat ...@@ -139,36 +42,9 @@ GLenum VertexDataManager::prepareVertexData(GLint start, GLsizei count, Translat
{ {
if(attribs[i].mArrayEnabled) if(attribs[i].mArrayEnabled)
{ {
Buffer *buffer = attribs[i].mBoundBuffer.get(); translated[i].vertexBuffer = attribs[i].buffer;
if(!buffer && attribs[i].mPointer == NULL)
{
// This is an application error that would normally result in a crash, but we catch it and return an error
ERR("An enabled vertex array has no buffer and no pointer.");
return GL_INVALID_OPERATION;
}
sw::Resource *staticBuffer = buffer ? buffer->getResource() : NULL;
if(staticBuffer)
{
translated[i].vertexBuffer = staticBuffer;
translated[i].offset = start * attribs[i].stride() + attribs[i].mOffset; translated[i].offset = start * attribs[i].stride() + attribs[i].mOffset;
translated[i].stride = attribs[i].stride(); translated[i].stride = attribs[i].stride();
}
else
{
unsigned int streamOffset = writeAttributeData(mStreamingBuffer, start, count, attribs[i]);
if(streamOffset == -1)
{
return GL_OUT_OF_MEMORY;
}
translated[i].vertexBuffer = mStreamingBuffer->getResource();
translated[i].offset = streamOffset;
translated[i].stride = attribs[i].typeSize();
}
switch(attribs[i].mType) switch(attribs[i].mType)
{ {
...@@ -186,15 +62,7 @@ GLenum VertexDataManager::prepareVertexData(GLint start, GLsizei count, Translat ...@@ -186,15 +62,7 @@ GLenum VertexDataManager::prepareVertexData(GLint start, GLsizei count, Translat
} }
else else
{ {
if(mDirtyCurrentValue[i]) translated[i].vertexBuffer = 0;
{
delete mCurrentValueBuffer[i];
mCurrentValueBuffer[i] = new ConstantVertexBuffer(attribs[i].mCurrentValue[0], attribs[i].mCurrentValue[1], attribs[i].mCurrentValue[2], attribs[i].mCurrentValue[3]);
mDirtyCurrentValue[i] = false;
}
translated[i].vertexBuffer = mCurrentValueBuffer[i]->getResource();
translated[i].type = sw::STREAMTYPE_FLOAT; translated[i].type = sw::STREAMTYPE_FLOAT;
translated[i].count = 4; translated[i].count = 4;
translated[i].stride = 0; translated[i].stride = 0;
...@@ -206,129 +74,4 @@ GLenum VertexDataManager::prepareVertexData(GLint start, GLsizei count, Translat ...@@ -206,129 +74,4 @@ GLenum VertexDataManager::prepareVertexData(GLint start, GLsizei count, Translat
return GL_NO_ERROR; return GL_NO_ERROR;
} }
VertexBuffer::VertexBuffer(unsigned int size) : mVertexBuffer(NULL)
{
if(size > 0)
{
mVertexBuffer = new sw::Resource(size + 1024);
if(!mVertexBuffer)
{
ERR("Out of memory allocating a vertex buffer of size %lu.", size);
}
}
}
VertexBuffer::~VertexBuffer()
{
if(mVertexBuffer)
{
mVertexBuffer->destruct();
}
}
void VertexBuffer::unmap()
{
if(mVertexBuffer)
{
mVertexBuffer->unlock();
}
}
sw::Resource *VertexBuffer::getResource() const
{
return mVertexBuffer;
}
ConstantVertexBuffer::ConstantVertexBuffer(float x, float y, float z, float w) : VertexBuffer(4 * sizeof(float))
{
if(mVertexBuffer)
{
float *vector = (float*)mVertexBuffer->lock(sw::PUBLIC);
vector[0] = x;
vector[1] = y;
vector[2] = z;
vector[3] = w;
mVertexBuffer->unlock();
}
}
ConstantVertexBuffer::~ConstantVertexBuffer()
{
}
StreamingVertexBuffer::StreamingVertexBuffer(unsigned int size) : VertexBuffer(size)
{
mBufferSize = size;
mWritePosition = 0;
mRequiredSpace = 0;
}
StreamingVertexBuffer::~StreamingVertexBuffer()
{
}
void StreamingVertexBuffer::addRequiredSpace(unsigned int requiredSpace)
{
mRequiredSpace += requiredSpace;
}
void *StreamingVertexBuffer::map(const VertexAttribute &attribute, unsigned int requiredSpace, unsigned int *offset)
{
void *mapPtr = NULL;
if(mVertexBuffer)
{
mapPtr = (char*)mVertexBuffer->lock(sw::PUBLIC) + mWritePosition;
if(!mapPtr)
{
ERR("Lock failed");
return NULL;
}
*offset = mWritePosition;
mWritePosition += requiredSpace;
}
return mapPtr;
}
void StreamingVertexBuffer::reserveRequiredSpace()
{
if(mRequiredSpace > mBufferSize)
{
if(mVertexBuffer)
{
mVertexBuffer->destruct();
mVertexBuffer = 0;
}
mBufferSize = std::max(mRequiredSpace, 3 * mBufferSize / 2); // 1.5 x mBufferSize is arbitrary and should be checked to see we don't have too many reallocations.
mVertexBuffer = new sw::Resource(mBufferSize);
if(!mVertexBuffer)
{
ERR("Out of memory allocating a vertex buffer of size %lu.", mBufferSize);
}
mWritePosition = 0;
}
else if(mWritePosition + mRequiredSpace > mBufferSize) // Recycle
{
if(mVertexBuffer)
{
mVertexBuffer->destruct();
mVertexBuffer = new sw::Resource(mBufferSize);
}
mWritePosition = 0;
}
mRequiredSpace = 0;
}
} }
...@@ -36,62 +36,16 @@ struct TranslatedAttribute ...@@ -36,62 +36,16 @@ struct TranslatedAttribute
sw::Resource *vertexBuffer; sw::Resource *vertexBuffer;
}; };
class VertexBuffer
{
public:
VertexBuffer(unsigned int size);
virtual ~VertexBuffer();
void unmap();
sw::Resource *getResource() const;
protected:
sw::Resource *mVertexBuffer;
};
class ConstantVertexBuffer : public VertexBuffer
{
public:
ConstantVertexBuffer(float x, float y, float z, float w);
~ConstantVertexBuffer();
};
class StreamingVertexBuffer : public VertexBuffer
{
public:
StreamingVertexBuffer(unsigned int size);
~StreamingVertexBuffer();
void *map(const VertexAttribute &attribute, unsigned int requiredSpace, unsigned int *streamOffset);
void reserveRequiredSpace();
void addRequiredSpace(unsigned int requiredSpace);
protected:
unsigned int mBufferSize;
unsigned int mWritePosition;
unsigned int mRequiredSpace;
};
class VertexDataManager class VertexDataManager
{ {
public: public:
VertexDataManager(Context *context); VertexDataManager(Context *context);
virtual ~VertexDataManager(); virtual ~VertexDataManager();
void dirtyCurrentValue(int index) { mDirtyCurrentValue[index] = true; }
GLenum prepareVertexData(GLint start, GLsizei count, TranslatedAttribute *outAttribs); GLenum prepareVertexData(GLint start, GLsizei count, TranslatedAttribute *outAttribs);
private: private:
unsigned int writeAttributeData(StreamingVertexBuffer *vertexBuffer, GLint start, GLsizei count, const VertexAttribute &attribute);
Context *const mContext; Context *const mContext;
StreamingVertexBuffer *mStreamingBuffer;
bool mDirtyCurrentValue[MAX_VERTEX_ATTRIBS];
ConstantVertexBuffer *mCurrentValueBuffer[MAX_VERTEX_ATTRIBS];
}; };
} }
......
...@@ -156,17 +156,15 @@ public: ...@@ -156,17 +156,15 @@ public:
{ {
if(buffer) if(buffer)
{ {
buffer->release(); buffer->destruct();
} }
} }
void storage(RADsizei size) void storage(RADsizei size)
{ {
ASSERT(!buffer); ASSERT(!buffer);
buffer = new es2::Buffer(0); const int padding = 1024; // For SIMD processing of vertices
buffer->addRef(); buffer = new sw::Resource(size + padding);
buffer->mContents = new sw::Resource(size);
buffer->mSize = size;
} }
void *map() void *map()
...@@ -175,7 +173,7 @@ public: ...@@ -175,7 +173,7 @@ public:
return const_cast<void*>(buffer->data()); return const_cast<void*>(buffer->data());
} }
es2::Buffer *buffer; sw::Resource *buffer;
RADbitfield access; RADbitfield access;
RADbitfield mapAccess; RADbitfield mapAccess;
...@@ -1005,7 +1003,7 @@ public: ...@@ -1005,7 +1003,7 @@ public:
class DrawElements : public Command class DrawElements : public Command
{ {
public: public:
DrawElements(RADprimitiveType mode, RADindexType type, RADsizei count, es2::Buffer *indexBuffer, RADuint offset) DrawElements(RADprimitiveType mode, RADindexType type, RADsizei count, sw::Resource *indexBuffer, RADuint offset)
{ {
this->mode = mode; this->mode = mode;
this->type = type; this->type = type;
...@@ -1060,21 +1058,21 @@ public: ...@@ -1060,21 +1058,21 @@ public:
default: UNREACHABLE(); default: UNREACHABLE();
} }
context->mState.elementArrayBuffer.set(indexBuffer); context->mState.elementArrayBuffer = indexBuffer;
context->drawElements(glMode, count, glType, 0); context->drawElements(glMode, count, glType, 0);
} }
RADprimitiveType mode; RADprimitiveType mode;
RADindexType type; RADindexType type;
RADsizei count; RADsizei count;
es2::Buffer *indexBuffer; sw::Resource *indexBuffer;
RADuint offset; RADuint offset;
}; };
class BindGroup : public Command class BindGroup : public Command
{ {
public: public:
BindGroup(RADbitfield stages, RADuint group, RADuint count, es2::Buffer *buffer, RADuint offset) BindGroup(RADbitfield stages, RADuint group, RADuint count, sw::Resource *buffer, RADuint offset)
{ {
this->stages = stages; this->stages = stages;
this->group = group; this->group = group;
...@@ -1096,19 +1094,19 @@ public: ...@@ -1096,19 +1094,19 @@ public:
// FIXME: Should parse the layout out of the shaders // FIXME: Should parse the layout out of the shaders
es2::Program *program = pipeline->vertexProgram->program; es2::Program *program = pipeline->vertexProgram->program;
es2::Buffer *element0 = reinterpret_cast<es2::Buffer*>(groupElements[0].handle); sw::Resource *element0 = reinterpret_cast<sw::Resource*>(groupElements[0].handle);
void *offset0 = reinterpret_cast<void*>(static_cast<uintptr_t>(groupElements[0].offset)); uintptr_t offset0 = static_cast<uintptr_t>(groupElements[0].offset);
int position = program->getAttributeLocation("position"); int position = program->getAttributeLocation("position");
context->setVertexAttribState(position, element0, 3, GL_FLOAT, GL_TRUE, 0, offset0); context->setVertexAttribState(position, element0, 3, GL_FLOAT, GL_TRUE, 0, offset0);
context->setEnableVertexAttribArray(position, true); context->setEnableVertexAttribArray(position, true);
es2::Buffer *element1 = reinterpret_cast<es2::Buffer*>(groupElements[1].handle); sw::Resource *element1 = reinterpret_cast<sw::Resource*>(groupElements[1].handle);
void *offset1 = reinterpret_cast<void*>(static_cast<uintptr_t>(groupElements[1].offset)); uintptr_t offset1 = static_cast<uintptr_t>(groupElements[1].offset);
int tc = program->getAttributeLocation("tc"); int tc = program->getAttributeLocation("tc");
context->setVertexAttribState(tc, element1, 4, GL_UNSIGNED_BYTE, GL_TRUE, 0, offset1); context->setVertexAttribState(tc, element1, 4, GL_UNSIGNED_BYTE, GL_TRUE, 0, offset1);
context->setEnableVertexAttribArray(tc, true); context->setEnableVertexAttribArray(tc, true);
es2::Buffer *element2 = reinterpret_cast<es2::Buffer*>(groupElements[2].handle); sw::Resource *element2 = reinterpret_cast<sw::Resource*>(groupElements[2].handle);
const void *uniform = static_cast<const uint8_t*>(element2->data()) + groupElements[2].offset; const void *uniform = static_cast<const uint8_t*>(element2->data()) + groupElements[2].offset;
int scale = program->getUniformLocation("scale"); int scale = program->getUniformLocation("scale");
program->setUniform4fv(scale, 1, (const GLfloat*)uniform); program->setUniform4fv(scale, 1, (const GLfloat*)uniform);
...@@ -1123,7 +1121,7 @@ public: ...@@ -1123,7 +1121,7 @@ public:
RADbitfield stages; RADbitfield stages;
RADuint group; RADuint group;
RADuint count; RADuint count;
es2::Buffer *buffer; sw::Resource *buffer;
RADuint offset; RADuint offset;
}; };
...@@ -1319,7 +1317,7 @@ void RADAPIENTRY radQueueDrawArrays(RADqueue queue, RADprimitiveType mode, RADin ...@@ -1319,7 +1317,7 @@ void RADAPIENTRY radQueueDrawArrays(RADqueue queue, RADprimitiveType mode, RADin
void RADAPIENTRY radQueueDrawElements(RADqueue queue, RADprimitiveType mode, RADindexType type, RADsizei count, RADindexHandle indexHandle, RADuint offset) void RADAPIENTRY radQueueDrawElements(RADqueue queue, RADprimitiveType mode, RADindexType type, RADsizei count, RADindexHandle indexHandle, RADuint offset)
{ {
rad::Queue *radQueue = reinterpret_cast<rad::Queue*>(queue); rad::Queue *radQueue = reinterpret_cast<rad::Queue*>(queue);
es2::Buffer *indexBuffer = reinterpret_cast<es2::Buffer*>(indexHandle); sw::Resource *indexBuffer = reinterpret_cast<sw::Resource*>(indexHandle);
rad::DrawElements *command = new rad::DrawElements(mode, type, count, indexBuffer, offset); rad::DrawElements *command = new rad::DrawElements(mode, type, count, indexBuffer, offset);
radQueue->submit(command); radQueue->submit(command);
} }
...@@ -1334,7 +1332,7 @@ void RADAPIENTRY radQueueBindPipeline(RADqueue queue, RADpipelineType pipelineTy ...@@ -1334,7 +1332,7 @@ void RADAPIENTRY radQueueBindPipeline(RADqueue queue, RADpipelineType pipelineTy
void RADAPIENTRY radQueueBindGroup(RADqueue queue, RADbitfield stages, RADuint group, RADuint count, RADbindGroupHandle groupHandle, RADuint offset) void RADAPIENTRY radQueueBindGroup(RADqueue queue, RADbitfield stages, RADuint group, RADuint count, RADbindGroupHandle groupHandle, RADuint offset)
{ {
rad::Queue *radQueue = reinterpret_cast<rad::Queue*>(queue); rad::Queue *radQueue = reinterpret_cast<rad::Queue*>(queue);
es2::Buffer *groupBuffer = reinterpret_cast<es2::Buffer*>(groupHandle); sw::Resource *groupBuffer = reinterpret_cast<sw::Resource*>(groupHandle);
rad::BindGroup *command = new rad::BindGroup(stages, group, count, groupBuffer, offset); rad::BindGroup *command = new rad::BindGroup(stages, group, count, groupBuffer, offset);
radQueue->submit(command); radQueue->submit(command);
} }
......
...@@ -196,7 +196,6 @@ copy "$(OutDir)libRAD.dll" "$(ProjectDir)..\..\..\lib\$(Configuration)\"</Comman ...@@ -196,7 +196,6 @@ copy "$(OutDir)libRAD.dll" "$(ProjectDir)..\..\..\lib\$(Configuration)\"</Comman
<ClInclude Include="IndexDataManager.h" /> <ClInclude Include="IndexDataManager.h" />
<ClInclude Include="main.h" /> <ClInclude Include="main.h" />
<ClInclude Include="mathutil.h" /> <ClInclude Include="mathutil.h" />
<ClInclude Include="Object.hpp" />
<ClInclude Include="Program.h" /> <ClInclude Include="Program.h" />
<ClInclude Include="Query.h" /> <ClInclude Include="Query.h" />
<ClInclude Include="RefCountObject.h" /> <ClInclude Include="RefCountObject.h" />
......
...@@ -145,9 +145,6 @@ ...@@ -145,9 +145,6 @@
<ClInclude Include="..\common\debug.h"> <ClInclude Include="..\common\debug.h">
<Filter>Header Files</Filter> <Filter>Header Files</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="Object.hpp">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ResourceCompile Include="libRAD.rc" /> <ResourceCompile Include="libRAD.rc" />
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment