diff options
| author | Alex Sakhartchouk <alexst@google.com> | 2010-11-08 15:10:52 -0800 |
|---|---|---|
| committer | Alex Sakhartchouk <alexst@google.com> | 2010-11-08 15:10:52 -0800 |
| commit | 54929cce0bf44090424b1f91b676529a2422378f (patch) | |
| tree | 7c1c8c96a962513360b435263384c0d50f60abbe /rsMesh.cpp | |
| parent | a77567380b00740001cc3652417e7b70c4ec9fb2 (diff) | |
| download | android_frameworks_rs-54929cce0bf44090424b1f91b676529a2422378f.tar.gz android_frameworks_rs-54929cce0bf44090424b1f91b676529a2422378f.tar.bz2 android_frameworks_rs-54929cce0bf44090424b1f91b676529a2422378f.zip | |
Moving attrib creation to Mesh. Adding arrays as shader inputs.
Removing fixed size arrays.
Change-Id: I0213e403a2f1283dd43f21bea770aeb059561903
Diffstat (limited to 'rsMesh.cpp')
| -rw-r--r-- | rsMesh.cpp | 112 |
1 files changed, 106 insertions, 6 deletions
@@ -37,6 +37,10 @@ Mesh::Mesh(Context *rsc) : ObjectBase(rsc) mPrimitivesCount = 0; mVertexBuffers = NULL; mVertexBufferCount = 0; + mAttribs = NULL; + mAttribAllocationIndex = NULL; + + mAttribCount = 0; } Mesh::~Mesh() @@ -51,6 +55,87 @@ Mesh::~Mesh() } delete[] mPrimitives; } + + if(mAttribs) { + delete[] mAttribs; + delete[] mAttribAllocationIndex; + } +} + +bool Mesh::isValidGLComponent(const Element *elem, uint32_t fieldIdx) { + // Do not create attribs for padding + if(elem->getFieldName(fieldIdx)[0] == '#') { + return false; + } + + // Only GL_BYTE, GL_UNSIGNED_BYTE, GL_SHORT, GL_UNSIGNED_SHORT, GL_FIXED, GL_FLOAT are accepted. + // Filter rs types accordingly + RsDataType dt = elem->getField(fieldIdx)->getComponent().getType(); + if(dt != RS_TYPE_FLOAT_32 && dt != RS_TYPE_UNSIGNED_8 && + dt != RS_TYPE_UNSIGNED_16 && dt != RS_TYPE_SIGNED_8 && + dt != RS_TYPE_SIGNED_16) { + return false; + } + + // Now make sure they are not arrays + uint32_t arraySize = elem->getFieldArraySize(fieldIdx); + if(arraySize != 1) { + return false; + } + + return true; +} + +void Mesh::initVertexAttribs() { + // Count the number of gl attrs to initialize + mAttribCount = 0; + for (uint32_t ct=0; ct < mVertexBufferCount; ct++) { + const Element *elem = mVertexBuffers[ct]->getType()->getElement(); + for (uint32_t ct=0; ct < elem->getFieldCount(); ct++) { + if(isValidGLComponent(elem, ct)) { + mAttribCount ++; + } + } + } + + if(mAttribs) { + delete [] mAttribs; + delete [] mAttribAllocationIndex; + mAttribs = NULL; + mAttribAllocationIndex = NULL; + } + if(!mAttribCount) { + return; + } + + mAttribs = new VertexArray::Attrib[mAttribCount]; + mAttribAllocationIndex = new uint32_t[mAttribCount]; + + uint32_t userNum = 0; + for (uint32_t ct=0; ct < mVertexBufferCount; ct++) { + const Element *elem = mVertexBuffers[ct]->getType()->getElement(); + uint32_t stride = elem->getSizeBytes(); + for (uint32_t fieldI=0; fieldI < elem->getFieldCount(); fieldI++) { + const Component &c = elem->getField(fieldI)->getComponent(); + + if(!isValidGLComponent(elem, fieldI)) { + continue; + } + + mAttribs[userNum].size = c.getVectorSize(); + mAttribs[userNum].offset = elem->getFieldOffsetBytes(fieldI); + mAttribs[userNum].type = c.getGLType(); + mAttribs[userNum].normalized = c.getType() != RS_TYPE_FLOAT_32;//c.getIsNormalized(); + mAttribs[userNum].stride = stride; + String8 tmp(RS_SHADER_ATTR); + tmp.append(elem->getFieldName(fieldI)); + mAttribs[userNum].name.setTo(tmp.string()); + + // Remember which allocation this attribute came from + mAttribAllocationIndex[userNum] = ct; + userNum ++; + } + } } void Mesh::render(Context *rsc) const @@ -78,21 +163,29 @@ void Mesh::renderPrimitive(Context *rsc, uint32_t primIndex) const { void Mesh::renderPrimitiveRange(Context *rsc, uint32_t primIndex, uint32_t start, uint32_t len) const { - if (len < 1 || primIndex >= mPrimitivesCount) { + if (len < 1 || primIndex >= mPrimitivesCount || mAttribCount == 0) { + LOGE("Invalid mesh or parameters"); return; } rsc->checkError("Mesh::renderPrimitiveRange 1"); - VertexArray va; for (uint32_t ct=0; ct < mVertexBufferCount; ct++) { mVertexBuffers[ct]->uploadCheck(rsc); - if (mVertexBuffers[ct]->getIsBufferObject()) { - va.setActiveBuffer(mVertexBuffers[ct]->getBufferObjectID()); + } + // update attributes with either buffer information or data ptr based on their current state + for (uint32_t ct=0; ct < mAttribCount; ct++) { + uint32_t allocIndex = mAttribAllocationIndex[ct]; + Allocation *alloc = mVertexBuffers[allocIndex].get(); + if (alloc->getIsBufferObject()) { + mAttribs[ct].buffer = alloc->getBufferObjectID(); + mAttribs[ct].ptr = NULL; } else { - va.setActiveBuffer(mVertexBuffers[ct]->getPtr()); + mAttribs[ct].buffer = 0; + mAttribs[ct].ptr = (const uint8_t*)alloc->getPtr(); } - mVertexBuffers[ct]->getType()->enableGLVertexBuffer(&va); } + + VertexArray va(mAttribs, mAttribCount); va.setupGL2(rsc, &rsc->mStateVertexArray, &rsc->mShaderCache); rsc->checkError("Mesh::renderPrimitiveRange 2"); @@ -215,6 +308,7 @@ Mesh *Mesh::createFromStream(Context *rsc, IStream *stream) } mesh->updateGLPrimitives(); + mesh->initVertexAttribs(); mesh->uploadAll(rsc); return mesh; @@ -310,6 +404,12 @@ void rsi_MeshBindIndex(Context *rsc, RsMesh mv, RsAllocation va, uint32_t primTy sm->updateGLPrimitives(); } +void rsi_MeshInitVertexAttribs(Context *rsc, RsMesh mv) +{ + Mesh *sm = static_cast<Mesh *>(mv); + sm->initVertexAttribs(); +} + }} void rsaMeshGetVertexBufferCount(RsContext con, RsMesh mv, int32_t *numVtx) |
