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

#pragma once

#include "BSDF.ih"

OSPRAY_BEGIN_ISPC_NAMESPACE

struct Scale
{
  BSDF super;

  const varying BSDF *uniform base;
  float factor;
};

inline BSDF_EvalRes Scale_eval(const varying BSDF *uniform super,
    const vec3f &wo,
    const vec3f &wi,
    const uniform FeatureFlagsHandler &ffh)
{
  const varying Scale *uniform self = (const varying Scale *uniform)super;

  BSDF_EvalRes res = BSDF_dispatch_eval_base(self->base, wo, wi, ffh);
  res.value = res.value * self->factor;

  return res;
}

inline BSDF_SampleRes Scale_sample(const varying BSDF *uniform super,
    const vec3f &wo,
    const vec2f &s,
    float ss,
    const uniform FeatureFlagsHandler &ffh)
{
  const varying Scale *uniform self = (const varying Scale *uniform)super;

  BSDF_SampleRes res = BSDF_dispatch_sample_base(self->base, wo, s, ss, ffh);
  res.weight = res.weight * self->factor;

  return res;
}

inline void Scale_Constructor(varying Scale *uniform self,
    const varying linear3f *uniform frame,
    const varying BSDF *uniform base,
    float factor)
{
  BSDF_Constructor(&self->super,
      base->albedo * factor,
      base->bsdfType,
      BSDF_TYPE_SCALE,
#ifdef OSPRAY_TARGET_SYCL
      NULL,
      NULL,
#else
      Scale_eval,
      Scale_sample,
#endif
      frame);
  self->base = base;
  self->factor = factor;
}

inline varying BSDF *uniform Scale_create(uniform ShadingContext *uniform ctx,
    const varying BSDF *uniform base,
    float factor)
{
  varying Scale *uniform self =
      (varying Scale * uniform) ShadingContext_alloc(ctx, sizeof(Scale));
  Scale_Constructor(self,
      LinearSpace3f_create(ctx, make_LinearSpace3f_varying_identity()),
      base,
      factor);
  return &self->super;
}

OSPRAY_END_ISPC_NAMESPACE
