Updates to support instanced rendering (OpenGL only)

This commit is contained in:
Jack Andersen 2015-11-24 16:14:30 -10:00
parent 8682485dea
commit 3bcfa99b5a
5 changed files with 54 additions and 11 deletions

View File

@ -48,7 +48,7 @@ public:
IShaderDataBinding* IShaderDataBinding*
newShaderDataBinding(IShaderPipeline* pipeline, newShaderDataBinding(IShaderPipeline* pipeline,
IVertexFormat* vtxFormat, IVertexFormat* vtxFormat,
IGraphicsBuffer* vbo, IGraphicsBuffer* ibo, IGraphicsBuffer* vbo, IGraphicsBuffer* instVbo, IGraphicsBuffer* ibo,
size_t ubufCount, IGraphicsBuffer** ubufs, size_t ubufCount, IGraphicsBuffer** ubufs,
size_t texCount, ITexture** texs); size_t texCount, ITexture** texs);

View File

@ -24,6 +24,12 @@ struct IGraphicsCommandQueue
virtual void setRenderTarget(ITextureR* target)=0; virtual void setRenderTarget(ITextureR* target)=0;
virtual void setViewport(const SWindowRect& rect)=0; virtual void setViewport(const SWindowRect& rect)=0;
/**
* @brief Which dynamic buffer slot is being populated for pending command list
* @return Index [0,2] indicating the buffer slot
*/
virtual int pendingDynamicSlot()=0;
virtual void resizeRenderTexture(ITextureR* tex, size_t width, size_t height)=0; virtual void resizeRenderTexture(ITextureR* tex, size_t width, size_t height)=0;
virtual void flushBufferUpdates()=0; virtual void flushBufferUpdates()=0;

View File

@ -100,12 +100,18 @@ struct IVertexFormat {};
/** Types of vertex attributes */ /** Types of vertex attributes */
enum class VertexSemantic enum class VertexSemantic
{ {
None = 0,
Position, Position,
Normal, Normal,
Color, Color,
UV, UV,
Weight UV4,
Weight,
ModelView,
SemanticMask = 0xf,
Instanced = 0x10
}; };
ENABLE_BITWISE_ENUM(VertexSemantic)
/** Used to create IVertexFormat */ /** Used to create IVertexFormat */
struct VertexElementDescriptor struct VertexElementDescriptor
@ -147,7 +153,9 @@ enum class BlendFactor
SrcAlpha, SrcAlpha,
InvSrcAlpha, InvSrcAlpha,
DstAlpha, DstAlpha,
InvDstAlpha InvDstAlpha,
SrcColor1,
InvSrcColor1
}; };
/** Factory object for creating batches of resources as an IGraphicsData token */ /** Factory object for creating batches of resources as an IGraphicsData token */
@ -196,7 +204,7 @@ struct IGraphicsDataFactory
virtual IShaderDataBinding* virtual IShaderDataBinding*
newShaderDataBinding(IShaderPipeline* pipeline, newShaderDataBinding(IShaderPipeline* pipeline,
IVertexFormat* vtxFormat, IVertexFormat* vtxFormat,
IGraphicsBuffer* vbo, IGraphicsBuffer* ibo, IGraphicsBuffer* vbo, IGraphicsBuffer* instVbo, IGraphicsBuffer* ibo,
size_t ubufCount, IGraphicsBuffer** ubufs, size_t ubufCount, IGraphicsBuffer** ubufs,
size_t texCount, ITexture** texs)=0; size_t texCount, ITexture** texs)=0;

View File

@ -346,7 +346,9 @@ static const GLenum BLEND_FACTOR_TABLE[] =
GL_SRC_ALPHA, GL_SRC_ALPHA,
GL_ONE_MINUS_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA,
GL_DST_ALPHA, GL_DST_ALPHA,
GL_ONE_MINUS_DST_ALPHA GL_ONE_MINUS_DST_ALPHA,
GL_SRC1_COLOR,
GL_ONE_MINUS_SRC1_COLOR
}; };
IShaderPipeline* GLDataFactory::newShaderPipeline IShaderPipeline* GLDataFactory::newShaderPipeline
@ -503,7 +505,7 @@ struct GLShaderDataBinding : IShaderDataBinding
IShaderDataBinding* IShaderDataBinding*
GLDataFactory::newShaderDataBinding(IShaderPipeline* pipeline, GLDataFactory::newShaderDataBinding(IShaderPipeline* pipeline,
IVertexFormat* vtxFormat, IVertexFormat* vtxFormat,
IGraphicsBuffer*, IGraphicsBuffer*, IGraphicsBuffer*, IGraphicsBuffer*, IGraphicsBuffer*,
size_t ubufCount, IGraphicsBuffer** ubufs, size_t ubufCount, IGraphicsBuffer** ubufs,
size_t texCount, ITexture** texs) size_t texCount, ITexture** texs)
{ {
@ -550,28 +552,34 @@ void GLDataFactory::destroyAllData()
static const GLint SEMANTIC_COUNT_TABLE[] = static const GLint SEMANTIC_COUNT_TABLE[] =
{ {
0,
3, 3,
3, 3,
4, 4,
2, 2,
4,
4 4
}; };
static const size_t SEMANTIC_SIZE_TABLE[] = static const size_t SEMANTIC_SIZE_TABLE[] =
{ {
0,
12, 12,
12, 12,
4, 4,
8, 8,
16,
16 16
}; };
static const GLenum SEMANTIC_TYPE_TABLE[] = static const GLenum SEMANTIC_TYPE_TABLE[] =
{ {
GL_INVALID_ENUM,
GL_FLOAT, GL_FLOAT,
GL_FLOAT, GL_FLOAT,
GL_UNSIGNED_BYTE, GL_UNSIGNED_BYTE,
GL_FLOAT, GL_FLOAT,
GL_FLOAT,
GL_FLOAT GL_FLOAT
}; };
@ -647,13 +655,18 @@ struct GLCommandQueue : IGraphicsCommandQueue
glGenVertexArrays(1, &fmt->m_vao); glGenVertexArrays(1, &fmt->m_vao);
size_t stride = 0; size_t stride = 0;
size_t instStride = 0;
for (size_t i=0 ; i<fmt->m_elementCount ; ++i) for (size_t i=0 ; i<fmt->m_elementCount ; ++i)
{ {
const VertexElementDescriptor* desc = &fmt->m_elements[i]; const VertexElementDescriptor* desc = &fmt->m_elements[i];
stride += SEMANTIC_SIZE_TABLE[int(desc->semantic)]; if ((desc->semantic & VertexSemantic::Instanced) != VertexSemantic::None)
instStride += SEMANTIC_SIZE_TABLE[int(desc->semantic & VertexSemantic::SemanticMask)];
else
stride += SEMANTIC_SIZE_TABLE[int(desc->semantic & VertexSemantic::SemanticMask)];
} }
size_t offset = 0; size_t offset = 0;
size_t instOffset = 0;
glBindVertexArray(fmt->m_vao); glBindVertexArray(fmt->m_vao);
const IGraphicsBuffer* lastVBO = nullptr; const IGraphicsBuffer* lastVBO = nullptr;
const IGraphicsBuffer* lastEBO = nullptr; const IGraphicsBuffer* lastEBO = nullptr;
@ -677,9 +690,20 @@ struct GLCommandQueue : IGraphicsCommandQueue
static_cast<const GLGraphicsBufferS*>(lastEBO)->bindIndex(); static_cast<const GLGraphicsBufferS*>(lastEBO)->bindIndex();
} }
glEnableVertexAttribArray(i); glEnableVertexAttribArray(i);
glVertexAttribPointer(i, SEMANTIC_COUNT_TABLE[int(desc->semantic)], int maskedSem = int(desc->semantic & VertexSemantic::SemanticMask);
SEMANTIC_TYPE_TABLE[int(desc->semantic)], GL_TRUE, stride, (void*)offset); if ((desc->semantic & VertexSemantic::Instanced) != VertexSemantic::None)
offset += SEMANTIC_SIZE_TABLE[int(desc->semantic)]; {
glVertexAttribPointer(i, SEMANTIC_COUNT_TABLE[maskedSem],
SEMANTIC_TYPE_TABLE[maskedSem], GL_TRUE, instStride, (void*)instOffset);
glVertexAttribDivisor(i, 1);
instOffset += SEMANTIC_SIZE_TABLE[maskedSem];
}
else
{
glVertexAttribPointer(i, SEMANTIC_COUNT_TABLE[maskedSem],
SEMANTIC_TYPE_TABLE[maskedSem], GL_TRUE, stride, (void*)offset);
offset += SEMANTIC_SIZE_TABLE[maskedSem];
}
} }
} }
@ -845,6 +869,11 @@ struct GLCommandQueue : IGraphicsCommandQueue
cmds.back().rect = rect; cmds.back().rect = rect;
} }
int pendingDynamicSlot()
{
return m_fillBuf;
}
void resizeRenderTexture(ITextureR* tex, size_t width, size_t height) void resizeRenderTexture(ITextureR* tex, size_t width, size_t height)
{ {
std::unique_lock<std::mutex> lk(m_mt); std::unique_lock<std::mutex> lk(m_mt);

View File

@ -374,7 +374,7 @@ struct TestApplicationCallback : IApplicationCallback
/* Make shader data binding */ /* Make shader data binding */
self->m_binding = self->m_binding =
factory->newShaderDataBinding(pipeline, vfmt, vbo, nullptr, 0, nullptr, 1, &texture); factory->newShaderDataBinding(pipeline, vfmt, vbo, nullptr, nullptr, 0, nullptr, 1, &texture);
/* Commit objects */ /* Commit objects */
IGraphicsData* data = factory->commit(); IGraphicsData* data = factory->commit();