// constructor Untyped::Untyped(unsigned int aId, size_t aStride, size_t aBits) : mId(aId), mBits(aBits), mLimit(1 << mBits), mCount(0), mMask((2 << mBits) - 1) { // if allocating... if (signed(aBits) >= 0) { // set up the memory pool mPool = new MemoryPoolRef(aStride, 0, std::max<size_t>(16, 1024 / aStride)); // allocate memory Alloc(); // fill with empty values memset(mMap, EMPTY, mLimit * 2 * sizeof(size_t)); memset(mKey, 0, mLimit * sizeof(Key)); memset(mData, 0, mLimit * sizeof(void *)); memset(mNil, 0, GetStride()); } else { // no pool (yet) mPool = NULL; // clear pointers mMap = NULL; mKey = NULL; mData = NULL; mNil = NULL; } if (mId) GetDatabases().Put(mId, this); }
// allocate pools void Untyped::Alloc() { mMap = static_cast<size_t *>(malloc(mLimit * 2 * sizeof(size_t))); mKey = static_cast<Key *>(malloc(mLimit * sizeof(Key))); mData = static_cast<void **>(malloc(mLimit * sizeof(void *))); mNil = malloc(GetStride()); }
void cgl::CD3D11IndexBuffer::Bind(UINT offset) { switch(GetStride()) { case sizeof(DWORD): getDevice()->GetContext()->IASetIndexBuffer(get(), DXGI_FORMAT_R32_UINT, offset); break; case sizeof(WORD): getDevice()->GetContext()->IASetIndexBuffer(get(), DXGI_FORMAT_R16_UINT, offset); break; } }
sm::Vec3 VertexInformation::GetPosition(const void *vertices, uint32_t index, uint8_t vertexType) { vertices = reinterpret_cast<const uint8_t*>(vertices) + (GetStride(vertexType) * index) + GetOffset(vertexType, VertexAttrib::Position); return *reinterpret_cast<const sm::Vec3*>(vertices); }
sm::Vec2 VertexInformation::GetTexcoord0(const void *vertices, uint32_t index, uint8_t vertexType) { vertices = reinterpret_cast<const uint8_t*>(vertices)+ (GetStride(vertexType) * index) + GetOffset(vertexType, VertexAttrib::Coords1); return *reinterpret_cast<const sm::Vec2*>(vertices); }
// grow the database // (doubles the size) void Untyped::Grow(void) { // resize ++mBits; mMask = (2 << mBits) - 1; mLimit = 1 << mBits; DebugPrint("Grow database this=%p id=%08x stride=%d chunk=%d limit=%d count=%d\n", this, mId, GetStride(), GetChunk(), GetLimit(), GetCount()); // reallocate map free(mMap); mMap = static_cast<size_t *>(malloc(mLimit * 2 * sizeof(size_t))); memset(mMap, EMPTY, mLimit * 2 * sizeof(size_t)); // reallocate keys mKey = static_cast<Key *>(realloc(mKey, mLimit * sizeof(size_t))); memset(mKey + mCount, 0, (mLimit - mCount) * sizeof(size_t)); // reallocate data mData = static_cast<void **>(realloc(mData, mLimit * sizeof(void *))); memset(mData + mCount, 0, (mLimit - mCount) * sizeof(void *)); // rebuild hash for (size_t record = 0; record < mCount; ++record) { // get the record key size_t key = mKey[record]; // convert key to a hash map index // (HACK: assume key is already a hash) size_t index = FindIndex(key); // insert the record key mMap[index] = record; } }
void cgl::CD3D11VertexBuffer::Bind(UINT slot, UINT offset) { UINT stride = GetStride(); getDevice()->GetContext()->IASetVertexBuffers(slot, 1, ptr(), &stride, &offset ); }
uint64_t IAudioStream::GetNumSamples() { return GetLength() / (uint64_t)GetStride(); }
u32 CNativeTexture::GetBytesRequired() const { return GetStride() * mCorrectedHeight; }
void CNativeTexture::SetData( void * data, void * palette ) { // It's pretty gross that we don't pass this in, or better yet, provide a way for // the caller to write directly to our buffers instead of setting the data. size_t data_len = GetBytesRequired(); memcpy(mpData, data, data_len); if (mTextureFormat == TexFmt_CI4_8888) { memcpy(mpPalette, palette, kPalette4BytesRequired); } else if (mTextureFormat == TexFmt_CI8_8888) { memcpy(mpPalette, palette, kPalette8BytesRequired); } if (HasData()) { glBindTexture( GL_TEXTURE_2D, mTextureId ); glPixelStorei( GL_UNPACK_ALIGNMENT, 1 ); switch (mTextureFormat) { case TexFmt_5650: glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, mCorrectedWidth, mCorrectedHeight, 0, GL_RGB, GL_UNSIGNED_SHORT_5_6_5_REV, data ); break; case TexFmt_5551: glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, mCorrectedWidth, mCorrectedHeight, 0, GL_RGBA, GL_UNSIGNED_SHORT_1_5_5_5_REV, data ); break; case TexFmt_4444: glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, mCorrectedWidth, mCorrectedHeight, 0, GL_RGBA, GL_UNSIGNED_SHORT_4_4_4_4_REV, data ); break; case TexFmt_8888: glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, mCorrectedWidth, mCorrectedHeight, 0, GL_RGBA, GL_UNSIGNED_INT_8_8_8_8_REV, data ); break; case TexFmt_CI4_8888: { // Convert palletised texture to non-palletised. This is wsteful - we should avoid generating these updated for OSX. const NativePfCI44 * pix_ptr = static_cast< const NativePfCI44 * >( data ); const NativePf8888 * pal_ptr = static_cast< const NativePf8888 * >( palette ); NativePf8888 * out = static_cast<NativePf8888 *>( malloc(mCorrectedWidth * mCorrectedHeight * sizeof(NativePf8888)) ); NativePf8888 * out_ptr = out; u32 pitch = GetStride(); for (u32 y = 0; y < mCorrectedHeight; ++y) { for (u32 x = 0; x < mCorrectedWidth; ++x) { NativePfCI44 colors = pix_ptr[ x / 2 ]; u8 pal_idx = (x&1) ? colors.GetIdxA() : colors.GetIdxB(); *out_ptr = pal_ptr[ pal_idx ]; out_ptr++; } pix_ptr = reinterpret_cast<const NativePfCI44 *>( reinterpret_cast<const u8 *>(pix_ptr) + pitch ); } glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, mCorrectedWidth, mCorrectedHeight, 0, GL_RGBA, GL_UNSIGNED_INT_8_8_8_8_REV, out ); free(out); } break; case TexFmt_CI8_8888: { // Convert palletised texture to non-palletised. This is wsteful - we should avoid generating these updated for OSX. const NativePfCI8 * pix_ptr = static_cast< const NativePfCI8 * >( data ); const NativePf8888 * pal_ptr = static_cast< const NativePf8888 * >( palette ); NativePf8888 * out = static_cast<NativePf8888 *>( malloc(mCorrectedWidth * mCorrectedHeight * sizeof(NativePf8888)) ); NativePf8888 * out_ptr = out; u32 pitch = GetStride(); for (u32 y = 0; y < mCorrectedHeight; ++y) { for (u32 x = 0; x < mCorrectedWidth; ++x) { u8 pal_idx = pix_ptr[ x ].Bits; *out_ptr = pal_ptr[ pal_idx ]; out_ptr++; } pix_ptr = reinterpret_cast<const NativePfCI8 *>( reinterpret_cast<const u8 *>(pix_ptr) + pitch ); } glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, mCorrectedWidth, mCorrectedHeight, 0, GL_RGBA, GL_UNSIGNED_INT_8_8_8_8_REV, out ); free(out); } break; default: DAEDALUS_ASSERT( !IsTextureFormatPalettised( mTextureFormat ), "Unhandled palette texture" ); DAEDALUS_ASSERT( palette == NULL, "Palette provided when not needed" ); break; } } }
void cgl::util::CGLInputBuffer::onParamUpdate() { SetParam<UINT>(BIND_PARAM_STRIDE_UINT, GetStride()); }
void OpenGLMesh::Load ( const MeshBuffer& aMeshBuffer ) { mAABB = AABB { { aMeshBuffer.center().x(), aMeshBuffer.center().y(), aMeshBuffer.center().z() }, { aMeshBuffer.radii().x(), aMeshBuffer.radii().y(), aMeshBuffer.radii().z() } }; mVertexCount = aMeshBuffer.vertexcount(); mIndexCount = aMeshBuffer.indexcount(); mIndexSize = aMeshBuffer.indexsize(); mVertexFlags = aMeshBuffer.vertexflags(); // OpenGL Specific Code: ///@todo Use OpenGLBuffer class instead of raw GL ids #ifndef SINGLE_VAO glGenVertexArrays ( 1, &mArray ); OPENGL_CHECK_ERROR_THROW; glBindVertexArray ( mArray ); OPENGL_CHECK_ERROR_THROW; #endif glGenBuffers ( 1, &mVertexBuffer ); OPENGL_CHECK_ERROR_THROW; glBindBuffer ( GL_ARRAY_BUFFER, mVertexBuffer ); OPENGL_CHECK_ERROR_THROW; glBufferData ( GL_ARRAY_BUFFER, aMeshBuffer.vertexbuffer().size(), aMeshBuffer.vertexbuffer().data(), GL_STATIC_DRAW ); OPENGL_CHECK_ERROR_THROW; size_t offset{0}; if ( mVertexFlags & Mesh::POSITION_BIT ) { glEnableVertexAttribArray ( 0 ); OPENGL_CHECK_ERROR_THROW; glVertexAttribPointer ( 0, 3, GL_FLOAT, GL_FALSE, GetStride ( mVertexFlags ), reinterpret_cast<const void*> ( offset ) ); OPENGL_CHECK_ERROR_THROW; offset += sizeof ( float ) * 3; } if ( mVertexFlags & Mesh::NORMAL_BIT ) { glEnableVertexAttribArray ( 1 ); OPENGL_CHECK_ERROR_THROW; glVertexAttribPointer ( 1, 3, GL_FLOAT, GL_FALSE, GetStride ( mVertexFlags ), reinterpret_cast<const void*> ( offset ) ); OPENGL_CHECK_ERROR_THROW; offset += sizeof ( float ) * 3; } if ( mVertexFlags & Mesh::TANGENT_BIT ) { glEnableVertexAttribArray ( 2 ); OPENGL_CHECK_ERROR_THROW; glVertexAttribPointer ( 2, 3, GL_FLOAT, GL_FALSE, GetStride ( mVertexFlags ), reinterpret_cast<const void*> ( offset ) ); OPENGL_CHECK_ERROR_THROW; offset += sizeof ( float ) * 3; } if ( mVertexFlags & Mesh::BITANGENT_BIT ) { glEnableVertexAttribArray ( 3 ); OPENGL_CHECK_ERROR_THROW; glVertexAttribPointer ( 3, 3, GL_FLOAT, GL_FALSE, GetStride ( mVertexFlags ), reinterpret_cast<const void*> ( offset ) ); OPENGL_CHECK_ERROR_THROW; offset += sizeof ( float ) * 3; } if ( mVertexFlags & Mesh::UV_BIT ) { glEnableVertexAttribArray ( 4 ); OPENGL_CHECK_ERROR_THROW; glVertexAttribPointer ( 4, 2, GL_FLOAT, GL_FALSE, GetStride ( mVertexFlags ), reinterpret_cast<const void*> ( offset ) ); OPENGL_CHECK_ERROR_THROW; offset += sizeof ( float ) * 2; } if ( mVertexFlags & Mesh::WEIGHT_IDX_BIT ) { glEnableVertexAttribArray ( 5 ); OPENGL_CHECK_ERROR_THROW; glVertexAttribIPointer ( 5, 4, GL_UNSIGNED_BYTE, GetStride ( mVertexFlags ), reinterpret_cast<const void*> ( offset ) ); OPENGL_CHECK_ERROR_THROW; offset += sizeof ( uint8_t ) * 4; } if ( mVertexFlags & Mesh::WEIGHT_BIT ) { glEnableVertexAttribArray ( 6 ); OPENGL_CHECK_ERROR_THROW; glVertexAttribPointer ( 6, 4, GL_UNSIGNED_BYTE, GL_TRUE, GetStride ( mVertexFlags ), reinterpret_cast<const void*> ( offset ) ); OPENGL_CHECK_ERROR_THROW; offset += sizeof ( uint8_t ) * 4; } if ( mVertexFlags & Mesh::COLOR_BIT ) { glEnableVertexAttribArray ( 7 ); OPENGL_CHECK_ERROR_THROW; glVertexAttribPointer ( 7, 3, GL_FLOAT, GL_FALSE, GetStride ( mVertexFlags ), reinterpret_cast<const void*> ( offset ) ); OPENGL_CHECK_ERROR_THROW; offset += sizeof ( float ) * 3; } //---Index Buffer--- if ( mIndexCount ) { glGenBuffers ( 1, &mIndexBuffer ); OPENGL_CHECK_ERROR_THROW; glBindBuffer ( GL_ELEMENT_ARRAY_BUFFER, mIndexBuffer ); OPENGL_CHECK_ERROR_THROW; glBufferData ( GL_ELEMENT_ARRAY_BUFFER, aMeshBuffer.indexbuffer().size(), aMeshBuffer.indexbuffer().data(), GL_STATIC_DRAW ); OPENGL_CHECK_ERROR_THROW; } }