// Copyright 2020 Intel Corporation
// SPDX-License-Identifier: Apache-2.0

#pragma once

#include "../../observer/ObserverRegistry.ih"
#include "VdbSamplerShared.h"

inline uniform bool VdbLeafAccessObserver_isObservable(
    const VdbSamplerShared *uniform sampler)
{
  assert(sampler);
  return sampler->leafAccessObservers &&
         (((ObserverRegistry * uniform) sampler->leafAccessObservers)->size >
          0);
}

#define __define_leaf_access_observer(univary)                                 \
  inline void VdbLeafAccessObserver_observe_##univary(                         \
      const VdbSamplerShared *uniform sampler, const univary uint32 leafIndex) \
  {                                                                            \
    assert(sampler->leafAccessObservers);                                      \
    ObserverRegistry *uniform registry =                                       \
        ((ObserverRegistry * uniform) sampler->leafAccessObservers);           \
    for (uniform size_t i = 0; i < registry->size; ++i) {                      \
      uint32 *uniform accessBuffer = ((uint32 * uniform) registry->data[i]);   \
      /* NOTE: this is not synchronized between threads! */                    \
      accessBuffer[leafIndex] = 1;                                             \
    }                                                                          \
  }

__define_leaf_access_observer(uniform)
__define_leaf_access_observer(varying)

#undef __define_leaf_access_observer
