/*------------------------------------- * Copy data from one buffer into another. -------------------------------------*/ bool BufferObject::copy_data(const BufferObject& from) noexcept { LS_DEBUG_ASSERT(from.gpuId != this->gpuId); LS_DEBUG_ASSERT(this->is_valid()); LS_DEBUG_ASSERT(from.is_valid()); LS_DEBUG_ASSERT(from.get_type() == this->get_type()); // bind the buffers to OGL's read/write targets to prevent a pipeline stall. glBindBuffer(VBO_COPY_READ, from.gpuId); GLint numBytes = 0; buffer_access_t usage = buffer_access_t::VBO_STATIC_DRAW; glGetBufferParameteriv(VBO_COPY_READ, GL_BUFFER_SIZE, &numBytes); glGetBufferParameteriv(VBO_COPY_READ, GL_BUFFER_USAGE, (GLint*) & usage); if (numBytes != 0 && !this->gpuId) { if (!init()) { terminate(); return false; } } glBindBuffer(VBO_COPY_WRITE, this->gpuId); glBufferData(VBO_COPY_WRITE, numBytes, nullptr, usage); glCopyBufferSubData(VBO_COPY_READ, VBO_COPY_WRITE, 0, 0, numBytes); // better safe than sorry glBindBuffer(VBO_COPY_READ, 0); glBindBuffer(VBO_COPY_WRITE, 0); copy_attribs(from); return true; }
Buffer Buffer::makeCopy() const { Buffer ret; ret.m_usage = m_usage; ret.m_type = m_type; ret.m_compType = m_compType; ret.m_compCount = m_compCount; ret.m_byteCount = m_byteCount; if (!m_byteCount) return ret; ret.init(); glBindBuffer(GL_COPY_READ_BUFFER, getGlId()); glBindBuffer(GL_COPY_WRITE_BUFFER, ret.getGlId()); glBufferData(GL_COPY_WRITE_BUFFER, m_byteCount, nullptr, m_usage); glCopyBufferSubData(GL_COPY_READ_BUFFER, GL_COPY_WRITE_BUFFER, 0, 0, m_byteCount); glBindBuffer(GL_COPY_READ_BUFFER, 0); glBindBuffer(GL_COPY_WRITE_BUFFER, 0); return ret; }
void GLES2HardwareVertexBuffer::copyData(HardwareBuffer& srcBuffer, size_t srcOffset, size_t dstOffset, size_t length, bool discardWholeBuffer) { // If the buffer is not in system memory we can use ARB_copy_buffers to do an optimised copy. if (srcBuffer.isSystemMemory()) { HardwareBuffer::copyData(srcBuffer, srcOffset, dstOffset, length, discardWholeBuffer); } else { // Unbind the current buffer OGRE_CHECK_GL_ERROR(glBindBuffer(GL_ARRAY_BUFFER, 0)); // Zero out this(destination) buffer OGRE_CHECK_GL_ERROR(glBindBuffer(GL_ARRAY_BUFFER, mBufferId)); OGRE_CHECK_GL_ERROR(glBufferData(GL_ARRAY_BUFFER, length, 0, GLES2HardwareBufferManager::getGLUsage(mUsage))); OGRE_CHECK_GL_ERROR(glBindBuffer(GL_ARRAY_BUFFER, 0)); // Do it the fast way. OGRE_CHECK_GL_ERROR(glBindBuffer(GL_COPY_READ_BUFFER, static_cast<GLES2HardwareVertexBuffer &>(srcBuffer).getGLBufferId())); OGRE_CHECK_GL_ERROR(glBindBuffer(GL_COPY_WRITE_BUFFER, mBufferId)); OGRE_CHECK_GL_ERROR(glCopyBufferSubData(GL_COPY_READ_BUFFER, GL_COPY_WRITE_BUFFER, srcOffset, dstOffset, length)); OGRE_CHECK_GL_ERROR(glBindBuffer(GL_COPY_READ_BUFFER, 0)); OGRE_CHECK_GL_ERROR(glBindBuffer(GL_COPY_WRITE_BUFFER, 0)); } }
void Buffer::resize(GLsizei size) { if(size == _capacity) return; // initialize new buffer GLuint resizedBufferObject = 0; glGenBuffers(1, &resizedBufferObject); glBindBuffer(GL_COPY_WRITE_BUFFER, resizedBufferObject); glBufferData(GL_COPY_WRITE_BUFFER, size, 0, _mode); // copy over old buffer contents glBindBuffer(GL_COPY_READ_BUFFER, _bufferObject); glCopyBufferSubData(GL_COPY_READ_BUFFER, GL_COPY_WRITE_BUFFER, 0, 0, _occupied < size ? _occupied : size); // unbind buffers glBindBuffer(GL_COPY_WRITE_BUFFER, 0); glBindBuffer(GL_COPY_READ_BUFFER, 0); // delete old buffer glDeleteBuffers(1, &_bufferObject); // update variables _bufferObject = resizedBufferObject; _capacity = size; if(_occupied > _capacity) _occupied = _capacity; }
static void resizeBufferIfNecessary(size_t &lastIndex, size_t newLastIndex, size_t bufferSize, size_t stride, GLenum type, GLuint &id, void *&Pointer) { if (newLastIndex * stride >= bufferSize) { while (newLastIndex >= bufferSize) bufferSize = 2 * bufferSize + 1; GLuint newVBO; glGenBuffers(1, &newVBO); glBindBuffer(type, newVBO); if (CVS->supportsAsyncInstanceUpload()) { glBufferStorage(type, bufferSize *stride, 0, GL_MAP_WRITE_BIT | GL_MAP_PERSISTENT_BIT); Pointer = glMapBufferRange(type, 0, bufferSize * stride, GL_MAP_WRITE_BIT | GL_MAP_PERSISTENT_BIT); } else glBufferData(type, bufferSize * stride, 0, GL_DYNAMIC_DRAW); if (id) { // Copy old data GLuint oldVBO = id; glBindBuffer(GL_COPY_WRITE_BUFFER, newVBO); glBindBuffer(GL_COPY_READ_BUFFER, oldVBO); glCopyBufferSubData(GL_COPY_READ_BUFFER, GL_COPY_WRITE_BUFFER, 0, 0, lastIndex * stride); glDeleteBuffers(1, &oldVBO); } id = newVBO; } lastIndex = newLastIndex; }
static enum piglit_result do_copy() { GLuint buffer_handles[2]; glGenBuffersARB(2, buffer_handles); glBindBufferARB(GL_COPY_READ_BUFFER, buffer_handles[0]); glBindBufferARB(GL_COPY_WRITE_BUFFER, buffer_handles[1]); glBufferData(GL_COPY_READ_BUFFER, COPY_BUFFER_SIZE, src_data, GL_STREAM_COPY); glBufferData(GL_COPY_WRITE_BUFFER, COPY_BUFFER_SIZE, NULL, GL_STREAM_READ); glCopyBufferSubData(GL_COPY_READ_BUFFER, GL_COPY_WRITE_BUFFER, 0, 0, COPY_BUFFER_SIZE); glGetBufferSubDataARB(GL_COPY_WRITE_BUFFER, 0, COPY_BUFFER_SIZE, dest_data); if (memcmp(src_data, dest_data, COPY_BUFFER_SIZE) != 0) return PIGLIT_FAIL; return PIGLIT_PASS; }
void ofBufferObject::copyTo(ofBufferObject & dstBuffer){ bind(GL_COPY_READ_BUFFER); dstBuffer.bind(GL_COPY_WRITE_BUFFER); glCopyBufferSubData(GL_COPY_READ_BUFFER,GL_COPY_WRITE_BUFFER,0,0,size()); unbind(GL_COPY_READ_BUFFER); dstBuffer.unbind(GL_COPY_WRITE_BUFFER); }
void BufferObject::copy(const BufferObject& source, uint32_t sourceOffset, uint32_t targetOffset, uint32_t size) { source.bind(TARGET_COPY_READ_BUFFER); bind(TARGET_COPY_WRITE_BUFFER); glCopyBufferSubData(TARGET_COPY_READ_BUFFER, TARGET_COPY_WRITE_BUFFER, sourceOffset, targetOffset, size); unbind(TARGET_COPY_WRITE_BUFFER); source.unbind(TARGET_COPY_READ_BUFFER); GET_GL_ERROR(); }
void GLGpuBufferCore::copyData(GpuBufferCore& srcBuffer, UINT32 srcOffset, UINT32 dstOffset, UINT32 length, bool discardWholeBuffer) { GLGpuBufferCore& glSrcBuffer = static_cast<GLGpuBufferCore&>(srcBuffer); GLuint srcId = glSrcBuffer.getGLBufferId(); glCopyBufferSubData(srcId, getGLBufferId(), srcOffset, dstOffset, length); }
void GLBufferObject::Copy( GLBufferObject& writetarget, int readoffset, int writeoffset, int size ) { Bind(); writetarget.Bind(); glCopyBufferSubData(target, writetarget.Target(), readoffset, writeoffset, size); writetarget.Unbind(); Unbind(); }
void piglit_init(int argc, char **argv) { GLuint bufs[2], list; uint8_t data[8]; uint8_t bad_data[8]; int i; void *ptr; piglit_require_extension("GL_ARB_copy_buffer"); glGenBuffers(2, bufs); for (i = 0; i < ARRAY_SIZE(data); i++) data[i] = i; memset(bad_data, 0xd0, sizeof(bad_data)); glBindBuffer(GL_COPY_READ_BUFFER, bufs[0]); glBufferData(GL_COPY_READ_BUFFER, sizeof(data), data, GL_DYNAMIC_DRAW); glBindBuffer(GL_COPY_WRITE_BUFFER, bufs[1]); glBufferData(GL_COPY_WRITE_BUFFER, sizeof(bad_data), bad_data, GL_DYNAMIC_DRAW); list = glGenLists(1); glNewList(list, GL_COMPILE); glCopyBufferSubData(GL_COPY_READ_BUFFER, GL_COPY_WRITE_BUFFER, 0, 0, sizeof(data)); glEndList(); /* Make sure that it immediately executed. */ ptr = glMapBuffer(GL_COPY_WRITE_BUFFER, GL_READ_ONLY); if (memcmp(ptr, data, sizeof(data))) { fprintf(stderr, "data not copied during display list compile\n"); piglit_report_result(PIGLIT_FAIL); } glUnmapBuffer(GL_COPY_WRITE_BUFFER); /* Now, make sure that it isn't in the list. */ glBufferData(GL_COPY_WRITE_BUFFER, sizeof(bad_data), bad_data, GL_DYNAMIC_DRAW); glCallList(list); ptr = glMapBuffer(GL_COPY_WRITE_BUFFER, GL_READ_ONLY); if (memcmp(ptr, bad_data, sizeof(bad_data))) { fprintf(stderr, "data copied during display list execute\n"); piglit_report_result(PIGLIT_FAIL); } glUnmapBuffer(GL_COPY_WRITE_BUFFER); glDeleteBuffers(2, bufs); piglit_report_result(PIGLIT_PASS); }
void GlContext::copy_buffer_data(const BufferObject& from, BufferObject& to, size_t offset_from, size_t offset_to, size_t count) { glBindBuffer(GL_COPY_READ_BUFFER, from.handle()); CHECK_GL_ERROR(glBindBuffer); glBindBuffer(GL_COPY_WRITE_BUFFER, to.handle()); CHECK_GL_ERROR(glBindBuffer); glCopyBufferSubData(GL_COPY_READ_BUFFER, GL_COPY_WRITE_BUFFER, offset_from, offset_to, count); rebind_buffer_object(); CHECK_GL_ERROR(glCopyBufferSubData); }
void KT_API OGL3HardwareBuffer::CopyData( OGL3ImmediateContext* ctx, Uint32 offset, Uint32 size, Uint32 srcOffset, OGL3HardwareBuffer* src ) { ktOGL3Check( glBindBuffer(GL_COPY_READ_BUFFER, src->GetHandle()) ); ktOGL3Check( glBindBuffer(GL_COPY_WRITE_BUFFER, myBufferID) ); ktOGL3Check( glCopyBufferSubData(GL_COPY_READ_BUFFER, GL_COPY_WRITE_BUFFER, srcOffset, offset, size) ); ktOGL3Check( glBindBuffer(GL_COPY_READ_BUFFER, 0) ); ktOGL3Check( glBindBuffer(GL_COPY_WRITE_BUFFER, 0) ); }
void ofBufferObject::copyTo(ofBufferObject & dstBuffer, int readOffset, int writeOffset, size_t size) const{ #ifdef GLEW_VERSION_4_5 if (GLEW_EXT_direct_state_access) { glCopyNamedBufferSubData(data->id,dstBuffer.getId(),readOffset,writeOffset,size); return; } #endif bind(GL_COPY_READ_BUFFER); dstBuffer.bind(GL_COPY_WRITE_BUFFER); glCopyBufferSubData(GL_COPY_READ_BUFFER,GL_COPY_WRITE_BUFFER,readOffset,writeOffset,size); unbind(GL_COPY_READ_BUFFER); dstBuffer.unbind(GL_COPY_WRITE_BUFFER); }
PIGLIT_GL_TEST_CONFIG_END static bool test_copy(void) { #define BUF_SIZE 600 /* multiple of 100 */ GLuint bufs[2]; char data[BUF_SIZE], *p; int i; int chunk = 100; bool pass = true; assert(BUF_SIZE % chunk == 0); glGenBuffers(2, bufs); for (i = 0; i < BUF_SIZE; i++) data[i] = rand() % 256; glBindBufferARB(GL_COPY_READ_BUFFER, bufs[0]); glBufferData(GL_COPY_READ_BUFFER, BUF_SIZE, data, GL_STATIC_DRAW); glBindBufferARB(GL_COPY_WRITE_BUFFER, bufs[1]); glBufferData(GL_COPY_WRITE_BUFFER, BUF_SIZE, NULL, GL_DYNAMIC_COPY); /* copy from bufs[0] to bufs[1] in chunks */ for (i = 0; i < BUF_SIZE / chunk; i++) { int srcOffset = i * chunk; int dstOffset = i * chunk; glCopyBufferSubData(GL_COPY_READ_BUFFER, GL_COPY_WRITE_BUFFER, srcOffset, dstOffset, chunk); } /* verify dest buffer */ p = glMapBuffer(GL_COPY_WRITE_BUFFER, GL_READ_ONLY); for (i = 0; i < BUF_SIZE; i++) { if (p[i] != data[i]) { printf("expected %d, found %d at location %d\n", data[i], p[i], i); pass = false; break; } } glUnmapBuffer(GL_COPY_WRITE_BUFFER); glDeleteBuffers(2, bufs); return pass; }
bool initBuffer() { GLint const Alignement = 256; GLint BufferPageSize = 0; glGetIntegerv(GL_SPARSE_BUFFER_PAGE_SIZE_ARB, &BufferPageSize); bool Validated(true); GLintptr CopyBufferSize = glm::ceilMultiple<GLint>(VertexSize, Alignement) + glm::ceilMultiple<GLint>(ElementSize, Alignement); glCreateBuffers(buffer::MAX, &BufferName[0]); glNamedBufferStorage(BufferName[buffer::COPY], CopyBufferSize, nullptr, GL_MAP_WRITE_BIT); glm::byte* CopyBufferPointer = reinterpret_cast<glm::byte*>(glMapNamedBufferRange(BufferName[buffer::COPY], 0, CopyBufferSize, GL_MAP_WRITE_BIT | GL_MAP_INVALIDATE_BUFFER_BIT)); memcpy(CopyBufferPointer + 0, VertexData, VertexSize); memcpy(CopyBufferPointer + glm::ceilMultiple<GLint>(VertexSize, Alignement), ElementData, ElementSize); glUnmapNamedBuffer(BufferName[buffer::COPY]); glBindBuffer(GL_COPY_READ_BUFFER, BufferName[buffer::COPY]); glBindBuffer(GL_COPY_WRITE_BUFFER, BufferName[buffer::ELEMENT]); glBufferStorage(GL_COPY_WRITE_BUFFER, glm::ceilMultiple<GLint>(ElementSize, BufferPageSize), nullptr, GL_SPARSE_STORAGE_BIT_ARB); glBufferPageCommitmentARB(GL_COPY_WRITE_BUFFER, 0, BufferPageSize, GL_TRUE); glCopyBufferSubData(GL_COPY_READ_BUFFER, GL_COPY_WRITE_BUFFER, glm::ceilMultiple<GLint>(VertexSize, Alignement), 0, glm::ceilMultiple<GLint>(ElementSize, Alignement)); glBindBuffer(GL_COPY_WRITE_BUFFER, BufferName[buffer::VERTEX]); glBufferStorage(GL_COPY_WRITE_BUFFER, glm::ceilMultiple<GLint>(VertexSize, BufferPageSize), nullptr, GL_SPARSE_STORAGE_BIT_ARB); glBufferPageCommitmentARB(GL_COPY_WRITE_BUFFER, 0, BufferPageSize, GL_TRUE); glCopyBufferSubData(GL_COPY_READ_BUFFER, GL_COPY_WRITE_BUFFER, 0, 0, glm::ceilMultiple<GLint>(VertexSize, Alignement)); GLint UniformBufferOffset(0); glGetIntegerv(GL_UNIFORM_BUFFER_OFFSET_ALIGNMENT, &UniformBufferOffset); GLint UniformBlockSize = glm::max(GLint(sizeof(glm::mat4)), UniformBufferOffset); glNamedBufferStorage(BufferName[buffer::TRANSFORM], UniformBlockSize, nullptr, GL_MAP_WRITE_BIT | GL_MAP_PERSISTENT_BIT | GL_MAP_COHERENT_BIT); return Validated; }
glm::uvec3 AtomicCounter::read(void) { // GLuint *userCounters; glBindBufferBase(GL_ATOMIC_COUNTER_BUFFER, 0, atomicsBuffer); GLuint userCounters[3]; glBindBuffer(GL_ATOMIC_COUNTER_BUFFER, atomicsBuffer); glBindBuffer(GL_SHADER_STORAGE_BUFFER, atomicsReadBuffer); glCopyBufferSubData(GL_ATOMIC_COUNTER_BUFFER, GL_SHADER_STORAGE_BUFFER, 0,0,12); glGetBufferSubData(GL_SHADER_STORAGE_BUFFER, 0, 12, userCounters); glm::uvec3 ret = glm::uvec3(userCounters[0], userCounters[1], userCounters[2]); glUnmapBuffer(GL_SHADER_STORAGE_BUFFER); glUnmapBuffer(GL_ATOMIC_COUNTER_BUFFER); return ret; }
void OGLGraphicsBuffer::CopyToBuffer(GraphicsBuffer& rhs) { if (glloader_GL_VERSION_3_1() || glloader_GL_ARB_copy_buffer()) { OGLRenderEngine& re = *checked_cast<OGLRenderEngine*>(&Context::Instance().RenderFactoryInstance().RenderEngineInstance()); re.BindBuffer(GL_COPY_READ_BUFFER, vb_); re.BindBuffer(GL_COPY_WRITE_BUFFER, checked_cast<OGLGraphicsBuffer*>(&rhs)->vb_); glCopyBufferSubData(GL_COPY_READ_BUFFER, GL_COPY_WRITE_BUFFER, 0, 0, size_in_byte_); } else { GraphicsBuffer::Mapper lhs_mapper(*this, BA_Read_Only); GraphicsBuffer::Mapper rhs_mapper(rhs, BA_Write_Only); std::copy(lhs_mapper.Pointer<uint8_t>(), lhs_mapper.Pointer<uint8_t>() + size_in_byte_, rhs_mapper.Pointer<uint8_t>()); } }
/** Update shadowSplit values and make Cascade Bounding Box pointer valid. * The function aunches two compute kernel that generates an histogram of the depth buffer value (between 0 and 250 with increment of 0.25) * and get an axis aligned bounding box (from SunCamMatrix view) containing all depth buffer value. * It also retrieves the result from the previous computations (in a Round Robin fashion) and update CBB pointer. * \param width of the depth buffer * \param height of the depth buffer * TODO : The depth histogram part is commented out, needs to tweak it when I have some motivation */ void IrrDriver::UpdateSplitAndLightcoordRangeFromComputeShaders(size_t width, size_t height) { // Value that should be kept between multiple calls static bool ssboInit = false; static GLuint CBBssbo, tempShadowMatssbo; CascadeBoundingBox InitialCBB[4]; for (unsigned i = 0; i < 4; i++) { InitialCBB[i].xmin = InitialCBB[i].ymin = InitialCBB[i].zmin = 1000; InitialCBB[i].xmax = InitialCBB[i].ymax = InitialCBB[i].zmax = -1000; } if (!ssboInit) { glGenBuffers(1, &CBBssbo); glGenBuffers(1, &tempShadowMatssbo); ssboInit = true; } glBindBuffer(GL_SHADER_STORAGE_BUFFER, CBBssbo); glBufferData(GL_SHADER_STORAGE_BUFFER, 4 * sizeof(CascadeBoundingBox), InitialCBB, GL_STATIC_DRAW); glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 2, CBBssbo); glUseProgram(FullScreenShader::LightspaceBoundingBoxShader::getInstance()->Program); FullScreenShader::LightspaceBoundingBoxShader::getInstance()->SetTextureUnits(getDepthStencilTexture()); FullScreenShader::LightspaceBoundingBoxShader::getInstance()->setUniforms(m_suncam->getViewMatrix(), shadowSplit[1], shadowSplit[2], shadowSplit[3], shadowSplit[4]); glDispatchCompute((int)width / 64, (int)height / 64, 1); glMemoryBarrier(GL_SHADER_STORAGE_BARRIER_BIT); glBindBuffer(GL_SHADER_STORAGE_BUFFER, tempShadowMatssbo); glBufferData(GL_SHADER_STORAGE_BUFFER, 4 * 16 * sizeof(float), 0, GL_STATIC_COPY); glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 1, tempShadowMatssbo); glUseProgram(FullScreenShader::ShadowMatrixesGenerationShader::getInstance()->Program); FullScreenShader::ShadowMatrixesGenerationShader::getInstance()->setUniforms(m_suncam->getViewMatrix()); glDispatchCompute(4, 1, 1); glMemoryBarrier(GL_SHADER_STORAGE_BARRIER_BIT); glBindBuffer(GL_COPY_READ_BUFFER, tempShadowMatssbo); glBindBuffer(GL_COPY_WRITE_BUFFER, SharedObject::ViewProjectionMatrixesUBO); glCopyBufferSubData(GL_COPY_READ_BUFFER, GL_COPY_WRITE_BUFFER, 0, 80 * sizeof(float), 4 * 16 * sizeof(float)); }
void GLVertexBuffer::copyBuffer(const GraphicBufferPtr& to) { if(to) { GLVertexBuffer* dest = checked_cast<GLVertexBuffer*>(to.get()); if(glCopyBufferSubData) { glBindBuffer(GL_COPY_READ_BUFFER, this->getGLBuffer()); glBindBuffer(GL_COPY_WRITE_BUFFER, dest->getGLBuffer()); CHECK_GL_CALL(glCopyBufferSubData(GL_COPY_READ_BUFFER, GL_COPY_WRITE_BUFFER, 0, 0, this->count() * GetVertexElementsTotalSize(this->format()))); glBindBuffer(GL_COPY_READ_BUFFER, 0); glBindBuffer(GL_COPY_WRITE_BUFFER, 0); } else { log_error(L"GLVertexBuffer::copyBuffer: GL Copy Buffer Sub Data not supported"); } } }
void OsdMeshData::updateGeometry(const MHWRender::MVertexBuffer *points) { // Update coarse vertex int nCoarsePoints = _pointArray.length(); GLuint mayaPositionVBO = *static_cast<GLuint*>(points->resourceHandle()); int size = nCoarsePoints * 3 * sizeof(float); glBindBuffer(GL_COPY_READ_BUFFER, mayaPositionVBO); glBindBuffer(GL_COPY_WRITE_BUFFER, _mesh->BindVertexBuffer()); glCopyBufferSubData(GL_COPY_READ_BUFFER, GL_COPY_WRITE_BUFFER, 0, 0, size); _mesh->Refine(); glBindBuffer(GL_COPY_READ_BUFFER, 0); glBindBuffer(GL_COPY_WRITE_BUFFER, 0); _needsUpdate = false; }
// Buffer update using glMapBufferRange bool initBuffer() { // Generate a buffer object glGenBuffers(buffer::MAX, &BufferName[0]); // Bind the buffer for use glBindBuffer(GL_ARRAY_BUFFER, BufferName[buffer::ARRAY]); // Reserve buffer memory but don't copy the values glBufferData( GL_ARRAY_BUFFER, PositionSize, 0, GL_STATIC_DRAW); // Copy the vertex data in the buffer, in this sample for the whole range of data. // It doesn't required to be the buffer size but pointers require no memory overlapping. GLvoid* Data = glMapBufferRange( GL_ARRAY_BUFFER, 0, // Offset PositionSize, // Size, GL_MAP_WRITE_BIT | GL_MAP_INVALIDATE_BUFFER_BIT | GL_MAP_UNSYNCHRONIZED_BIT | GL_MAP_FLUSH_EXPLICIT_BIT); memcpy(Data, PositionData, PositionSize); // Explicitly send the data to the graphic card. glFlushMappedBufferRange(GL_ARRAY_BUFFER, 0, PositionSize); glUnmapBuffer(GL_ARRAY_BUFFER); // Unbind the buffer glBindBuffer(GL_ARRAY_BUFFER, 0); // Copy buffer glBindBuffer(GL_ARRAY_BUFFER, BufferName[buffer::COPY]); glBufferData(GL_ARRAY_BUFFER, PositionSize, 0, GL_STATIC_DRAW); glBindBuffer(GL_ARRAY_BUFFER, 0); glBindBuffer(GL_COPY_READ_BUFFER, BufferName[buffer::ARRAY]); glBindBuffer(GL_COPY_WRITE_BUFFER, BufferName[buffer::COPY]); glCopyBufferSubData( GL_COPY_READ_BUFFER, GL_COPY_WRITE_BUFFER, 0, 0, PositionSize); glBindBuffer(GL_COPY_READ_BUFFER, 0); glBindBuffer(GL_COPY_WRITE_BUFFER, 0); GLint UniformBlockSize = 0; { glGetActiveUniformBlockiv( ProgramName, UniformTransform, GL_UNIFORM_BLOCK_DATA_SIZE, &UniformBlockSize); glBindBuffer(GL_UNIFORM_BUFFER, BufferName[buffer::TRANSFORM]); glBufferData(GL_UNIFORM_BUFFER, UniformBlockSize, 0, GL_DYNAMIC_DRAW); glBindBuffer(GL_UNIFORM_BUFFER, 0); } { glm::vec4 Diffuse(1.0f, 0.5f, 0.0f, 1.0f); glGetActiveUniformBlockiv( ProgramName, UniformMaterial, GL_UNIFORM_BLOCK_DATA_SIZE, &UniformBlockSize); glBindBuffer(GL_UNIFORM_BUFFER, BufferName[buffer::MATERIAL]); glBufferData(GL_UNIFORM_BUFFER, UniformBlockSize, &Diffuse[0], GL_DYNAMIC_DRAW); glBindBuffer(GL_UNIFORM_BUFFER, 0); } return this->checkError("initBuffer"); }
void Buffer::copySubData(GLenum readTarget, GLenum writeTarget, GLintptr readOffset, GLintptr writeOffset, GLsizeiptr size) { glCopyBufferSubData(readTarget, writeTarget, readOffset, writeOffset, size); CheckGLError(); }
JNIEXPORT void JNICALL Java_org_lwjgl_opengl_GL31_nglCopyBufferSubData(JNIEnv *env, jclass clazz, jint readtarget, jint writetarget, jlong readoffset, jlong writeoffset, jlong size, jlong function_pointer) { glCopyBufferSubDataPROC glCopyBufferSubData = (glCopyBufferSubDataPROC)((intptr_t)function_pointer); glCopyBufferSubData(readtarget, writetarget, readoffset, writeoffset, size); }
bool render() { glm::vec2 WindowSize(this->getWindowSize()); transfer* TransferFBO = reserveTransfer(); transfer* TransferFB = reserveTransfer(); { glBindBuffer(GL_UNIFORM_BUFFER, BufferName[buffer::TRANSFORM]); glm::mat4* Pointer = (glm::mat4*)glMapBufferRange( GL_UNIFORM_BUFFER, 0, sizeof(glm::mat4), GL_MAP_WRITE_BIT | GL_MAP_INVALIDATE_BUFFER_BIT); //glm::mat4 Projection = glm::perspectiveFov(glm::pi<float>() * 0.25f, 640.f, 480.f, 0.1f, 100.0f); glm::mat4 Projection = glm::perspective(glm::pi<float>() * 0.25f, WindowSize.x / WindowSize.y, 0.1f, 100.0f); glm::mat4 Model = glm::mat4(1.0f); *Pointer = Projection * this->view() * Model; // Make sure the uniform buffer is uploaded glUnmapBuffer(GL_UNIFORM_BUFFER); } { glEnable(GL_DEPTH_TEST); glDepthFunc(GL_LESS); glViewport(0, 0, static_cast<GLsizei>(WindowSize.x) * this->FramebufferScale, static_cast<GLsizei>(WindowSize.y) * this->FramebufferScale); glBindFramebuffer(GL_FRAMEBUFFER, FramebufferName); //glEnable(GL_FRAMEBUFFER_SRGB); float Depth(1.0f); glClearBufferfv(GL_DEPTH , 0, &Depth); glClearBufferfv(GL_COLOR, 0, &glm::vec4(1.0f, 0.5f, 0.0f, 1.0f)[0]); glUseProgram(ProgramName[program::TEXTURE]); glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, TextureName[texture::DIFFUSE]); glBindVertexArray(VertexArrayName[program::TEXTURE]); glBindBufferBase(GL_UNIFORM_BUFFER, semantic::uniform::TRANSFORM0, BufferName[buffer::TRANSFORM]); glDrawElementsInstancedBaseVertex(GL_TRIANGLES, ElementCount, GL_UNSIGNED_SHORT, 0, 1, 0); glBindBuffer(GL_PIXEL_PACK_BUFFER, TransferFBO->Buffer); glReadBuffer(GL_COLOR_ATTACHMENT0); glReadPixels(0, 0, 640, 480, GL_RGBA, GL_UNSIGNED_BYTE, 0); glBindBuffer(GL_COPY_READ_BUFFER, TransferFBO->Buffer); glBindBuffer(GL_COPY_WRITE_BUFFER, TransferFBO->Stagging); glCopyBufferSubData(GL_COPY_READ_BUFFER, GL_COPY_WRITE_BUFFER, 0, 0, 640 * 480 * 4); TransferFBO->Fence = glFenceSync(GL_SYNC_GPU_COMMANDS_COMPLETE, 0); } { glDisable(GL_DEPTH_TEST); glViewport(0, 0, static_cast<GLsizei>(WindowSize.x), static_cast<GLsizei>(WindowSize.y)); glBindFramebuffer(GL_FRAMEBUFFER, 0); glUseProgram(ProgramName[program::SPLASH]); glActiveTexture(GL_TEXTURE0); glBindVertexArray(VertexArrayName[program::SPLASH]); glBindTexture(GL_TEXTURE_2D, TextureName[texture::COLORBUFFER]); glDrawArraysInstanced(GL_TRIANGLES, 0, 3, 1); glBindBuffer(GL_PIXEL_PACK_BUFFER, TransferFB->Buffer); glReadBuffer(GL_BACK); glReadPixels(0, 0, 640, 480, GL_RGBA, GL_UNSIGNED_BYTE, 0); glBindBuffer(GL_COPY_READ_BUFFER, TransferFB->Buffer); glBindBuffer(GL_COPY_WRITE_BUFFER, TransferFB->Stagging); glCopyBufferSubData(GL_COPY_READ_BUFFER, GL_COPY_WRITE_BUFFER, 0, 0, 640 * 480 * 4); TransferFB->Fence = glFenceSync(GL_SYNC_GPU_COMMANDS_COMPLETE, 0); } this->queryTranfer(); printf("Live %d, Free %d\r", static_cast<int>(ReadPixelBufferLive.size()), static_cast<int>(ReadPixelBufferFree.size())); return true; }
void OsdPtexMeshData::updateGeometry(const MHWRender::MVertexBuffer *points, const MHWRender::MVertexBuffer *normals) { // Update coarse vertex int nCoarsePoints = _pointArray.length(); GLuint mayaPositionVBO = *static_cast<GLuint*>(points->resourceHandle()); GLuint mayaNormalVBO = normals ? *static_cast<GLuint*>(normals->resourceHandle()) : NULL; int size = nCoarsePoints * 3 * sizeof(float); OpenSubdiv::FarKernelBatchVector const &batches = _farmesh->GetKernelBatches(); if (_kernel == kCPU || _kernel == kOPENMP) { float *d_pos = _cpuPositionBuffer->BindCpuBuffer(); glBindBuffer(GL_ARRAY_BUFFER, mayaPositionVBO); glGetBufferSubData(GL_ARRAY_BUFFER, 0, size, d_pos); g_cpuComputeController->Refine(_cpuComputeContext, batches, _cpuPositionBuffer); if (not _adaptive) { d_pos = _cpuNormalBuffer->BindCpuBuffer(); glBindBuffer(GL_ARRAY_BUFFER, mayaNormalVBO); glGetBufferSubData(GL_ARRAY_BUFFER, 0, size, d_pos); g_cpuComputeController->Refine(_cpuComputeContext, batches, _cpuNormalBuffer); } glBindBuffer(GL_ARRAY_BUFFER, 0); #ifdef OPENSUBDIV_HAS_CUDA } else if (_kernel == kCUDA) { glBindBuffer(GL_COPY_READ_BUFFER, mayaPositionVBO); glBindBuffer(GL_COPY_WRITE_BUFFER, _cudaPositionBuffer->BindVBO()); glCopyBufferSubData(GL_COPY_READ_BUFFER, GL_COPY_WRITE_BUFFER, 0, 0, size); g_cudaComputeController->Refine(_cudaComputeContext, batches, _cudaPositionBuffer); if (not _adaptive) { glBindBuffer(GL_COPY_READ_BUFFER, mayaNormalVBO); glBindBuffer(GL_COPY_WRITE_BUFFER, _cudaNormalBuffer->BindVBO()); glCopyBufferSubData(GL_COPY_READ_BUFFER, GL_COPY_WRITE_BUFFER, 0, 0, size); g_cudaComputeController->Refine(_cudaComputeContext, batches, _cudaNormalBuffer); } glBindBuffer(GL_COPY_READ_BUFFER, 0); glBindBuffer(GL_COPY_WRITE_BUFFER, 0); #endif #ifdef OPENSUBDIV_HAS_OPENCL } else if (_kernel == kCL) { glBindBuffer(GL_COPY_READ_BUFFER, mayaPositionVBO); glBindBuffer(GL_COPY_WRITE_BUFFER, _clPositionBuffer->BindVBO()); glCopyBufferSubData(GL_COPY_READ_BUFFER, GL_COPY_WRITE_BUFFER, 0, 0, size); g_clComputeController->Refine(_clComputeContext, batches, _clPositionBuffer); if (not _adaptive) { glBindBuffer(GL_COPY_READ_BUFFER, mayaNormalVBO); glBindBuffer(GL_COPY_WRITE_BUFFER, _clNormalBuffer->BindVBO()); glCopyBufferSubData(GL_COPY_READ_BUFFER, GL_COPY_WRITE_BUFFER, 0, 0, size); g_clComputeController->Refine(_clComputeContext, batches, _clNormalBuffer); } glBindBuffer(GL_COPY_READ_BUFFER, 0); glBindBuffer(GL_COPY_WRITE_BUFFER, 0); #endif } _needsUpdate = false; }
int FLightBuffer::UploadLights(FDynLightData &data) { int size0 = data.arrays[0].Size()/4; int size1 = data.arrays[1].Size()/4; int size2 = data.arrays[2].Size()/4; int totalsize = size0 + size1 + size2 + 1; // pointless type casting because some compilers can't print enough warnings. if (mBlockAlign > 0 && (unsigned int)totalsize + (mIndex % mBlockAlign) > mBlockSize) { mIndex = ((mIndex + mBlockAlign) / mBlockAlign) * mBlockAlign; // can't be rendered all at once. if ((unsigned int)totalsize > mBlockSize) { int diff = totalsize - (int)mBlockSize; size2 -= diff; if (size2 < 0) { size1 += size2; size2 = 0; } if (size1 < 0) { size0 += size1; size1 = 0; } totalsize = size0 + size1 + size2 + 1; } } if (totalsize <= 1) return -1; if (mIndex + totalsize > mBufferSize/4) { // reallocate the buffer with twice the size unsigned int newbuffer; // first unmap the old buffer glBindBuffer(mBufferType, mBufferId); glUnmapBuffer(mBufferType); // create and bind the new buffer, bind the old one to a copy target (too bad that DSA is not yet supported well enough to omit this crap.) glGenBuffers(1, &newbuffer); glBindBufferBase(mBufferType, LIGHTBUF_BINDINGPOINT, newbuffer); glBindBuffer(mBufferType, newbuffer); // Note: Some older AMD drivers don't do that in glBindBufferBase, as they should. glBindBuffer(GL_COPY_READ_BUFFER, mBufferId); // create the new buffer's storage (twice as large as the old one) mBufferSize *= 2; mByteSize *= 2; if (gl.lightmethod == LM_DIRECT) { glBufferStorage(mBufferType, mByteSize, NULL, GL_MAP_WRITE_BIT | GL_MAP_PERSISTENT_BIT | GL_MAP_COHERENT_BIT); mBufferPointer = (float*)glMapBufferRange(mBufferType, 0, mByteSize, GL_MAP_WRITE_BIT | GL_MAP_PERSISTENT_BIT | GL_MAP_COHERENT_BIT); } else { glBufferData(mBufferType, mByteSize, NULL, GL_DYNAMIC_DRAW); mBufferPointer = (float*)glMapBufferRange(mBufferType, 0, mByteSize, GL_MAP_WRITE_BIT|GL_MAP_INVALIDATE_BUFFER_BIT); } // copy contents and delete the old buffer. glCopyBufferSubData(GL_COPY_READ_BUFFER, mBufferType, 0, 0, mByteSize/2); glBindBuffer(GL_COPY_READ_BUFFER, 0); glDeleteBuffers(1, &mBufferId); mBufferId = newbuffer; } float *copyptr; assert(mBufferPointer != NULL); if (mBufferPointer == NULL) return -1; copyptr = mBufferPointer + mIndex * 4; float parmcnt[] = { 0, float(size0), float(size0 + size1), float(size0 + size1 + size2) }; memcpy(©ptr[0], parmcnt, 4 * sizeof(float)); memcpy(©ptr[4], &data.arrays[0][0], 4 * size0*sizeof(float)); memcpy(©ptr[4 + 4*size0], &data.arrays[1][0], 4 * size1*sizeof(float)); memcpy(©ptr[4 + 4*(size0 + size1)], &data.arrays[2][0], 4 * size2*sizeof(float)); unsigned int bufferindex = mIndex; mIndex += totalsize; draw_dlight += (totalsize-1) / 2; return bufferindex; }
void base::app::capture_screen() { const int screen_size = get_wnd_width() * get_wnd_height() * base::get_pfd(CAPTURE_PF)->_size; const int tmp_head = (_rb_head + 1) & RB_BUFFERS_MASK; if(tmp_head == _rb_tail) { printf("we are too fast there is no free screen buffer!\n"); } else { screen_buffer *sc = _screen_buffers_rb + tmp_head; glReadBuffer(GL_BACK); glBindTexture(GL_TEXTURE_2D, sc->_tmp_texture); glCopyTexSubImage2D( GL_TEXTURE_2D, 0, 0, 0, 0, 0, get_wnd_width(), get_wnd_height()); glBindTexture(GL_TEXTURE_2D, 0); glQueryCounter(sc->_queries[0], GL_TIMESTAMP); glBindBuffer( GL_PIXEL_PACK_BUFFER, base::cfg().use_nvidia_fast_download ? sc->_tmp_buffer : sc->_buffer); glReadPixels( 0, 0, get_wnd_width(), get_wnd_height(), base::get_pfd(CAPTURE_PF)->_format, base::get_pfd(CAPTURE_PF)->_type, 0); glBindBuffer(GL_PIXEL_PACK_BUFFER, 0); glQueryCounter(sc->_queries[1], GL_TIMESTAMP); if(base::cfg().use_nvidia_fast_download) { glBindBuffer(GL_COPY_READ_BUFFER, sc->_tmp_buffer); glBindBuffer(GL_COPY_WRITE_BUFFER, sc->_buffer); glCopyBufferSubData( GL_COPY_READ_BUFFER, GL_COPY_WRITE_BUFFER, 0, 0, screen_size); } glQueryCounter(sc->_queries[2], GL_TIMESTAMP); sc->_fence = glFenceSync(GL_SYNC_GPU_COMMANDS_COMPLETE, 0); sc->_frame_num = _frame_number; sc->_stage = 0; // update ring buffer head _rb_head = tmp_head; } }
enum piglit_result piglit_display(void) { float white[4] = {1.0, 1.0, 1.0, 1.0}; GLboolean pass = GL_TRUE; int i; #ifdef PIGLIT_USE_OPENGL float array[] = { 17, 13, 0, 17, 18, 0, 12, 13, 0, 12, 18, 0, 27, 13, 0, 27, 18, 0, 22, 13, 0, 22, 18, 0, 37, 13, 0, 37, 18, 0, 32, 13, 0, 32, 18, 0, 47, 13, 0, 47, 18, 0, 42, 13, 0, 42, 18, 0 }; #else // PIGLIT_USE_OPENGL_ES3 float array[] = { 1.00, 0.75, 0.0, 1.00, 1.00, 0.0, 0.75, 0.75, 0.0, 0.75, 1.00, 0.0, 0.50, 0.75, 0.0, 0.50, 1.00, 0.0, 0.25, 0.75, 0.0, 0.25, 1.00, 0.0, 0.00, 0.75, 0.0, 0.00, 1.00, 0.0, -0.25, 0.75, 0.0, -0.25, 1.00, 0.0, -0.50, 0.75, 0.0, -0.50, 1.00, 0.0, -0.75, 0.75, 0.0, -0.75, 1.00, 0.0, }; #endif glClear(GL_COLOR_BUFFER_BIT); if (test == DRAW) { #ifdef PIGLIT_USE_OPENGL glEnableClientState(GL_VERTEX_ARRAY); glBindBuffer(GL_ARRAY_BUFFER, buffer); glVertexPointer(3, GL_FLOAT, 0, 0); glBindBuffer(GL_ARRAY_BUFFER, 0); #else // PIGLIT_USE_OPENGL_ES3 glBindVertexArray(vao); #endif memcpy(map, array, 12 * sizeof(float)); if (!coherent) glMemoryBarrier(GL_CLIENT_MAPPED_BUFFER_BARRIER_BIT); glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); memcpy(map+12, array+12, 12 * sizeof(float)); if (!coherent) glMemoryBarrier(GL_CLIENT_MAPPED_BUFFER_BARRIER_BIT); glDrawArrays(GL_TRIANGLE_STRIP, 4, 4); memcpy(map+12*2, array+12*2, 12 * sizeof(float)); if (!coherent) glMemoryBarrier(GL_CLIENT_MAPPED_BUFFER_BARRIER_BIT); glDrawArrays(GL_TRIANGLE_STRIP, 8, 4); memcpy(map+12*3, array+12*3, 12 * sizeof(float)); if (!coherent) glMemoryBarrier(GL_CLIENT_MAPPED_BUFFER_BARRIER_BIT); glDrawArrays(GL_TRIANGLE_STRIP, 12, 4); piglit_check_gl_error(0); #ifdef PIGLIT_USE_OPENGL pass = pass && piglit_probe_pixel_rgb(15, 15, white); pass = pass && piglit_probe_pixel_rgb(25, 15, white); pass = pass && piglit_probe_pixel_rgb(35, 15, white); pass = pass && piglit_probe_pixel_rgb(45, 15, white); glDisableClientState(GL_VERTEX_ARRAY); #else // PIGLIT_USE_OPENGL_ES3 pass = pass && piglit_probe_pixel_rgba(13, 87, white); pass = pass && piglit_probe_pixel_rgba(39, 87, white); pass = pass && piglit_probe_pixel_rgba(65, 87, white); pass = pass && piglit_probe_pixel_rgba(91, 87, white); #endif } else if (test == READ) { GLuint srcbuf; GLsync fence; glGenBuffers(1, &srcbuf); glBindBuffer(GL_COPY_READ_BUFFER, srcbuf); glBufferData(GL_COPY_READ_BUFFER, BUF_SIZE, array, GL_STATIC_DRAW); /* Copy some data to the mapped buffer and check if the CPU can see it. */ glBindBuffer(GL_COPY_WRITE_BUFFER, buffer); glCopyBufferSubData(GL_COPY_READ_BUFFER, GL_COPY_WRITE_BUFFER, 0, 0, BUF_SIZE); glBindBuffer(GL_COPY_READ_BUFFER, 0); glBindBuffer(GL_COPY_WRITE_BUFFER, 0); glDeleteBuffers(1, &srcbuf); if (!coherent) glMemoryBarrier(GL_CLIENT_MAPPED_BUFFER_BARRIER_BIT); fence = glFenceSync(GL_SYNC_GPU_COMMANDS_COMPLETE, 0); glClientWaitSync(fence, GL_SYNC_FLUSH_COMMANDS_BIT, GL_TIMEOUT_IGNORED); for (i = 0; i < ARRAY_SIZE(array); i++) { if (map[i] != array[i]) { printf("Probe [%i] failed. Expected: %f Observed: %f\n", i, array[i], map[i]); pass = GL_FALSE; } } } else { assert(0); } piglit_present_results(); return pass ? PIGLIT_PASS : PIGLIT_FAIL; }
PIGLIT_GL_TEST_CONFIG_END void piglit_init(int argc, char *argv[]) { bool pass = true; uint32_t dummy_data_1[4], dummy_data_2[4]; uint32_t good_data[4] = {0, 1, 2, 3}; uint32_t result_data[4]; bool subtest_pass; size_t size = sizeof(good_data); GLuint buffer_handles[2]; memset(dummy_data_1, 0xaa, size); memset(dummy_data_2, 0xbb, size); piglit_require_extension("GL_ARB_copy_buffer"); glGenBuffers(2, buffer_handles); glBindBuffer(GL_COPY_READ_BUFFER, buffer_handles[0]); glBindBuffer(GL_COPY_WRITE_BUFFER, buffer_handles[1]); glBufferData(GL_COPY_READ_BUFFER, 4096, NULL, GL_STREAM_COPY); glBufferData(GL_COPY_WRITE_BUFFER, 4096, NULL, GL_STREAM_COPY); glBufferSubData(GL_COPY_READ_BUFFER, 0, size, good_data); glBufferSubData(GL_COPY_WRITE_BUFFER, 0, size, dummy_data_1); glCopyBufferSubData(GL_COPY_READ_BUFFER, GL_COPY_WRITE_BUFFER, 0, 0, size); glBufferSubData(GL_COPY_READ_BUFFER, 0, size, dummy_data_2); memset(result_data, 0xd0, size); glGetBufferSubData(GL_COPY_WRITE_BUFFER, 0, size, result_data); subtest_pass = memcmp(good_data, result_data, size) == 0; if (!subtest_pass) { fprintf(stderr, "found 0x%08x 0x%08x 0x%08x 0x%08x\n", result_data[0], result_data[1], result_data[2], result_data[3]); pass = false; } piglit_report_subtest_result(subtest_pass ? PIGLIT_PASS : PIGLIT_FAIL, "overwrite source data"); glBufferData(GL_COPY_READ_BUFFER, 4096, NULL, GL_STREAM_COPY); glBufferData(GL_COPY_WRITE_BUFFER, 4096, NULL, GL_STREAM_COPY); glBufferSubData(GL_COPY_READ_BUFFER, 0, size, dummy_data_1); glBufferSubData(GL_COPY_WRITE_BUFFER, 0, size, dummy_data_2); glCopyBufferSubData(GL_COPY_READ_BUFFER, GL_COPY_WRITE_BUFFER, 0, 0, size); glBufferSubData(GL_COPY_WRITE_BUFFER, 0, size, good_data); memset(result_data, 0xd0, size); glGetBufferSubData(GL_COPY_WRITE_BUFFER, 0, size, result_data); subtest_pass = memcmp(good_data, result_data, size) == 0; if (!subtest_pass) { fprintf(stderr, "found 0x%08x 0x%08x 0x%08x 0x%08x\n", result_data[0], result_data[1], result_data[2], result_data[3]); pass = false; } piglit_report_subtest_result(subtest_pass ? PIGLIT_PASS : PIGLIT_FAIL, "overwrite destination data"); piglit_report_result(pass ? PIGLIT_PASS : PIGLIT_FAIL); }