int Histogram::setupHistogram() { int i = 0; int status = mapBuffer( dataBuf, data, sizeof(cl_uint) * width * height, CL_MAP_WRITE_INVALIDATE_REGION); CHECK_ERROR(status, SDK_SUCCESS, "Failed to map device buffer.(dataBuf)"); for(i = 0; i < width * height; i++) { data[i] = rand() % (cl_uint)(binSize); } status = unmapBuffer( dataBuf, data ); CHECK_ERROR(status, SDK_SUCCESS, "Failed to unmap device buffer.(dataBuf)"); hostBin = (cl_uint*)malloc(binSize * sizeof(cl_uint)); CHECK_ALLOCATION(hostBin, "Failed to allocate host memory. (hostBin)"); memset(hostBin, 0, binSize * sizeof(cl_uint)); deviceBin = (cl_uint*)malloc(binSize * sizeof(cl_uint)); CHECK_ALLOCATION(deviceBin, "Failed to allocate host memory. (deviceBin)"); memset(deviceBin, 0, binSize * sizeof(cl_uint)); return SDK_SUCCESS; }
int ComputeBench::verifyResults() { if (sampleArgs->verify) { int vecElements = (vec3 == true) ? 3 : vectorSize; int sizeElement = vectorSize * sizeof (cl_float); //int readLength = length + (NUM_READS * 1024 / sizeElement) + EXTRA_ELEMENTS; int status, passStatus; /////////////////////////////////////////////////////////////////////////////////////////////////// std::cout << "\nVerifying results for KAdd : " << std::endl; // Map cl_mem outputKadd to host for reading status = mapBuffer(outputKadd, outputKaddHost, (length * sizeElement), CL_MAP_READ); CHECK_ERROR(status, SDK_SUCCESS, "Failed to map device buffer.(outputKadd)"); passStatus = 1; uint* devBuffer = (uint *) outputKaddHost; for (int i = 0; i < length; i++) { for (int j = 0; j < vecElements; j++) { //uint answer = i+j; uint answer = i; for (int ii = 0; ii < 1000; ii++) { //answer ^= ii; //answer = answer << (ii ) | answer >> (32 - ii ); //answer += ii; answer += ii; answer = answer ^ ii; answer++; } // std::cout << " gid:" << i << " vec:" << j << " answer:" << answer << " result:" << devBuffer[j] << std::endl; if (devBuffer[j] != answer) passStatus = 0; } if (passStatus != 1) break; devBuffer += vectorSize; } status = unmapBuffer(outputKadd, outputKaddHost); CHECK_ERROR(status, SDK_SUCCESS, "Failed to unmap device buffer.(outputKadd)"); if (passStatus == 1) { std::cout << "Passed!\n" << std::endl; } else { std::cout << "Failed!\n" << std::endl; return SDK_FAILURE; } } return SDK_SUCCESS; }
Result GraphicsInterface::writeBuffer(void *dst_buf, const void *src_mem, size_t write_size, BufferType type) { MapContext ctx; ctx.resource = dst_buf; ctx.type = type; ctx.mode = MapMode::Write; auto res = mapBuffer(ctx); if (res == Result::OK) { memcpy(ctx.data_ptr, src_mem, write_size); res = unmapBuffer(ctx); } return res; }
Result GraphicsInterface::readBuffer(void *dst_mem, void *src_buf, size_t read_size, BufferType type) { MapContext ctx; ctx.resource = src_buf; ctx.type = type; ctx.mode = MapMode::Read; auto res = mapBuffer(ctx); if (res == Result::OK) { memcpy(dst_mem, ctx.data_ptr, read_size); res = unmapBuffer(ctx); } return res; }
// Test reading from a memory-mapped buffer. TEST_F(ClBufferTest, mapForReading) { // initialize buffer with input data ClBuffer buffer(error, *c, bufferSize, input.data(), clReadOnly); ASSERT_OK(error); // memory-map and read data ClMapBuffer mapBuffer(buffer, CL_MAP_READ); q->enqueueBlocking(error, mapBuffer); ASSERT_OK(error); quint8 * bytes = (quint8 * )mapBuffer.pointer(); for (int i = 0; i < (int)bufferSize; ++i) output[i] = bytes[i]; ClUnmapBuffer unmapBuffer(mapBuffer); q->enqueue(error, unmapBuffer); ASSERT_OK(error); ASSERT_EQ(input, output) << "Data read from buffer was not the same as " "data loaded into it."; }
// Test writing to a memory-mapped buffer. TEST_F(ClBufferTest, mapForWriting) { ClBuffer buffer(error, *c, bufferSize); ASSERT_OK(error); // memory-map and write to buffer ClMapBuffer mapBuffer(buffer, CL_MAP_WRITE); q->enqueueBlocking(error, mapBuffer); ASSERT_OK(error); quint8 * bytes = (quint8 * )mapBuffer.pointer(); for (int i = 0; i < (int)bufferSize; ++i) bytes[i] = input[i]; ClUnmapBuffer unmapBuffer(mapBuffer); q->enqueue(error, unmapBuffer); ASSERT_OK(error); // read data back ClReadBuffer readBuffer(buffer, output.data()); q->enqueue(error, readBuffer); ASSERT_OK(error); q->finish(error); ASSERT_OK(error); ASSERT_EQ(input, output) << "Data read from buffer was not the same as " "data written to it."; }
int Histogram::calculateHostBin() { int status = mapBuffer( dataBuf, data, sizeof(cl_uint) * width * height, CL_MAP_READ); CHECK_ERROR(status, SDK_SUCCESS, "Failed to map device buffer.(dataBuf in calcHostBin)"); for(int i = 0; i < height; ++i) { for(int j = 0; j < width; ++j) { hostBin[data[i * width + j]]++; } } status = unmapBuffer( dataBuf, data ); CHECK_ERROR(status, SDK_SUCCESS, "Failed to unmap device buffer.(dataBuf in calcHostBin)"); return SDK_SUCCESS; }
// Set for rendering void LLVertexBuffer::setBuffer(U32 data_mask) { LLMemType mt(LLMemType::MTYPE_VERTEX_DATA); //set up pointers if the data mask is different ... BOOL setup = (sLastMask != data_mask); if (useVBOs()) { if (mGLBuffer && (mGLBuffer != sGLRenderBuffer || !sVBOActive)) { glBindBufferARB(GL_ARRAY_BUFFER_ARB, mGLBuffer); sVBOActive = TRUE; setup = TRUE; // ... or the bound buffer changed } if (mGLIndices && (mGLIndices != sGLRenderIndices || !sIBOActive)) { glBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, mGLIndices); sIBOActive = TRUE; } unmapBuffer(); } else { if (mGLBuffer) { if (sEnableVBOs && sVBOActive) { glBindBufferARB(GL_ARRAY_BUFFER_ARB, 0); sVBOActive = FALSE; setup = TRUE; // ... or a VBO is deactivated } if (sGLRenderBuffer != mGLBuffer) { setup = TRUE; // ... or a client memory pointer changed } } if (sEnableVBOs && mGLIndices && sIBOActive) { glBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, 0); sIBOActive = FALSE; } } if (mGLIndices) { sGLRenderIndices = mGLIndices; } if (mGLBuffer) { sGLRenderBuffer = mGLBuffer; if (data_mask && setup) { if (!sRenderActive) { llwarns << "Vertex buffer set for rendering outside of render frame." << llendl; } setupVertexBuffer(data_mask); // subclass specific setup (virtual function) sLastMask = data_mask; } } }
// Set for rendering void LLVertexBuffer::setBuffer(U32 data_mask, S32 type) { LLMemType mt(LLMemType::MTYPE_VERTEX_DATA); //set up pointers if the data mask is different ... BOOL setup = (sLastMask != data_mask); if (useVBOs()) { if (mGLBuffer && (mGLBuffer != sGLRenderBuffer || !sVBOActive)) { /*if (sMapped) { llerrs << "VBO bound while another VBO mapped!" << llendl; }*/ stop_glerror(); glBindBufferARB(GL_ARRAY_BUFFER_ARB, mGLBuffer); stop_glerror(); sBindCount++; sVBOActive = TRUE; setup = TRUE; // ... or the bound buffer changed } if (mGLIndices && (mGLIndices != sGLRenderIndices || !sIBOActive)) { /*if (sMapped) { llerrs << "VBO bound while another VBO mapped!" << llendl; }*/ stop_glerror(); glBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, mGLIndices); stop_glerror(); sBindCount++; sIBOActive = TRUE; } BOOL error = FALSE; if (gDebugGL) { GLint buff; glGetIntegerv(GL_ARRAY_BUFFER_BINDING_ARB, &buff); if ((GLuint)buff != mGLBuffer) { llerrs << "Invalid GL vertex buffer bound: " << buff << llendl; } if (mGLIndices) { glGetIntegerv(GL_ELEMENT_ARRAY_BUFFER_BINDING_ARB, &buff); if ((GLuint)buff != mGLIndices) { llerrs << "Invalid GL index buffer bound: " << buff << llendl; } } } if (mResized) { if (gDebugGL) { GLint buff; glGetIntegerv(GL_ARRAY_BUFFER_BINDING_ARB, &buff); if ((GLuint)buff != mGLBuffer) { llerrs << "Invalid GL vertex buffer bound: " << buff << llendl; } if (mGLIndices != 0) { glGetIntegerv(GL_ELEMENT_ARRAY_BUFFER_BINDING_ARB, &buff); if ((GLuint)buff != mGLIndices) { llerrs << "Invalid GL index buffer bound: " << buff << llendl; } } } if (mGLBuffer) { stop_glerror(); glBufferDataARB(GL_ARRAY_BUFFER_ARB, getSize(), NULL, mUsage); stop_glerror(); } if (mGLIndices) { stop_glerror(); glBufferDataARB(GL_ELEMENT_ARRAY_BUFFER_ARB, getIndicesSize(), NULL, mUsage); stop_glerror(); } mEmpty = TRUE; mResized = FALSE; if (data_mask != 0) { llerrs << "Buffer set for rendering before being filled after resize." << llendl; } } if (error) { llerrs << "LLVertexBuffer::mapBuffer failed" << llendl; } unmapBuffer(type); } else { if (mGLBuffer) { if (sVBOActive) { glBindBufferARB(GL_ARRAY_BUFFER_ARB, 0); sBindCount++; sVBOActive = FALSE; setup = TRUE; // ... or a VBO is deactivated } if (sGLRenderBuffer != mGLBuffer) { setup = TRUE; // ... or a client memory pointer changed } } if (mGLIndices && sIBOActive) { /*if (sMapped) { llerrs << "VBO unbound while potentially mapped!" << llendl; }*/ glBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, 0); sBindCount++; sIBOActive = FALSE; } } setupClientArrays(data_mask); if (mGLIndices) { sGLRenderIndices = mGLIndices; } if (mGLBuffer) { sGLRenderBuffer = mGLBuffer; if (data_mask && setup) { setupVertexBuffer(data_mask); // subclass specific setup (virtual function) sSetCount++; } } }
int Histogram::runCLKernels(void) { cl_int status; cl_int eventStatus = CL_QUEUED; status = this->setWorkGroupSize(); CHECK_ERROR(status, SDK_SUCCESS, "setKernelWorkGroupSize() failed"); // whether sort is to be in increasing order. CL_TRUE implies increasing status = clSetKernelArg(kernel, 0, sizeof(cl_mem), (void*)&dataBuf); CHECK_OPENCL_ERROR(status, "clSetKernelArg failed. (dataBuf)"); status = clSetKernelArg(kernel, 1, groupSize * binSize * sizeof(cl_uchar), NULL); CHECK_OPENCL_ERROR(status, "clSetKernelArg failed. (local memory)"); status = clSetKernelArg(kernel, 2, sizeof(cl_mem), (void*)&midDeviceBinBuf); CHECK_OPENCL_ERROR(status, "clSetKernelArg failed. (deviceBinBuf)"); // Enqueue a kernel run call. cl_event ndrEvt; status = clEnqueueNDRangeKernel( commandQueue, kernel, 1, NULL, &globalThreads, &localThreads, 0, NULL, &ndrEvt); CHECK_OPENCL_ERROR(status, "clEnqueueNDRangeKernel failed."); status = clFlush(commandQueue); CHECK_OPENCL_ERROR(status, "clFlush failed."); status = waitForEventAndRelease(&ndrEvt); CHECK_ERROR(status, SDK_SUCCESS, "WaitForEventAndRelease(ndrEvt1) Failed"); status = mapBuffer( midDeviceBinBuf, midDeviceBin, subHistgCnt * binSize * sizeof(cl_uint), CL_MAP_READ); CHECK_ERROR(status, SDK_SUCCESS, "Failed to map device buffer.(midDeviceBinBuf)"); // Clear deviceBin array memset(deviceBin, 0, binSize * sizeof(cl_uint)); // Calculate final histogram bin for(int i = 0; i < subHistgCnt; ++i) { for(int j = 0; j < binSize; ++j) { deviceBin[j] += midDeviceBin[i * binSize + j]; } } status = unmapBuffer( midDeviceBinBuf, midDeviceBin); CHECK_ERROR(status, SDK_SUCCESS, "Failed to unmap device buffer.(midDeviceBinBuf)"); return SDK_SUCCESS; }
bool Magic3D::TextData::update(Object* object) { if (object) { } Renderer* renderer = Renderer::getInstance(); Font* font = getFont(); std::vector<float> lines; if (!font) { return false; } float fsize = (textSize / 512.0f) / font->getLineHeightInPixels(); float lineHeight = fsize * (font->getLineHeightInPixels() + 2.0f); int quadCount = getVerticesCount() / 4; if (quadCount < (int)text.size() - lastReturns) { lastReturns = 0; size_t tsize = text.size(); for (size_t i = 0; i < tsize; i++) { if (text.at(i) == '\n') { lastReturns++; continue; } else if (renderer->hasMapBuffer() || ((int)i >= quadCount)) { addQuad(0.0f, 0.0f, 1.0f, 1.0f, eAXIS_Z, false); } } createVbo(); quadCount = getVerticesCount() / 4; } width = 0.0f; height = lineHeight; if (getVerticesCount() > 0) { int quad = -1; float startX = 0.0f; float startY = 0.0f; float* buffer = mapBuffer(); int lastIndex = 0; size_t tsize = text.size(); for (size_t i = 0; i < tsize; i++) { int index = text.at(i); if (index < 0) { index = 256 + index; } FontChar* fchar = font->getChar(index); if (text.at(i) == '\n') { lines.push_back(startX); if (width < startX) { width = startX; } startX = 0.0f; if (invert) { startY -= lineHeight; } else { startY += lineHeight; } height += lineHeight; continue; } else { quad++; } float texX = fchar->x; float texY = 1.0f - fchar->y; float texW = fchar->width; float texH = fchar->height; float cwidth = fchar->width * font->getTextureWidth() * fsize; float cheight = fchar->height * font->getTextureHeight() * fsize; float coffsetX = fchar->offsetX * font->getTextureWidth() * fsize; float coffsetY = fchar->offsetY * font->getTextureHeight() * fsize; float cadvanceX = fchar->advanceX * font->getTextureWidth() * fsize; float ckernel = 0.0f; std::map<int, float>::const_iterator k = fchar->kernel.find(lastIndex); if (k != fchar->kernel.end()) { ckernel = (*k).second * fsize; } setQuad(buffer, quad, startX + coffsetX + ckernel, startY + coffsetY * 0.5f, cwidth, cheight); if (invert) { setQuadTexture(buffer, quad, texX, texY - texH, texW, -texH); } else { setQuadTexture(buffer, quad, texX, texY, texW, texH); } startX += cadvanceX; lastIndex = index; } quad++; for (int i = quad; i < quadCount; i++) { setQuad(buffer, i, startX, startY, 0.0f, 0.0f); } lines.push_back(startX); if (width < startX) { width = startX; } width += (2.0f * fsize); quad = 0; int line = 0; tsize = text.size(); for (size_t i = 0; i < tsize; i++) { if (text.at(i) != '\n') { float w = width * -0.5f; float diff = line < (int)lines.size() ? lines.at(line) : width; switch (textAlignment) { case eHORIZONTAL_ALIGN_CENTER: w += (width - diff) * 0.5f; break; case eHORIZONTAL_ALIGN_RIGHT: w += (width - diff); break; default: break; } if (invert) { moveQuad(buffer, quad, w, height * 0.5f - lineHeight, 0.0f); } else { moveQuad(buffer, quad, w, height * -0.5f, 0.0f); } quad++; } else { line++; } } unmapBuffer(); } changed = false; box.corners[0] = Vector3(-width * 0.5f, -height * 0.5f, 0.0f); box.corners[1] = Vector3(width * 0.5f, height * 0.5f, 0.0f); return true; }
void VertexUpdateCommand::unmap() { auto ifs = gi::GetGraphicsInterface(); ifs->unmapBuffer(m_ctx_vb); ifs->unmapBuffer(m_ctx_ib); }