void CLWrapper::copyTo( CLWrapper *target, int srcOffset, int dstOffset, int count ) { if( !onDevice ) { throw std::runtime_error("Must have called copyToDevice() or createOnDevice() before calling copyTo(CLWrapper*)"); } if( !target->onDevice ) { throw std::runtime_error("Must have called copyToDevice() or createOnDevice() on target before calling copyTo(target)"); } if( srcOffset + count > N ) { throw std::runtime_error("copyTo: not enough source elements, given offset " + easycl::toString(srcOffset) + " and count " + easycl::toString(count)); } if( dstOffset + count > target->N ) { throw std::runtime_error("copyTo: not enough destation elements, given offset " + easycl::toString(dstOffset) + " and count " + easycl::toString(count)); } if( getElementSize() != target->getElementSize() ) { throw std::runtime_error("copyTo: element size mismatch between source and target CLWrapper objects"); } // can assume that we have our data on the device now, because of if check // just now // we will also assume that destination CLWrapper* is valid cl_event event = NULL; cl_int err = clEnqueueCopyBuffer( *(cl->queue), devicearray, target->devicearray, srcOffset * getElementSize(), dstOffset * getElementSize(), count * getElementSize(), 0, NULL, &event ); if (err != CL_SUCCESS) { throw std::runtime_error("copyTo failed with " + easycl::toString( err ) ); } else { /* Wait for calculations to be finished. */ // err = clWaitForEvents(1, &event); } clReleaseEvent(event); target->markDeviceDirty(); }
UINT DataArray::operator[](size_t index) const { switch(getElementSize()) { case 1 : return ByteArray::operator[](index); case 2 : return *(unsigned short*)(ByteArray::getData() + index * sizeof(short)); case 4 : return *(unsigned int *)(ByteArray::getData() + index * sizeof(int)); default: return *(unsigned int *)(ByteArray::getData() + index * getElementSize()); } }
void DataArray::setElement(BYTE *e, UINT value) { switch(getElementSize()) { case 1 : *e = (BYTE)value; break; case 2 : *((unsigned short*)e) = (unsigned short)value; break; default: *((unsigned int*)e) = value; break; } }
bool CD3D11Texture3D::copyDataToAnotherTexture(ITexture* dest) { if (!dest) return false; if (dest->getType() != ETT_TEXTURE_3D) { GF_PRINT_CONSOLE_INFO("Texture's data cannot be copied between different types\n"); return false; } CD3D11Texture3D* pAnotherTexture = dynamic_cast<CD3D11Texture3D*>(dest); if (getElementSize() != pAnotherTexture->getElementSize()) { GF_PRINT_CONSOLE_INFO("Buffers with different element size couldn't copy data with each other.\n"); return false; } if (mTextureWidth > pAnotherTexture->getWidth() || mTextureHeight > pAnotherTexture->getHeight() || mTextureDepth > pAnotherTexture->getDepth()) { GF_PRINT_CONSOLE_INFO("Destination Buffer' size is smaller than Source Buffer.\n"); return false; } md3dDeviceContext->CopyResource(pAnotherTexture->md3dTexture, md3dTexture); return true; }
/** * Finalize at most the first 'count' elements of the allocation by applying * the finalizer to each element in turn, from last to first, thus ensuring * that elements of the array are destroyed in the opposite order to that in * which they were created. */ void Header::finalize(count_t count) { if (finalizer_t f = getFinalizer()) // Not yet finalized? { size_t n = getElementSize(); // ...size of element size_t c = std::min(getElementCount(),count); // ...el's to destroy byte_t* p = getPayload(); // ...head of payload byte_t* q = p + c * n; // ...tail of payload /* Clear the 'finalizer' flag to signal that the finalization has been taken care of. We do so *before* invoking 'f', in case 'f' goes and frees the entire allocation in which the header sits, as happens to the underlying pages of class ScopedArena, for example...*/ _flags &= ~finalizer; // ...taken care of /* Finalize each of the elements from the end of the array back toward its beginning, that is, in the opposite order to that in which they were first constructed...*/ for (size_t i=0; i!=c; ++i) // ...for each element { f(q -= n); // ....call finalizer } /* It is now no longer safe to access any of this object's members...*/ } }
void CC3VertexBoneWeights::setBoneWeights( GLfloat* weights, GLuint vtxIndex ) { GLfloat* vtxWeights = getBoneWeightsAt(vtxIndex); GLint numWts = getElementSize(); for (int i = 0; i < numWts; i++) vtxWeights[i] = weights[i]; }
void DataArray::init() { UINT i; reset(); BYTE *ep = (BYTE*)getData(); const size_t n = size(); const UINT elemSize = getElementSize(); UINT maxValue; switch(elemSize) { case 1 : maxValue = min((UINT)n, 0xff ); break; case 2 : maxValue = min((UINT)n, 0xffff); break; default: maxValue = (UINT)n; break; } switch(m_param.m_initMethod) { case IDC_RADIO_RANDOM: { switch(m_param.m_randomizationMethod) { case FIXED_SEED : m_param.m_random.setSeed(m_param.m_seed); break; case SAME_RANDOM_SEED : m_param.m_random.setSeed(m_param.m_randomSeed); break; case RANDOM_SEED : m_param.m_random.randomize(); break; } for(i = 0; i < n; i++, ep += elemSize) { setElement(ep, m_param.m_random.nextInt(maxValue)); } } break; case IDC_RADIO_SORTED: for(i = 0; i < n; i++, ep += elemSize) { setElement(ep, (UINT)((i*maxValue)/n)); } break; case IDC_RADIO_INVERSESORTED: for(i = 0; i < n; i++, ep += elemSize) { setElement(ep, (UINT)((n-i)*maxValue/n)); } break; case IDC_RADIO_SINUS: for(i = 0; i < n; i++, ep += elemSize) { setElement(ep, (unsigned int)(maxValue * (0.5*(1.0+sin(M_PI*2*i / (n-1)*m_param.m_periodCount))))); } break; case IDC_RADIO_FILEDATA: for(i = 0; i < n; i++, ep += elemSize) { setElement(ep, m_param.m_fileData[i]); } break; default: throwException(_T("DataArray::init:Unknown initmethod (=%d)"), m_param.m_initMethod); break; } }
void CLWrapper::createOnDevice() { if(onDevice) { throw std::runtime_error("createOnDevice(): already on device"); } // std::cout << "creating buffer on device of " << N << " elements" << std::endl; devicearray = clCreateBuffer(*(cl->context), CL_MEM_READ_WRITE, getElementSize() * N, 0, &error); cl->checkError(error); onDevice = true; deviceDirty = false; // std::cout << "... created ok" << std::endl; }
nuElementBuffer::nuElementBuffer(ELEMENT_TYPE type, ui32 element_num, nuGResource::RESOURCE_USAGE usage) : nuGResource(nuGResource::ELEMENT_BUFFER, usage), mpBuffer(nullptr), mSize(0), mCommitSize(0), mElementBufferID(0), mElementNum(element_num), mElementType(type) { mSize = getElementSize(mElementType) * mElementNum; }
void VertexDecl::bindArray() { bool haveTex = false; for ( InfoVec::iterator iter = mInfoVec.begin() , itEnd = mInfoVec.end(); iter != itEnd; ++iter ) { Info& info = *iter; switch( info.semantic ) { case Vertex::ePosition: glEnableClientState( GL_VERTEX_ARRAY ); glVertexPointer( getElementSize( info.format ) , getFormatType( info.format ) , mVertexSize , (void*)info.offset ); break; case Vertex::eNormal: assert( getElementSize( info.format ) == 3 ); glEnableClientState( GL_NORMAL_ARRAY ); glNormalPointer( getFormatType( info.format ) , mVertexSize , (void*)info.offset ); break; case Vertex::eColor: if ( info.idx == 0 ) { glEnableClientState( GL_COLOR_ARRAY ); glColorPointer( getElementSize( info.format ) , getFormatType( info.format ) , mVertexSize , (void*)info.offset ); } else { glEnableClientState( GL_SECONDARY_COLOR_ARRAY ); glSecondaryColorPointer( getElementSize( info.format ) , getFormatType( info.format ) , mVertexSize , (void*)info.offset ); } break; case Vertex::eTexcoord: glClientActiveTexture( GL_TEXTURE0 + info.idx ); glEnableClientState( GL_TEXTURE_COORD_ARRAY ); glTexCoordPointer( getElementSize( info.format ) , getFormatType( info.format ) , mVertexSize , (void*)info.offset ); haveTex = true; break; } } }
BitSet &DataArray::getModified(BitSet &dst, const DataArray &a) const { dst.setCapacity(size()); dst.clear(); const int n = size(); const int elementSize = getElementSize(); switch(elementSize) { case sizeof(BYTE): { const BYTE *p1 = (const BYTE*)getData(); const BYTE *p2 = (const BYTE*)a.getData(); for(int i = 0; i < n; i++) { if(*(p1++) != *(p2++)) { dst.add(i); } } } break; case sizeof(unsigned short): { const unsigned short *p1 = (const unsigned short*)getData(); const unsigned short *p2 = (const unsigned short*)a.getData(); for(int i = 0; i < n; i++) { if(*(p1++) != *(p2++)) { dst.add(i); } } } break; case sizeof(unsigned int): { const unsigned int *p1 = (const unsigned int*)getData(); const unsigned int *p2 = (const unsigned int*)a.getData(); for(int i = 0; i < n; i++) { if(*(p1++) != *(p2++)) { dst.add(i); } } } break; default: { const BYTE *p1 = (const BYTE*)getData(); const BYTE *p2 = (const BYTE*)a.getData(); for(int i = 0; i < n; i++, p1 += elementSize, p2 += elementSize) { if(*((unsigned int*)p1) != *((unsigned int*)p2)) { dst.add(i); } } } break; } return dst; }
void CLWrapper::copyToHost() { if(!onDevice) { throw std::runtime_error("copyToHost(): not on device"); } // cl->finish(); cl_event event = NULL; error = clEnqueueReadBuffer(*(cl->queue), devicearray, CL_TRUE, 0, getElementSize() * N, getHostArray(), 0, NULL, &event); cl->checkError(error); cl_int err = clWaitForEvents(1, &event); clReleaseEvent(event); if (err != CL_SUCCESS) { throw std::runtime_error("wait for event on copytohost failed with " + easycl::toString( err ) ); } deviceDirty = false; }
void fillTempBuffer(dae_reader_t *reader, geometry_t const *geometry, int meshIndex, vector<float>& rawData) { const vector<int> &triangles = geometry->triangles[meshIndex]; int v = geometry->vertexOffset[meshIndex]; int n = geometry->normalOffset[meshIndex]; int t = geometry->texcoordOffset[meshIndex]; int elementSize = getElementSize(reader); int x = reader->x; int y = reader->y; int z = reader->z; int elementCount = 1; elementCount += n != -1 ? 1 : 0; elementCount += t != -1 ? 1 : 0; for (uint32_t i = 0, j = 0; i < triangles.size(); i += elementCount, j++) { int vSrc = i + v; int trv = triangles[vSrc]; trv *= 3; rawData.push_back(geometry->vertices[trv + x]); rawData.push_back(geometry->vertices[trv + y]); rawData.push_back(geometry->vertices[trv + z]); if (n > -1) { int nSrc = i + n; int trn = triangles[nSrc]; trn *= 3; rawData.push_back(geometry->normals[trn + x]); rawData.push_back(geometry->normals[trn + y]); rawData.push_back(geometry->normals[trn + z]); } if (t > -1) { int tSrc = i + t; int trt = 2 * triangles[tSrc]; rawData.push_back(geometry->texcoords[trt + 0]); rawData.push_back(geometry->texcoords[trt + 1]); } } }
static void convertInputElements(D3D11_INPUT_ELEMENT_DESC* out, JBKShaderInputElement* elements, uint32_t elementCount) { uint32_t offsets[16] = { 0 }; for (uint32_t i = 0; i < elementCount; i++) { uint32_t slot = elements[i].slot; JBKShaderFormat format = elements[i].format; uint8_t instanced = isInstanced(format); out[i].SemanticName = elements[i].name; out[i].SemanticIndex = elements[i].index; out[i].Format = JBKD3DUtils_ConvertFormat(format); out[i].InputSlot = slot; out[i].AlignedByteOffset = offsets[slot]; out[i].InputSlotClass = instanced ? D3D11_INPUT_PER_INSTANCE_DATA : D3D11_INPUT_PER_VERTEX_DATA; out[i].InstanceDataStepRate = instanced ? 1 : 0; offsets[slot] += getElementSize(format); } }
void CLWrapper::copyToDevice() { if(!onHost ) { throw std::runtime_error("copyToDevice(): not on host"); } if( onDevice ) { error = clEnqueueWriteBuffer(*(cl->queue), devicearray, CL_TRUE, 0, getElementSize() * N, getHostArray(), 0, NULL, NULL); cl->checkError(error); deviceDirty = false; } else { // std::cout << "copying buffer to device of " << N << " elements" << std::endl; // for( int i = 0; i < N; i++ ) { // std::cout << "i " << i << " " << ((float*)getHostArrayConst())[i] << std::endl; // } devicearray = clCreateBuffer(*(cl->context), CL_MEM_READ_WRITE | CL_MEM_COPY_HOST_PTR, getElementSize() * N, (void *)getHostArrayConst(), &error); cl->checkError(error); onDevice = true; deviceDirty = false; } }
void fillIndices(dae_reader_t *reader, uint32_t geometryID, vector<float>& rawData) { geometry_t *geometry = &reader->geometry[geometryID]; elements_t elements = getElementOffsets(geometry); int totalTrianglesSize = getTrianglesSize(geometry); vector<uint32_t>& indices = geometry->indices; int elementSize = getElementSize(reader); indices.resize(totalTrianglesSize / elements.elementsCount); memset(indices.data(), 0xFF, indices.size() * sizeof(uint32_t)); int baseIdx = 0; for (uint32_t i = 0; i < geometryID; i++) { baseIdx += reader->geometry[i].maxIndex; } int idx = geometryID == 0 ? 0 : baseIdx + 1; for (uint32_t i = 0; i < indices.size(); i++) { if (indices[i] != -1) continue; indices[i] = idx; addVertex(rawData, geometry->bufferData, i*elementSize, elementSize); for (uint32_t j = i + 1; j < indices.size(); j++) { if (!compareVertices(rawData, i, j, elementSize)) { indices[j] = idx; } } idx++; } geometry->maxIndex = idx - baseIdx - 1; }
//virtual S32 LLSDMessageReader::getSize(const char *blockname, S32 blocknum, const char *varname) { return getElementSize(mMessage[blockname][blocknum][varname]); }