Repository: wolfpld/tracy
File: TracyOpenCL.hpp (C++)

Code before:

        void Collect()
        {
            ZoneScopedC(Color::Red4);

            if (m_tail == m_head) return;

#ifdef TRACY_ON_DEMAND
            if (!GetProfiler().IsConnected())
            {
                m_head = m_tail = 0;
            }
#endif

            for (; m_tail != m_head; m_tail = (m_tail + 1) % QueryCount)
            {
                EventInfo eventInfo = GetQuery(m_tail);
                cl_int eventStatus;
                cl_int err = clGetEventInfo(eventInfo.event, CL_EVENT_COMMAND_EXECUTION_STATUS, sizeof(cl_int), &eventStatus, nullptr);
                if (err != CL_SUCCESS) {
                    std::ostringstream oss;
                    oss << "clGetEventInfo falied with error code " << err << ", on event " << eventInfo.event << ", skipping...";
                    auto msg = oss.str();
                    TracyMessage(msg.data(), msg.size());
                    if (eventInfo.event == nullptr) {
                        TracyMessageL("A TracyCLZone must be paird with a TracyCLZoneSetEvent, check you code!");
                    }
                    assert(false && "clGetEventInfo failed, maybe a TracyCLZone is not paired with TracyCLZoneSetEvent");
                    continue;
                }
                if (eventStatus != CL_COMPLETE) {
                    TRACY_CL_CHECK_ERROR(clWaitForEvents(1, &eventInfo.event));
                }

                cl_int eventInfoQuery = (eventInfo.phase == EventPhase::Begin)
                    ? CL_PROFILING_COMMAND_START
                    : CL_PROFILING_COMMAND_END;

                cl_ulong eventTimeStamp = 0;
                TRACY_CL_CHECK_ERROR(clGetEventProfilingInfo(eventInfo.event, eventInfoQuery, sizeof(cl_ulong), &eventTimeStamp, nullptr));
                TRACY_CL_ASSERT(eventTimeStamp != 0);

                auto item = Profiler::QueueSerial();
                MemWrite(&item->hdr.type, QueueType::GpuTime);
                MemWrite(&item->gpuTime.gpuTime, (int64_t)eventTimeStamp);
                MemWrite(&item->gpuTime.queryId, (uint16_t)m_tail);
                MemWrite(&item->gpuTime.context, m_contextId);
                Profiler::QueueSerialFinish();

                if (eventInfo.phase == EventPhase::End)
                {

Review comment (bug):
Its it really worth it to emit two error messages? I'd say that a single message should be emitted for event in case something went wrong. 

Also I think that if you want to emit a TraceMessage you don't need the assert in the line below

Code after:

        void Collect()
        {
            ZoneScopedC(Color::Red4);

            if (m_tail == m_head) return;

#ifdef TRACY_ON_DEMAND
            if (!GetProfiler().IsConnected())
            {
                m_head = m_tail = 0;
            }
#endif

            for (; m_tail != m_head; m_tail = (m_tail + 1) % QueryCount)
            {
                EventInfo eventInfo = GetQuery(m_tail);
                cl_int eventStatus;
                cl_int err = clGetEventInfo(eventInfo.event, CL_EVENT_COMMAND_EXECUTION_STATUS, sizeof(cl_int), &eventStatus, nullptr);
                if (err != CL_SUCCESS)
                {
                    std::ostringstream oss;
                    oss << "clGetEventInfo falied with error code " << err << ", on event " << eventInfo.event << ", skipping...";
                    auto msg = oss.str();
                    TracyMessage(msg.data(), msg.size());
                    if (eventInfo.event == nullptr) {
                        TracyMessageL("A TracyCLZone must be paird with a TracyCLZoneSetEvent, check your code!");
                    }
                    assert(false && "clGetEventInfo failed, maybe a TracyCLZone is not paired with TracyCLZoneSetEvent");
                    continue;
                }
                if (eventStatus != CL_COMPLETE) return;

                cl_int eventInfoQuery = (eventInfo.phase == EventPhase::Begin)
                    ? CL_PROFILING_COMMAND_START
                    : CL_PROFILING_COMMAND_END;

                cl_ulong eventTimeStamp = 0;
                err = clGetEventProfilingInfo(eventInfo.event, eventInfoQuery, sizeof(cl_ulong), &eventTimeStamp, nullptr);
                if (err == CL_PROFILING_INFO_NOT_AVAILABLE)
                {
                    TracyMessageL("command queue is not created with CL_QUEUE_PROFILING_ENABLE flag, check your code!");
                    assert(false && "command queue is not created with CL_QUEUE_PROFILING_ENABLE flag");
                }
                else
                    TRACY_CL_CHECK_ERROR(err);

                TRACY_CL_ASSERT(eventTimeStamp != 0);

                auto item = Profiler::QueueSerial();
                MemWrite(&item->hdr.type, QueueType::GpuTime);