void piglit_init(int argc, char **argv) { bool pass = true; GLsync valid_sync; GLsync invalid_sync = (GLsync)20; if (piglit_get_gl_version() < 32) { piglit_require_extension("GL_ARB_sync"); } valid_sync = glFenceSync(GL_SYNC_GPU_COMMANDS_COMPLETE, 0); /* test that valid parameters passed results in NO_ERROR */ glWaitSync(valid_sync, 0, GL_TIMEOUT_IGNORED); pass = piglit_check_gl_error(GL_NO_ERROR) && pass; /* test that invalid sync results in INVALID_VALUE */ glWaitSync(invalid_sync, 0, GL_TIMEOUT_IGNORED); pass = piglit_check_gl_error(GL_INVALID_VALUE) && pass; /* test that invalid flag value results in INVALID_VALUE */ glWaitSync(valid_sync, 3, GL_TIMEOUT_IGNORED); pass = piglit_check_gl_error(GL_INVALID_VALUE) && pass; glDeleteSync(valid_sync); piglit_report_result(pass ? PIGLIT_PASS : PIGLIT_FAIL); }
void RenderThread::renderFrame() { auto windowSize = _window->geometry().size(); uvec2 readFboSize; uint32_t readFbo{ 0 }; if (_activeFrame) { const auto &frame = _activeFrame; _backend->recycle(); _backend->syncCache(); _gpuContext->enableStereo(frame->stereoState._enable); if (frame && !frame->batches.empty()) { _gpuContext->executeFrame(frame); } auto &glBackend = static_cast<gpu::gl::GLBackend&>(*_backend); readFbo = glBackend.getFramebufferID(frame->framebuffer); readFboSize = frame->framebuffer->getSize(); CHECK_GL_ERROR(); } else { hifi::qml::OffscreenSurface::TextureAndFence newTextureAndFence; if (_offscreen->fetchTexture(newTextureAndFence)) { if (_uiTexture != 0) { auto readFence = glFenceSync(GL_SYNC_GPU_COMMANDS_COMPLETE, 0); glFlush(); _offscreen->getDiscardLambda()(_uiTexture, readFence); _uiTexture = 0; } glWaitSync((GLsync)newTextureAndFence.second, 0, GL_TIMEOUT_IGNORED); glDeleteSync((GLsync)newTextureAndFence.second); _uiTexture = newTextureAndFence.first; glBindFramebuffer(GL_READ_FRAMEBUFFER, _uiFbo); glFramebufferTexture2D(GL_READ_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, _uiTexture, 0); } if (_uiTexture != 0) { readFbo = _uiFbo; readFboSize = { windowSize.width(), windowSize.height() }; } } if (readFbo) { glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0); glBindFramebuffer(GL_READ_FRAMEBUFFER, readFbo); glBlitFramebuffer( 0, 0, readFboSize.x, readFboSize.y, 0, 0, windowSize.width(), windowSize.height(), GL_COLOR_BUFFER_BIT, GL_NEAREST); glBindFramebuffer(GL_READ_FRAMEBUFFER, 0); } else { glClearColor(1, 0, 0, 1); glClear(GL_COLOR_BUFFER_BIT); } _glContext.swapBuffers(); }
bool WrappedOpenGL::Serialise_glWaitSync(GLsync sync, GLbitfield flags, GLuint64 timeout) { SERIALISE_ELEMENT(uint32_t, Flags, flags); SERIALISE_ELEMENT(uint64_t, Timeout, timeout); SERIALISE_ELEMENT(ResourceId, id, GetResourceManager()->GetSyncID(sync)); if(m_State < WRITING) { GLResource res = GetResourceManager()->GetLiveResource(id); glWaitSync(GetResourceManager()->GetSync(res.name), Flags, Timeout); } return true; }
cl_int computeVBO() { cl_int errNum; // a small internal counter for animation static cl_int seq = 0; seq = (seq + 1) % (imWidth); // Set the kernel arguments, send the cl_mem object for the VBO errNum = clSetKernelArg(kernel, 0, sizeof(cl_mem), &cl_vbo_mem); errNum = clSetKernelArg(kernel, 1, sizeof(cl_int), &imWidth); errNum = clSetKernelArg(kernel, 2, sizeof(cl_int), &imHeight); errNum = clSetKernelArg(kernel, 3, sizeof(cl_int), &seq); if (errNum != CL_SUCCESS) { std::cerr << "Error setting kernel arguments." << std::endl; Cleanup(); return 1; } size_t globalWorkSize[1] = { vbolen }; size_t localWorkSize[1] = { 32 }; // Acquire the GL Object // Note, we should ensure GL is completed with any commands that might affect this VBO // before we issue OpenCL commands glWaitSync(globalSync, 0, GL_TIMEOUT_IGNORED); //glFinish(); errNum = clEnqueueAcquireGLObjects(commandQueue, 1, &cl_vbo_mem, 0, NULL, NULL); // Queue the kernel up for execution across the array errNum = clEnqueueNDRangeKernel(commandQueue, kernel, 1, NULL,globalWorkSize, localWorkSize, 0, NULL, NULL); if (errNum != CL_SUCCESS) { std::cerr << "Error queuing kernel for execution." << std::endl; } // Release the GL Object // Note, we should ensure OpenCL is finished with any commands that might affect the VBO errNum = clEnqueueReleaseGLObjects(commandQueue, 1, &cl_vbo_mem, 0, NULL, NULL); clFinish(commandQueue); return 0; }
/// // Draw a triangle using the shader pair created in Init() // void Draw ( ESContext *esContext ) { UserData *userData = esContext->userData; // Block the GL server until transform feedback results are completed glWaitSync ( userData->emitSync, 0, GL_TIMEOUT_IGNORED ); glDeleteSync ( userData->emitSync ); // Set the viewport glViewport ( 0, 0, esContext->width, esContext->height ); // Clear the color buffer glClear ( GL_COLOR_BUFFER_BIT ); // Use the program object glUseProgram ( userData->drawProgramObject ); // Load the VBO and vertex attributes SetupVertexAttributes ( esContext, userData->particleVBOs[ userData->curSrcIndex ] ); // Set uniforms glUniform1f ( userData->drawTimeLoc, userData->time ); glUniform4f ( userData->drawColorLoc, 1.0f, 1.0f, 1.0f, 1.0f ); glUniform2f ( userData->drawAccelerationLoc, 0.0f, ACCELERATION ); // Blend particles glEnable ( GL_BLEND ); glBlendFunc ( GL_SRC_ALPHA, GL_ONE ); // Bind the texture glActiveTexture ( GL_TEXTURE0 ); glBindTexture ( GL_TEXTURE_2D, userData->textureId ); // Set the sampler texture unit to 0 glUniform1i ( userData->samplerLoc, 0 ); glDrawArrays ( GL_POINTS, 0, NUM_PARTICLES ); }
void Window::swapBuffers() { if (!_window->setAsCurrentContext()) Log::get() << Log::WARNING << "Window::" << __FUNCTION__ << " - A previous context has not been released." << Log::endl;; glFlush(); glWaitSync(_renderFence, 0, GL_TIMEOUT_IGNORED); // Only one window will wait for vblank, the others draws directly into front buffer auto windowIndex = _swappableWindowsCount.fetch_add(1); glBindFramebuffer(GL_READ_FRAMEBUFFER, _readFbo); // If swap interval is null (meaning no vsync), draw directly to the front buffer in any case #if HAVE_OSX glDrawBuffer(GL_BACK); #else if (windowIndex != 0) glDrawBuffer(GL_FRONT); else glDrawBuffer(GL_BACK); #endif glBlitFramebuffer(0, 0, _windowRect[2], _windowRect[3], 0, 0, _windowRect[2], _windowRect[3], GL_COLOR_BUFFER_BIT, GL_NEAREST); glBindFramebuffer(GL_READ_FRAMEBUFFER, 0); #if HAVE_OSX glfwSwapBuffers(_window->get()); #else if (windowIndex == 0) glfwSwapBuffers(_window->get()); #endif _window->releaseContext(); }
void GpuTimer::start() { QueryPair qp; if (availablePairs.empty()) { glGenQueries(1, &qp.startQuery); glGenQueries(1, &qp.endQuery); } else { qp = availablePairs.front(); availablePairs.pop(); // Reuse old OpenGL ids, no need for additional glGenQueries. } inflightPairs.push(qp); // Create a fence and insert it into the command stream. This fence will be signaled // when the GPU has completed all previously issued commands. glWaitSync will defer // subsequently issued commands (without blocking CPU) until the fence signals. // This prevents overlapped execution of OpenGL calls and should make timing measurements // more precise. For more, see e.g. OpenGL Insights. GLsync sync = glFenceSync(GL_SYNC_GPU_COMMANDS_COMPLETE, 0); glWaitSync(sync, 0, GL_TIMEOUT_IGNORED); glDeleteSync(sync); // Flag for deletion. glQueryCounter(qp.startQuery, GL_TIMESTAMP); }
void Sync::wait(const UnusedMask flags, const GLuint64 timeout) { glWaitSync(m_sync, flags, timeout); }
void GLSync::enqueueWait() { uint64_t timeout = 0xFFFFFFFFFFFFFFFFULL; glWaitSync(m_sync, 0, timeout); m_sync = 0; }
void Fence::wait(unsigned long long timeoutSpecifier) const { glWaitSync(mHandle, 0, timeoutSpecifier); }
void Render(float alpha, float elapsedtime) { float world[16]; float view[16]; float proj[16]; float eye[3]; basiccamera.Animate(alpha); basiccamera.GetViewMatrix(view); basiccamera.GetProjectionMatrix(proj); basiccamera.GetEyePosition(eye); GLVec4Set(uniformDTO.vsuniforms.lightPos, 6, 3, 10, 1); GLVec4Set(uniformDTO.vsuniforms.eyePos, eye[0], eye[1], eye[2], 1); GLMatrixMultiply(uniformDTO.vsuniforms.matViewProj, view, proj); GLMatrixScaling(uniformDTO.vsuniforms.matWorld, OBJECT_SCALE, OBJECT_SCALE, OBJECT_SCALE); switch( rendermethod ) { case 1: GLVec4Set(uniformDTO.fsuniforms.color, 1, 0, 0, 1); break; case 2: GLVec4Set(uniformDTO.fsuniforms.color, 1, 0.5f, 0, 1); break; case 3: GLVec4Set(uniformDTO.fsuniforms.color, 1, 1, 0, 1); break; case 4: GLVec4Set(uniformDTO.fsuniforms.color, 0, 0.75f, 0, 1); break; case 5: GLVec4Set(uniformDTO.fsuniforms.color, 0, 1, 0, 1); break; default: break; } // render pass OpenGLEffect* effect = ((rendermethod > 1) ? effect2 : effect1); GLsync sync = 0; if( rendermethod == 1 ) { effect1->SetMatrix("matViewProj", uniformDTO.vsuniforms.matViewProj); effect1->SetVector("lightPos", uniformDTO.vsuniforms.lightPos); effect1->SetVector("eyePos", uniformDTO.vsuniforms.eyePos); effect1->SetVector("color", uniformDTO.fsuniforms.color); } framebuffer->Set(); glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); effect->Begin(); { for( int i = 0; i < GRID_SIZE; ++i ) { for( int j = 0; j < GRID_SIZE; ++j ) { for( int k = 0; k < GRID_SIZE; ++k ) { if( currentcopy >= UNIFORM_COPIES ) { if( rendermethod == 4 || rendermethod == 2 ) { sync = glFenceSync(GL_SYNC_GPU_COMMANDS_COMPLETE, 0); } else if( rendermethod == 5 ) { glBindBuffer(GL_UNIFORM_BUFFER, uniformbuffer3); glUnmapBuffer(GL_UNIFORM_BUFFER); persistentdata = (EffectUniformBlock*)glMapBufferRange(GL_UNIFORM_BUFFER, 0, UNIFORM_COPIES * sizeof(EffectUniformBlock), GL_MAP_WRITE_BIT|GL_MAP_INVALIDATE_BUFFER_BIT|GL_MAP_PERSISTENT_BIT|GL_MAP_COHERENT_BIT); assert(persistentdata != 0); glBindBuffer(GL_UNIFORM_BUFFER, 0); currentcopy = 0; } } uniformDTO.vsuniforms.matWorld[12] = GRID_SIZE * -0.5f + i; uniformDTO.vsuniforms.matWorld[13] = GRID_SIZE * -0.5f + j; uniformDTO.vsuniforms.matWorld[14] = GRID_SIZE * -0.5f + k; if( rendermethod == 1 ) { effect1->SetMatrix("matWorld", uniformDTO.vsuniforms.matWorld); effect1->CommitChanges(); } else { if( rendermethod == 2 ) { if( sync != 0 ) { glWaitSync(sync, 0, GL_TIMEOUT_IGNORED); glDeleteSync(sync); sync = 0; } glBindBuffer(GL_UNIFORM_BUFFER, uniformbuffer1); glBufferSubData(GL_UNIFORM_BUFFER, 0, sizeof(EffectUniformBlock), &uniformDTO); glBindBufferRange(GL_UNIFORM_BUFFER, 0, uniformbuffer1, 0, sizeof(EffectUniformBlock::VertexUniformData)); glBindBufferRange(GL_UNIFORM_BUFFER, 1, uniformbuffer1, offsetof(EffectUniformBlock, fsuniforms), sizeof(EffectUniformBlock::FragmentUniformData)); } else if( rendermethod == 3 ) { glBindBuffer(GL_UNIFORM_BUFFER, uniformbuffer1); void* data = glMapBufferRange(GL_UNIFORM_BUFFER, 0, sizeof(EffectUniformBlock), GL_MAP_WRITE_BIT|GL_MAP_INVALIDATE_RANGE_BIT); { memcpy(data, &uniformDTO, sizeof(EffectUniformBlock)); } glUnmapBuffer(GL_UNIFORM_BUFFER); glBindBufferRange(GL_UNIFORM_BUFFER, 0, uniformbuffer1, 0, sizeof(EffectUniformBlock::VertexUniformData)); glBindBufferRange(GL_UNIFORM_BUFFER, 1, uniformbuffer1, offsetof(EffectUniformBlock, fsuniforms), sizeof(EffectUniformBlock::FragmentUniformData)); } else if( rendermethod == 4 ) { GLintptr baseoffset = currentcopy * sizeof(EffectUniformBlock); GLbitfield flags = GL_MAP_WRITE_BIT|GL_MAP_INVALIDATE_RANGE_BIT|GL_MAP_UNSYNCHRONIZED_BIT; if( sync != 0 ) { GLenum result = 0; GLbitfield waitflags = GL_SYNC_FLUSH_COMMANDS_BIT; do { result = glClientWaitSync(sync, waitflags, 500000); waitflags = 0; if( result == GL_WAIT_FAILED ) { std::cout << "glClientWaitSync() failed!\n"; break; } } while( result == GL_TIMEOUT_EXPIRED ); glDeleteSync(sync); sync = 0; currentcopy = 0; baseoffset = 0; } glBindBuffer(GL_UNIFORM_BUFFER, uniformbuffer2); void* data = glMapBufferRange(GL_UNIFORM_BUFFER, baseoffset, sizeof(EffectUniformBlock), flags); assert(data != 0); { memcpy(data, &uniformDTO, sizeof(EffectUniformBlock)); } glUnmapBuffer(GL_UNIFORM_BUFFER); glBindBufferRange(GL_UNIFORM_BUFFER, 0, uniformbuffer2, baseoffset, sizeof(EffectUniformBlock::VertexUniformData)); glBindBufferRange(GL_UNIFORM_BUFFER, 1, uniformbuffer2, baseoffset + offsetof(EffectUniformBlock, fsuniforms), sizeof(EffectUniformBlock::FragmentUniformData)); ++currentcopy; } else if( rendermethod == 5 ) { GLintptr baseoffset = currentcopy * sizeof(EffectUniformBlock); memcpy(persistentdata + currentcopy, &uniformDTO, sizeof(EffectUniformBlock)); glBindBufferRange(GL_UNIFORM_BUFFER, 0, uniformbuffer3, baseoffset, sizeof(EffectUniformBlock::VertexUniformData)); glBindBufferRange(GL_UNIFORM_BUFFER, 1, uniformbuffer3, baseoffset + offsetof(EffectUniformBlock, fsuniforms), sizeof(EffectUniformBlock::FragmentUniformData)); ++currentcopy; } } mesh->DrawSubset(0); } } } } effect->End(); framebuffer->Unset(); // present GLMatrixIdentity(world); glDisable(GL_DEPTH_TEST); glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); basic2D->SetMatrix("matTexture", world); basic2D->Begin(); { glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, framebuffer->GetColorAttachment(0)); screenquad->Draw(); } basic2D->End(); // render text glViewport(5, screenheight - 517, 512, 512); glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); float xzplane[4] = { 0, 1, 0, -0.5f }; GLMatrixReflect(world, xzplane); basic2D->SetMatrix("matTexture", world); basic2D->Begin(); { glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, text1); screenquad->Draw(); } basic2D->End(); glEnable(GL_DEPTH_TEST); glDisable(GL_BLEND); glViewport(0, 0, screenwidth, screenheight); // check errors GLenum err = glGetError(); if( err != GL_NO_ERROR ) std::cout << "Error\n"; SwapBuffers(hdc); }
JNIEXPORT void JNICALL Java_org_lwjgl_opengl_GL32_nglWaitSync(JNIEnv *env, jclass clazz, jlong sync, jint flags, jlong timeout, jlong function_pointer) { glWaitSyncPROC glWaitSync = (glWaitSyncPROC)((intptr_t)function_pointer); glWaitSync((GLsync)(intptr_t)sync, flags, timeout); }
//***************************************************************************** // Render //***************************************************************************** void ParticleSystem::Render(Matrix* pMatrixVP) { int hProg = GetShaderProgram(PARTICLE_RENDER_PROGRAM); glWaitSync((GLsync)m_pSync, 0, GL_TIMEOUT_IGNORED); glDeleteSync((GLsync)m_pSync); glUseProgram(hProg); glBindBuffer(GL_ARRAY_BUFFER, m_arVBOs[!m_nUpdateBuffer]); // Assign vertex attributes glVertexAttribPointer(s_hRendPosition, 3, GL_FLOAT, GL_FALSE, PARTICLE_DATA_SIZE, 0); glVertexAttribPointer(s_hRendColor, 4, GL_FLOAT, GL_FALSE, PARTICLE_DATA_SIZE, (void*) 24); glVertexAttribPointer(s_hRendLife, 1, GL_FLOAT, GL_FALSE, PARTICLE_DATA_SIZE, (void*) 40); glVertexAttribPointer(s_hRendSize, 1, GL_FLOAT, GL_FALSE, PARTICLE_DATA_SIZE, (void*) 44); // Enable the attrib arrays glEnableVertexAttribArray(s_hRendPosition); glEnableVertexAttribArray(s_hRendColor); glEnableVertexAttribArray(s_hRendLife); glEnableVertexAttribArray(s_hRendSize); // Setup the uniforms glUniformMatrix4fv(s_hRendMatrixVP, 1, GL_FALSE, pMatrixVP->GetArray()); if (m_pTexture == 0) { glUniform1i(s_hTexType, 0); } else { glUniform1i(s_hTexType, 1); } glUniform1i(s_hRendTexture, 0); // Bind texture if (m_pTexture != 0) { m_pTexture->Bind(); } glDrawArrays(GL_POINTS, 0, m_nParticleCount); }
JNIEXPORT void JNICALL Java_org_lwjgl_opengl_GL32C_nglWaitSync(JNIEnv *__env, jclass clazz, jlong syncAddress, jint flags, jlong timeout) { glWaitSyncPROC glWaitSync = (glWaitSyncPROC)tlsGetFunction(662); intptr_t sync = (intptr_t)syncAddress; UNUSED_PARAM(clazz) glWaitSync(sync, flags, timeout); }
static void waitOnFence(GLsync fence) { glWaitSync(fence, 0, GL_TIMEOUT_IGNORED); glDeleteSync(fence); }
/* * * Core in: * OpenGL : 3.2 * OpenGLES : 3.0 */ void rglWaitSync(void *sync, GLbitfield flags, uint64_t timeout) { #if defined(HAVE_OPENGL) || defined(HAVE_OPENGLES) && defined(HAVE_OPENGLES3) glWaitSync((GLsync)sync, flags, (GLuint64)timeout); #endif }