Path: blob/21.2-virgl/src/gallium/frontends/clover/core/object.hpp
4572 views
//1// Copyright 2013 Francisco Jerez2//3// Permission is hereby granted, free of charge, to any person obtaining a4// copy of this software and associated documentation files (the "Software"),5// to deal in the Software without restriction, including without limitation6// the rights to use, copy, modify, merge, publish, distribute, sublicense,7// and/or sell copies of the Software, and to permit persons to whom the8// Software is furnished to do so, subject to the following conditions:9//10// The above copyright notice and this permission notice shall be included in11// all copies or substantial portions of the Software.12//13// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR14// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,15// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL16// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR17// OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,18// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR19// OTHER DEALINGS IN THE SOFTWARE.20//2122#ifndef CLOVER_CORE_OBJECT_HPP23#define CLOVER_CORE_OBJECT_HPP2425#include <cassert>26#include <functional>27#include <vector>2829#include "CL/cl.h"3031#include "core/error.hpp"32#include "core/property.hpp"33#include "api/dispatch.hpp"34#include "util/macros.h"3536///37/// Main namespace of the CL gallium frontend.38///39namespace clover {40///41/// Class that represents a CL API object.42///43template<typename T, typename S>44struct descriptor {45typedef T object_type;46typedef S descriptor_type;4748descriptor() : dispatch(&_dispatch) {49static_assert(std::is_standard_layout<descriptor_type>::value,50"ICD requires CL API objects to be standard layout.");51}5253const cl_icd_dispatch *dispatch;54};5556struct default_tag;57struct allow_empty_tag;58struct wait_list_tag;59struct property_list_tag;6061namespace detail {62template<typename T, typename D>63struct descriptor_traits {64typedef T object_type;6566static void67validate(D *d) {68auto o = static_cast<typename D::object_type *>(d);69if (!o || o->dispatch != &_dispatch ||70!dynamic_cast<object_type *>(o))71throw invalid_object_error<T>();72}7374static void75validate_list(D * const *ds, size_t n) {76if (!ds || !n)77throw error(CL_INVALID_VALUE);78}79};8081template<typename D>82struct descriptor_traits<default_tag, D> {83typedef typename D::object_type object_type;8485static void86validate(D *d) {87if (!d || d->dispatch != &_dispatch)88throw invalid_object_error<object_type>();89}9091static void92validate_list(D *const *ds, size_t n) {93if (!ds || !n)94throw error(CL_INVALID_VALUE);95}96};9798template<typename D>99struct descriptor_traits<allow_empty_tag, D> {100typedef typename D::object_type object_type;101102static void103validate(D *d) {104if (!d || d->dispatch != &_dispatch)105throw invalid_object_error<object_type>();106}107108static void109validate_list(D *const *ds, size_t n) {110if (bool(ds) != bool(n))111throw error(CL_INVALID_VALUE);112}113};114115template<typename D>116struct descriptor_traits<wait_list_tag, D> {117typedef typename D::object_type object_type;118119static void120validate(D *d) {121if (!d || d->dispatch != &_dispatch)122throw invalid_wait_list_error();123}124125static void126validate_list(D *const *ds, size_t n) {127if (bool(ds) != bool(n))128throw invalid_wait_list_error();129}130};131}132133///134/// Get a Clover object from an API object performing object135/// validation.136///137/// \a T can either be the Clover object type to return or a \a tag138/// object to select some special validation behavior by means of a139/// specialization of the detail::descriptor_traits template. The140/// default behavior is to infer the most general Clover object141/// type for the given API object.142///143template<typename T = default_tag, typename D>144typename detail::descriptor_traits<T, D>::object_type &145obj(D *d) {146detail::descriptor_traits<T, D>::validate(d);147148return static_cast<149typename detail::descriptor_traits<T, D>::object_type &>(*d);150}151152///153/// Get a pointer to a Clover object from an API object performing154/// object validation. Returns \c NULL if its argument is \c NULL.155///156/// \sa obj157///158template<typename T = default_tag, typename D>159typename detail::descriptor_traits<T, D>::object_type *160pobj(D *d) {161if (d)162detail::descriptor_traits<T, D>::validate(d);163164return static_cast<165typename detail::descriptor_traits<T, D>::object_type *>(d);166}167168///169/// Get an API object from a Clover object.170///171template<typename O>172typename O::descriptor_type *173desc(O &o) {174return static_cast<typename O::descriptor_type *>(&o);175}176177///178/// Get an API object from a pointer to a Clover object.179///180template<typename O>181typename O::descriptor_type *182desc(O *o) {183return static_cast<typename O::descriptor_type *>(o);184}185186///187/// Get a range of Clover objects from a range of API objects188/// performing object validation.189///190/// \sa obj191///192template<typename T = default_tag, typename D>193ref_vector<typename detail::descriptor_traits<T, D>::object_type>194objs(D *const *ds, size_t n) {195detail::descriptor_traits<T, D>::validate_list(ds, n);196return map(obj<T, D>, range(ds, n));197}198199///200/// Get a range of API objects from a range of Clover objects.201///202template<typename Os>203std::vector<typename Os::value_type::descriptor_type *>204descs(const Os &os) {205return map([](typename Os::value_type &o) {206return desc(o);207}, os);208}209}210211struct _cl_context :212public clover::descriptor<clover::context, _cl_context> {};213214struct _cl_device_id :215public clover::descriptor<clover::device, _cl_device_id> {};216217struct _cl_event :218public clover::descriptor<clover::event, _cl_event> {};219220struct _cl_kernel :221public clover::descriptor<clover::kernel, _cl_kernel> {};222223struct _cl_mem :224public clover::descriptor<clover::memory_obj, _cl_mem> {};225226struct _cl_platform_id :227public clover::descriptor<clover::platform, _cl_platform_id> {};228229struct _cl_program :230public clover::descriptor<clover::program, _cl_program> {};231232struct _cl_command_queue :233public clover::descriptor<clover::command_queue, _cl_command_queue> {};234235struct _cl_sampler :236public clover::descriptor<clover::sampler, _cl_sampler> {};237238#endif239240241