bool render() { glm::vec2 WindowSize(this->getWindowSize()); static int FrameIndex = 0; // Bind shared objects glViewportIndexedf(0, 0, 0, WindowSize.x, WindowSize.y); glBindSampler(0, SamplerName); glBindVertexArray(VertexArrayName); // Update a colorbuffer bound as a framebuffer attachement and as a texture glBindFramebuffer(GL_FRAMEBUFFER, FramebufferName); glBindProgramPipeline(PipelineName[pipeline::UPDATE]); glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, FrameIndex ? TextureName[texture::COLORBUFFER] : TextureName[texture::DIFFUSE]); glMemoryBarrier(GL_TEXTURE_UPDATE_BARRIER_BIT | GL_TEXTURE_FETCH_BARRIER_BIT); glDrawArraysInstancedBaseInstance(GL_TRIANGLES, 0, VertexCount, 1, 0); // Blit to framebuffer glBindFramebuffer(GL_FRAMEBUFFER, 0); glBindProgramPipeline(PipelineName[pipeline::BLIT]); glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, TextureName[texture::COLORBUFFER]); glMemoryBarrier(GL_TEXTURE_UPDATE_BARRIER_BIT); glDrawArraysInstancedBaseInstance(GL_TRIANGLES, 0, VertexCount, 1, 0); FrameIndex = (FrameIndex + 1) % 256; return true; }
void render(double currentTime) { _compShader.Use(); glBindImageTexture(0, _images[0], 0, GL_FALSE, 0, GL_READ_ONLY, GL_R32F); glBindImageTexture(1, _images[1], 0, GL_FALSE, 0, GL_WRITE_ONLY, GL_R32F); glDispatchCompute(NUM_ELEMENTS, 1, 1); glMemoryBarrier(GL_SHADER_IMAGE_ACCESS_BARRIER_BIT); glBindImageTexture(0, _images[1], 0, GL_FALSE, 0, GL_READ_ONLY, GL_R32F); glBindImageTexture(1, _images[2], 0, GL_FALSE, 0, GL_WRITE_ONLY, GL_R32F); glDispatchCompute(NUM_ELEMENTS, 1, 1); glMemoryBarrier(GL_SHADER_IMAGE_ACCESS_BARRIER_BIT); glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, _images[2]); _showShader.Use(); glViewport(0, 0, info.windowWidth, info.windowHeight); glBindVertexArray(_vao); glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); }
void stepSimulation() { // Acceleration step glUseProgram(prog_nbody_acc); // Bind nbody-acc input glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 0, ssbo_pos); // Bind nbody-acc output glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 1, ssbo_acc); // Dispatch compute shader int workGroupCountAcc = (N_FOR_VIS - 1) / WORK_GROUP_SIZE_ACC + 1; glDispatchCompute(workGroupCountAcc, 1, 1); // Ensure something like memory coherency. // Otherwise, another invocation of this shader might // launch before this one is done writing values to the ssbo. glMemoryBarrier(GL_SHADER_STORAGE_BARRIER_BIT); // Velocity/position update step glUseProgram(prog_nbody_velpos); // Bind nbody-velpos input glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 0, ssbo_acc); // Bind nbody-velpos input/outputs glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 1, ssbo_pos); glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 2, ssbo_vel); // Dispatch compute shader int workGroupCountVelPos = (N_FOR_VIS - 1) / WORK_GROUP_SIZE_VELPOS + 1; glDispatchCompute(workGroupCountVelPos, 1, 1); glMemoryBarrier(GL_SHADER_STORAGE_BARRIER_BIT); }
void display() { // Update of the array buffer glm::mat4 Perspective = glm::perspective(45.0f, float(Window.Size.x) / float(Window.Size.y), 0.1f, 100.0f); glm::mat4 ViewTranslate = glm::translate(glm::mat4(1.0f), glm::vec3(0.0f, 0.0f, -Window.TranlationCurrent.y * 2.0 - 0.0)); glm::mat4 ViewRotateX = glm::rotate(ViewTranslate, Window.RotationCurrent.y, glm::vec3(1.f, 0.f, 0.f)); glm::mat4 View = glm::rotate(ViewRotateX, Window.RotationCurrent.x, glm::vec3(0.f, 1.f, 0.f)); glm::mat4 Model = glm::mat4(1.0f); glm::mat4 MVP = Perspective * View * Model; for(GLsizei i = (0); i < VertexCount; ++i) (Pointer + i)->Position = MVP * VertexData[i].Position; glBindBuffer(GL_ARRAY_BUFFER, BufferName[buffer::VERTEX]); glFlushMappedBufferRange(GL_ARRAY_BUFFER, 0, sizeof(glf::vertex_v4fv2f) * 4); glBindBuffer(GL_ARRAY_BUFFER, 0); glViewportIndexedf(0, 0, 0, GLfloat(Window.Size.x), GLfloat(Window.Size.y)); glClearBufferfv(GL_COLOR, 0, &glm::vec4(1.0f, 0.5f, 0.0f, 1.0f)[0]); // Bind rendering objects glBindProgramPipeline(PipelineName); glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, TextureName); glBindVertexArray(VertexArrayName); // Make sure the array buffer is uploaded glMemoryBarrier(GL_VERTEX_ATTRIB_ARRAY_BARRIER_BIT); glDrawElementsInstancedBaseVertexBaseInstance( GL_TRIANGLES, ElementCount, GL_UNSIGNED_SHORT, 0, 1, 0, 0); glf::swapBuffers(); }
void MainWindow::paintGL() { /*defined the function to copy */ auto copy_function=[this](auto * fbo) { glMemoryBarrier(GL_ALL_BARRIER_BITS); glBindFramebuffer(GL_FRAMEBUFFER,0); glClearColor(0.1f,0.6f,0.3f,1); glClearDepth(1); glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); glBindFramebuffer(GL_DRAW_FRAMEBUFFER,0); glBindFramebuffer(GL_READ_FRAMEBUFFER,fbo->getFBO()); glReadBuffer(GL_COLOR_ATTACHMENT0); glBlitFramebuffer(0,0,fbo->getWidth(),fbo->getHeight(), 0,height()/2,width()/2,height(),GL_COLOR_BUFFER_BIT , GL_LINEAR); glReadBuffer(GL_COLOR_ATTACHMENT1); glBlitFramebuffer(0,0,fbo->getWidth(),fbo->getHeight(), width()/2,0,width(),height()/2,GL_COLOR_BUFFER_BIT , GL_LINEAR); }; const auto _fbo=thisData->fbo; if ( thisData->isResize ) { glBindFramebuffer(GL_FRAMEBUFFER,0); glClearColor(0.1f,0.6f,0.3f,1); glClear(GL_COLOR_BUFFER_BIT); if ( thisData->fbo ) {copy_function(thisData->fbo.get()); } return; } if ((bool(thisData->fbo)==false)|| (thisData->fbo->getHeight()!=this->height())|| (thisData->fbo->getWidth()!=this->width()) ) { thisData->fbo= std::make_shared<SimpleFrameBuffer>(this->width(),this->height()); } auto * fbo=thisData->fbo.get(); if ((fbo==nullptr)||(fbo->isValid()==false)) { return;/*???*/ } glUseProgram(thisData->program); glBindFramebuffer(GL_FRAMEBUFFER,fbo->getFBO()); glEnable(GL_DEPTH_TEST); fbo->drawBuffer0(); glClearColor(0.1f,0.6f,0.8f,1); glClearDepth(1); glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); glBindVertexArray(thisData->vao); glDrawArrays(GL_TRIANGLES,0,6); glDisable(GL_DEPTH_TEST); fbo->drawBuffer1(); glClearColor(0.1f,0.6f,0.8f,1); glClearDepth(1); glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); glBindVertexArray(thisData->vao); glDrawArrays(GL_TRIANGLES,0,6); copy_function(fbo); }
void ImageProcessingComputeExample::Display(bool auto_redraw) { // Activate the compute program and bind the output texture image glUseProgram(compute_prog); glBindImageTexture(0, input_image, 0, GL_FALSE, 0, GL_READ_ONLY, GL_RGBA32F); glBindImageTexture(1, intermediate_image, 0, GL_FALSE, 0, GL_WRITE_ONLY, GL_RGBA32F); glDispatchCompute(1, 1024, 1); glMemoryBarrier(GL_SHADER_IMAGE_ACCESS_BARRIER_BIT); glBindImageTexture(0, intermediate_image, 0, GL_FALSE, 0, GL_READ_ONLY, GL_RGBA32F); glBindImageTexture(1, output_image, 0, GL_FALSE, 0, GL_WRITE_ONLY, GL_RGBA32F); glDispatchCompute(1, 1024, 1); // Now bind the texture for rendering _from_ glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, output_image); // Clear, select the rendering program and draw a full screen quad glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glUseProgram(render_prog); glDrawArrays(GL_TRIANGLE_FAN, 0, 4); base::Display(); }
// This function runs every frame void renderScene() { // Clear the color buffer and the depth buffer glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear the screen to black glClearColor(0.0, 0.0, 0.0, 1.0); // Ensures that we wait for the compute shader to finish before proceeding, since it runs in parallel. glMemoryBarrier(GL_VERTEX_ATTRIB_ARRAY_BARRIER_BIT); // Tell OpenGL to use the regular shader program you've created. glUseProgram(program); // Sets the MVP matrix uniform in the shader. glUniformMatrix4fv(uniMVP, 1, GL_FALSE, glm::value_ptr(MVP)); // Bind the vao glBindVertexArray(vao); // Draw 256 vertices from the buffer as GL_POINTS which will just draw each vertex individually as a single point. // There are several different drawing modes, GL_TRIANGLES takes every 3 vertices and makes them a triangle. // For reference, GL_TRIANGLE_STRIP would take each additional vertex after the first 3 and consider that a // triangle with the previous 2 vertices (so you could make 2 triangles with 4 vertices, 3 with 5, etc.) // The second parameter is the start index of the array, and the third parameter is the number of vertices to draw. glDrawArrays(GL_POINTS, 0, 256); }
void OpenGLWidget::paintGL() { const qreal retinaScale = devicePixelRatio(); glClear(GL_COLOR_BUFFER_BIT); glMemoryBarrier(GL_SHADER_IMAGE_ACCESS_BARRIER_BIT); majorOffset += offset; fractal.beginRender(); if (fill) glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); else glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); m_vao.bind(); glDrawElements(GL_TRIANGLES, indices.size(), GL_UNSIGNED_INT, 0); m_vao.release(); fractal.endRender(); majorOffset -= offset; if (go) { go = anim.nextFrame(); fractal.computeVariables.at(0)->setValue(); fractal.computeVariables.at(1)->setValue(); fractal.computeVariables.at(fractal.computeVariables.size() - 1)->setValue(); rendermodeLR = ALL; if (anim.checkBox->isChecked()) this->grabFramebuffer().save(anim.file); } }
bool render() { glm::vec2 WindowSize(this->getWindowSize()); glViewportIndexedf(0, 0, 0, WindowSize.x, WindowSize.y); glClearBufferfv(GL_COLOR, 0, &glm::vec4(1.0f)[0]); glBindProgramPipeline(PipelineName); glBindBufferBase(GL_SHADER_STORAGE_BUFFER, semantics::INPUT, BufferName[semantics::INPUT]); glBindBufferBase(GL_SHADER_STORAGE_BUFFER, semantics::OUTPUT, BufferName[semantics::OUTPUT]); glDispatchCompute(1, 1, 1); glMemoryBarrier(GL_ALL_BARRIER_BITS); std::vector<GLuint> Results(DataSize); glBindBuffer(GL_COPY_READ_BUFFER, BufferName[semantics::OUTPUT]); glm::uint* Pointer = (glm::uint*)glMapBufferRange(GL_COPY_READ_BUFFER, 0, sizeof(glm::uint) * Results.size(), GL_MAP_READ_BIT); memcpy(&Results[0], Pointer, Results.size() * sizeof(glm::uint)); glUnmapBuffer(GL_COPY_READ_BUFFER); std::array<glm::uint, 14> const Expect{0u, 2u, 3u, 4u, 6u, 7u, 8u}; for(std::size_t i = 0, n = Expect.size(); i < n; ++i) if(Expect[i] != Results[i]) return false; return true; }
void Renderer::Render() { glMemoryBarrier(GL_SHADER_IMAGE_ACCESS_BARRIER_BIT); mShader.Use(); glBindVertexArray(mVaoID); glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, mHeightMapCS->GetHeightMapTextureId()); glActiveTexture(GL_TEXTURE1); glBindTexture(GL_TEXTURE_BUFFER, mModelMatrixBuffer.GetTextureId()); glActiveTexture(GL_TEXTURE2); glBindTexture(mCubeMapTexture->GetTarget(), mCubeMapTexture->GetResourceId()); glActiveTexture(GL_TEXTURE3); glBindTexture(GL_TEXTURE_2D, mOceanColorTexture->GetResourceId()); glActiveTexture(GL_TEXTURE4); glBindTexture(GL_TEXTURE_2D, mHeightMapCS->GetNormalMapTextureId()); glDrawArraysInstanced(GL_PATCHES, 0, 4, mPatchCount.x * mPatchCount.y * mMapCount); glBindVertexArray(0); mShader.UnUse(); }
void ManagerImpl::cull( const GroupHandle& group, const ResultHandle& result, const dp::math::Mat44f& viewProjection ) { dp::util::ProfileEntry p("cull"); const GroupImplHandle& groupImpl = dp::util::smart_cast<GroupImpl>(group); const ResultBitSetHandle& resultImpl = dp::util::smart_cast<ResultBitSet>(result); dp::math::Mat44f vp = viewProjection; dp::math::Mat44f modelViewProjection; resultImpl->getChangedGroup()->clearObjects(); initializeComputeShader(); groupImpl->update( WORKGROUP_SIZE ); // initialize output buffer size_t numberOfWorkingGroups = (groupImpl->getObjectCount() + WORKGROUP_SIZE - 1) / WORKGROUP_SIZE; dp::gl::ProgramUseGuard( m_program ); glUniformMatrix4fv( m_uniformViewProjection, 1, false, viewProjection[0].getPtr() ); glBindBufferBase( GL_SHADER_STORAGE_BUFFER, OBJECT_BINDING, groupImpl->getInputBuffer() ); glBindBufferBase( GL_SHADER_STORAGE_BUFFER, MATRIX_BINDING, groupImpl->getMatrixBuffer() ); glBindBufferBase( GL_SHADER_STORAGE_BUFFER, VISIBILITY_BINDING, groupImpl->getOutputBuffer() ); glDispatchCompute( static_cast<GLuint>(numberOfWorkingGroups), 1, 1 ); //glMemoryBarrier( GL_BUFFER_UPDATE_BARRIER_BIT ); // TODO This is way too slow to use, but correct. glMemoryBarrier( GL_SHADER_STORAGE_BARRIER_BIT ); dp::gl::MapBuffer<dp::util::Uint32> visibleShader( groupImpl->getOutputBuffer(), GL_SHADER_STORAGE_BUFFER, GL_READ_ONLY ); resultImpl->updateChanged( visibleShader ); }
void RadixSort::SortBits (int bits) { // pass current bit shift to the shader programs glProgramUniform1i (counting.get (), counting_bitshift, bits); glProgramUniform1i (globalsort.get (), globalsort_bitshift, bits); // set buffer bindings { GLuint bufs[4] = { buffer, prefixsums, blocksums.front (), result }; glBindBuffersBase (GL_SHADER_STORAGE_BUFFER, 0, 4, bufs); } // counting counting.Use (); glDispatchCompute (numblocks, 1, 1); glMemoryBarrier (GL_SHADER_STORAGE_BARRIER_BIT); // create block sums level by level blockscan.Use (); uint32_t numblocksums = (4 * numblocks) / blocksize; for (int i = 0; i < blocksums.size () - 1; i++) { glBindBuffersBase (GL_SHADER_STORAGE_BUFFER, 0, 2, &blocksums[i]); glDispatchCompute (numblocksums > 0 ? numblocksums : 1, 1, 1); numblocksums /= blocksize; glMemoryBarrier (GL_SHADER_STORAGE_BARRIER_BIT); } // add block sums level by level (in reversed order) addblocksum.Use (); for (int i = blocksums.size () - 3; i >= 0; i--) { uint32_t numblocksums = (4 * numblocks) / intpow (blocksize, i + 1); glBindBuffersBase (GL_SHADER_STORAGE_BUFFER, 0, 2, &blocksums[i]); glDispatchCompute (numblocksums > 0 ? numblocksums : 1, 1, 1); glMemoryBarrier (GL_SHADER_STORAGE_BARRIER_BIT); } // map values to their global position in the output buffer { GLuint bufs[2] = { buffer, prefixsums }; glBindBuffersBase (GL_SHADER_STORAGE_BUFFER, 0, 2, bufs); } globalsort.Use (); glDispatchCompute (numblocks, 1, 1); glMemoryBarrier (GL_SHADER_STORAGE_BARRIER_BIT); }
int main() { GlRunner *runner = new GlRunner(RenderCB); int w, h, c; unsigned char *datum = SOIL_load_image("materials/stormtrooper.jpg", &w, &h, &c, SOIL_LOAD_RGBA); // init normal source texture glGenTextures(1, &srcObj); glBindTexture(GL_TEXTURE_2D, srcObj); glTexStorage2D(GL_TEXTURE_2D, 1, GL_RGBA8, _sz, _sz); glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, _sz, _sz, GL_RGBA, GL_UNSIGNED_BYTE, datum); SOIL_free_image_data(datum); // compute the writeObj texture as destination texture glGenTextures(1, &writeObj); glBindTexture(GL_TEXTURE_2D, writeObj); glTexStorage2D(GL_TEXTURE_2D, 1, GL_RGBA8, _sz, _sz); glBindImageTexture(0, srcObj, 0, GL_FALSE, 0, GL_READ_ONLY, GL_RGBA8); glBindImageTexture(1, writeObj, 0, GL_FALSE, 0, GL_WRITE_ONLY, GL_RGBA8); GLuint CS = runner->BuildShaderProgram("shaders/compute_large_image.comp", GL_COMPUTE_SHADER); GLuint CSPPO = runner->BuildProgramPipeline(); glUseProgramStages(CSPPO, GL_COMPUTE_SHADER_BIT, CS); glDispatchCompute(1, _sz, 1); glMemoryBarrier(GL_SHADER_IMAGE_ACCESS_BARRIER_BIT); // compute the writeObj texture as source texture glGenTextures(1, &dstObj); glBindTexture(GL_TEXTURE_2D, dstObj); glTexStorage2D(GL_TEXTURE_2D, 1, GL_RGBA8, _sz, _sz); glBindImageTexture(0, writeObj, 0, GL_FALSE, 0, GL_READ_ONLY, GL_RGBA8); glBindImageTexture(1, dstObj, 0, GL_FALSE, 0, GL_WRITE_ONLY, GL_RGBA8); glDispatchCompute(1, _sz, 1); glMemoryBarrier(GL_SHADER_IMAGE_ACCESS_BARRIER_BIT); runner->OnRender(); return 0; }
bool GLComputeEvaluator::EvalStencils( GLuint srcBuffer, BufferDescriptor const &srcDesc, GLuint dstBuffer, BufferDescriptor const &dstDesc, GLuint duBuffer, BufferDescriptor const &duDesc, GLuint dvBuffer, BufferDescriptor const &dvDesc, GLuint sizesBuffer, GLuint offsetsBuffer, GLuint indicesBuffer, GLuint weightsBuffer, GLuint duWeightsBuffer, GLuint dvWeightsBuffer, int start, int end) const { if (!_stencilKernel.program) return false; int count = end - start; if (count <= 0) { return true; } glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 0, srcBuffer); glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 1, dstBuffer); glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 2, duBuffer); glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 3, dvBuffer); glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 4, sizesBuffer); glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 5, offsetsBuffer); glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 6, indicesBuffer); glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 7, weightsBuffer); if (duWeightsBuffer) glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 8, duWeightsBuffer); if (dvWeightsBuffer) glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 9, dvWeightsBuffer); glUseProgram(_stencilKernel.program); glUniform1i(_stencilKernel.uniformStart, start); glUniform1i(_stencilKernel.uniformEnd, end); glUniform1i(_stencilKernel.uniformSrcOffset, srcDesc.offset); glUniform1i(_stencilKernel.uniformDstOffset, dstDesc.offset); if (_stencilKernel.uniformDuDesc > 0) { glUniform3i(_stencilKernel.uniformDuDesc, duDesc.offset, duDesc.length, duDesc.stride); } if (_stencilKernel.uniformDvDesc > 0) { glUniform3i(_stencilKernel.uniformDvDesc, dvDesc.offset, dvDesc.length, dvDesc.stride); } glDispatchCompute((count + _workGroupSize - 1) / _workGroupSize, 1, 1); glUseProgram(0); glMemoryBarrier(GL_TEXTURE_FETCH_BARRIER_BIT); for (int i = 0; i < 10; ++i) { glBindBufferBase(GL_SHADER_STORAGE_BUFFER, i, 0); } return true; }
void App::gaussiate() { glUseProgram(gaussianFilter.getProgramID()); glBindImageTexture(0, _deferredProgram.getTexture(), 0, GL_FALSE, 0, GL_READ_ONLY, GL_RGBA32F); glBindImageTexture(1, gaussianTexture, 0, GL_FALSE, 0, GL_WRITE_ONLY, GL_RGBA32F); glDispatchCompute(135, 90, 1); glMemoryBarrier(GL_SHADER_IMAGE_ACCESS_BARRIER_BIT); glUseProgram(0); }
/* * * Core in: * OpenGL : 4.2 * OpenGLES : 3.1 */ void rglMemoryBarrier( GLbitfield barriers) { #if !defined(HAVE_OPENGLES) || defined(HAVE_OPENGLES3) && defined(HAVE_OPENGLES_3_1) glMemoryBarrier(barriers); #else printf("WARNING! Not implemented.\n"); #endif }
/*!**************************************************************************** @Function Update @Input dt Elapsed time from last iteration (frame) @Description Advances the simulation by dt. Invalidates the following OpenGL state:Current program, GL_UNIFORM_BUFFER binding ******************************************************************************/ void ParticleSystemGPU::Update(float dt) { if (dt == 0) { return; } dt *= 0.001f; GLuint numGroups = m_ui32NumParticles / m_ui32WorkgroupSize; m_ParticleConfigData.fDt = dt; m_ParticleConfigData.fTotalTime += dt; glBindBuffer(GL_UNIFORM_BUFFER, m_ParticleConfigUbo); glBufferData(GL_UNIFORM_BUFFER, sizeof(m_ParticleConfigData), &m_ParticleConfigData, GL_STREAM_DRAW); glUseProgram(m_glProgram); glMemoryBarrier(GL_SHADER_STORAGE_BARRIER_BIT); glDispatchCompute(numGroups, 1, 1); glMemoryBarrier(GL_VERTEX_ATTRIB_ARRAY_BARRIER_BIT); }
/** 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 glBufferImpl::readData(size_t offsetInBytes, size_t rangeInBytes, const bufferPtr data) { if (_target == GL_ATOMIC_COUNTER_BUFFER) { glMemoryBarrier(MemoryBarrierMask::GL_ATOMIC_COUNTER_BARRIER_BIT); } if (_mappedBuffer && waitRange(offsetInBytes, rangeInBytes, true)) { if (_target != GL_ATOMIC_COUNTER_BUFFER) { glMemoryBarrier(MemoryBarrierMask::GL_CLIENT_MAPPED_BUFFER_BARRIER_BIT); } std::memcpy(data, ((Byte*)(_mappedBuffer)+offsetInBytes), rangeInBytes); } else { void* bufferData = glMapNamedBufferRange(_handle, offsetInBytes, rangeInBytes, BufferAccessMask::GL_MAP_READ_BIT); if (bufferData != nullptr) { std::memcpy(data, ((Byte*)(bufferData)+offsetInBytes), rangeInBytes); } glUnmapNamedBuffer(_handle); } }
void CS_Renderer::Draw() { vao->Bind(); Posbuffer->BindBufferBase(0); VelBuffer->BindBufferBase(1); AccBuffer->BindBufferBase(2); double StartTime = glfwGetTime(); glDisable(GL_DEPTH_TEST); glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE); int windowWidth, windowHeight; glfwGetCursorPos(g_GraphicsSys->GetCurrentWindow().glfw_GetWindow(), &MousePosX, &MousePosY); glfwGetWindowSize(g_GraphicsSys->GetCurrentWindow().glfw_GetWindow(), &windowWidth, &windowHeight); destPos.x = (float)(MousePosX / (windowWidth)-0.5f) * 2.0f; destPos.y = (float)((windowHeight - MousePosY) / windowHeight - 0.5f) * 2.0f; computeshader->Use(); computeshader->uni1f("dTime", 10 * 0.1f); computeshader->uni3f("destPos", destPos.x, destPos.y, 0); computeshader->uni1f("et", (float)glfwGetTime()); int workingGroups = NumParticles / 16; computeshader->Dispatch_Compute(workingGroups + 2, 1, 1); computeshader->Disable(); glMemoryBarrier(GL_VERTEX_ATTRIB_ARRAY_BARRIER_BIT); glUseProgram(shader); GLuint colLocation = glGetUniformLocation(shader, "Color"); glUniform4f(colLocation, colVec.x, colVec.y, colVec.z, 1.0f); glEnable(GL_PROGRAM_POINT_SIZE); texture->TexBind(); glUniform1i(glGetUniformLocation(shader, "image"), 0); GLuint posAttrib = glGetAttribLocation(shader, "position"); glm::mat4 mvp = g_GraphicsSys->GetCurrentCamera().getProjectionMatrix() * g_GraphicsSys->GetCurrentCamera().getWorldToViewMatrix(); glUniformMatrix4fv(glGetUniformLocation(shader, "mvp"), 1, GL_FALSE, glm::value_ptr(mvp)); glBindBuffer(GL_ARRAY_BUFFER, Posbuffer->Get_POS()); glVertexAttribPointer(posAttrib, 4, GL_FLOAT, GL_FALSE, 0, 0); glEnableVertexAttribArray(posAttrib); glPointSize(1); glDrawArrays(GL_POINTS, 0, NumParticles); texture->unBind(); glUseProgram(0); dTime = (float)(glfwGetTime() - StartTime); Color_Random(); }