How it works: GPU writes a buffer and CPU checks the buffer value to be changed. The buffer is accessible for writing by GPU and reading by CPU simultaneously - persistent buffer or buffer across shild context can be used for that.
| 102 | // simultaneously - persistent buffer or buffer across shild context can be used |
| 103 | // for that. |
| 104 | Status GlShaderSync::Wait() { |
| 105 | if (!flag_buffer_.is_valid()) { |
| 106 | return UnavailableError("GlShaderSync is not initialized."); |
| 107 | } |
| 108 | RETURN_IF_ERROR(flag_buffer_.BindToIndex(0)); |
| 109 | volatile int* flag_ptr_ = reinterpret_cast<int*>(flag_buffer_.data()); |
| 110 | *flag_ptr_ = 0; |
| 111 | RETURN_IF_ERROR(flag_program_.Dispatch({1, 1, 1})); |
| 112 | // glFlush must be called to upload GPU task. Adreno won't start executing |
| 113 | // the task without glFlush. |
| 114 | glFlush(); |
| 115 | // Wait for the value is being updated by the shader. |
| 116 | while (*flag_ptr_ != 1) { |
| 117 | } |
| 118 | return OkStatus(); |
| 119 | } |
| 120 | |
| 121 | } // namespace gl |
| 122 | } // namespace gpu |