diff options
author | Francisco Jerez <currojerez@riseup.net> | 2013-10-06 13:51:01 -0700 |
---|---|---|
committer | Mark Mueller <MarkKMueller@gmail.com> | 2013-10-28 11:06:47 -0700 |
commit | 5834ef3c71692fcd2d28ad8c0fc19ae3542b34a5 (patch) | |
tree | 5ddea2bb9bb98842d5d7c7a610697541f4b214f4 | |
parent | b7c003244ba2f3f529485849c582b9d05ed88384 (diff) |
clover: Define helper classes for the new object model.
Tested-by: Tom Stellard <thomas.stellard@amd.com>
20 files changed, 398 insertions, 107 deletions
diff --git a/src/gallium/state_trackers/clover/Makefile.sources b/src/gallium/state_trackers/clover/Makefile.sources index 96e7a4168f..b8231e2cbd 100644 --- a/src/gallium/state_trackers/clover/Makefile.sources +++ b/src/gallium/state_trackers/clover/Makefile.sources @@ -9,6 +9,8 @@ CPP_SOURCES := \ util/pointer.hpp \ util/range.hpp \ util/tuple.hpp \ + core/object.hpp \ + core/error.hpp \ core/compiler.hpp \ core/geometry.hpp \ core/device.hpp \ diff --git a/src/gallium/state_trackers/clover/api/util.hpp b/src/gallium/state_trackers/clover/api/util.hpp index e94b4b26a3..c8f786e96a 100644 --- a/src/gallium/state_trackers/clover/api/util.hpp +++ b/src/gallium/state_trackers/clover/api/util.hpp @@ -23,15 +23,11 @@ #ifndef CLOVER_API_UTIL_HPP #define CLOVER_API_UTIL_HPP -#include <cstdint> -#include <cstring> -#include <algorithm> -#include <map> +#include <cassert> -#include "core/base.hpp" +#include "core/error.hpp" #include "core/property.hpp" #include "util/algorithm.hpp" -#include "pipe/p_compiler.h" namespace clover { /// diff --git a/src/gallium/state_trackers/clover/core/base.hpp b/src/gallium/state_trackers/clover/core/base.hpp deleted file mode 100644 index ca42334891..0000000000 --- a/src/gallium/state_trackers/clover/core/base.hpp +++ /dev/null @@ -1,59 +0,0 @@ -// -// Copyright 2012 Francisco Jerez -// -// Permission is hereby granted, free of charge, to any person obtaining a -// copy of this software and associated documentation files (the "Software"), -// to deal in the Software without restriction, including without limitation -// the rights to use, copy, modify, merge, publish, distribute, sublicense, -// and/or sell copies of the Software, and to permit persons to whom the -// Software is furnished to do so, subject to the following conditions: -// -// The above copyright notice and this permission notice shall be included in -// all copies or substantial portions of the Software. -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL -// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR -// OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, -// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR -// OTHER DEALINGS IN THE SOFTWARE. -// - -#ifndef CLOVER_CORE_BASE_HPP -#define CLOVER_CORE_BASE_HPP - -#include <stdexcept> -#include <atomic> -#include <cassert> -#include <tuple> -#include <vector> -#include <functional> - -#include "CL/cl.h" -#include "util/pointer.hpp" - -/// -/// Main namespace of the CL state tracker. -/// -namespace clover { - /// - /// Class that represents an error that can be converted to an - /// OpenCL status code. - /// - class error : public std::runtime_error { - public: - error(cl_int code, std::string what = "") : - std::runtime_error(what), code(code) { - } - - cl_int get() const { - return code; - } - - protected: - cl_int code; - }; -} - -#endif diff --git a/src/gallium/state_trackers/clover/core/compiler.hpp b/src/gallium/state_trackers/clover/core/compiler.hpp index 5edb056ec5..49cd0226f6 100644 --- a/src/gallium/state_trackers/clover/core/compiler.hpp +++ b/src/gallium/state_trackers/clover/core/compiler.hpp @@ -24,37 +24,13 @@ #define CLOVER_CORE_COMPILER_HPP #include "util/compat.hpp" +#include "core/error.hpp" #include "core/module.hpp" #include "pipe/p_defines.h" namespace clover { - class build_error { - public: - build_error(const compat::string &log) : log(log) { - } - - virtual ~build_error() { - } - - compat::string what() { - return log; - } - - private: - compat::vector<char> log; - }; - - class invalid_option_error { - public: - invalid_option_error() { - } - - virtual ~invalid_option_error() { - } - }; - module compile_program_llvm(const compat::string &source, - enum pipe_shader_ir ir, + pipe_shader_ir ir, const compat::string &target, const compat::string &opts); diff --git a/src/gallium/state_trackers/clover/core/context.hpp b/src/gallium/state_trackers/clover/core/context.hpp index 62ad6ea0da..a4902dd7b9 100644 --- a/src/gallium/state_trackers/clover/core/context.hpp +++ b/src/gallium/state_trackers/clover/core/context.hpp @@ -23,7 +23,7 @@ #ifndef CLOVER_CORE_CONTEXT_HPP #define CLOVER_CORE_CONTEXT_HPP -#include "core/base.hpp" +#include "core/object.hpp" #include "core/device.hpp" namespace clover { diff --git a/src/gallium/state_trackers/clover/core/device.hpp b/src/gallium/state_trackers/clover/core/device.hpp index a310001c74..1dd47487ca 100644 --- a/src/gallium/state_trackers/clover/core/device.hpp +++ b/src/gallium/state_trackers/clover/core/device.hpp @@ -26,7 +26,7 @@ #include <set> #include <vector> -#include "core/base.hpp" +#include "core/object.hpp" #include "core/format.hpp" #include "pipe-loader/pipe_loader.h" diff --git a/src/gallium/state_trackers/clover/core/error.hpp b/src/gallium/state_trackers/clover/core/error.hpp new file mode 100644 index 0000000000..2b0b6c5e85 --- /dev/null +++ b/src/gallium/state_trackers/clover/core/error.hpp @@ -0,0 +1,196 @@ +// +// Copyright 2013 Francisco Jerez +// +// Permission is hereby granted, free of charge, to any person obtaining a +// copy of this software and associated documentation files (the "Software"), +// to deal in the Software without restriction, including without limitation +// the rights to use, copy, modify, merge, publish, distribute, sublicense, +// and/or sell copies of the Software, and to permit persons to whom the +// Software is furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL +// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR +// OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, +// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR +// OTHER DEALINGS IN THE SOFTWARE. +// + +#ifndef CLOVER_CORE_ERROR_HPP +#define CLOVER_CORE_ERROR_HPP + +#include "CL/cl.h" + +#include "util/compat.hpp" + +namespace clover { + typedef struct _cl_command_queue command_queue; + typedef struct _cl_context context; + typedef struct _cl_device_id device; + typedef struct _cl_event event; + class hard_event; + class soft_event; + typedef struct _cl_kernel kernel; + typedef struct _cl_mem memory_obj; + class buffer; + class root_buffer; + class sub_buffer; + class image; + class image2d; + class image3d; + typedef struct _cl_platform_id platform; + typedef struct _cl_program program; + typedef struct _cl_sampler sampler; + + /// + /// Class that represents an error that can be converted to an + /// OpenCL status code. + /// + class error : public compat::runtime_error { + public: + error(cl_int code, compat::string what = "") : + compat::runtime_error(what), code(code) { + } + + cl_int get() const { + return code; + } + + protected: + cl_int code; + }; + + class build_error : public error { + public: + build_error(const compat::string &log) : + error(CL_BUILD_PROGRAM_FAILURE, log) { + } + }; + + template<typename O> + class invalid_object_error; + + template<> + class invalid_object_error<command_queue> : public error { + public: + invalid_object_error(std::string what = "") : + error(CL_INVALID_COMMAND_QUEUE, what) {} + }; + + template<> + class invalid_object_error<context> : public error { + public: + invalid_object_error(std::string what = "") : + error(CL_INVALID_CONTEXT, what) {} + }; + + template<> + class invalid_object_error<device> : public error { + public: + invalid_object_error(std::string what = "") : + error(CL_INVALID_DEVICE, what) {} + }; + + template<> + class invalid_object_error<event> : public error { + public: + invalid_object_error(std::string what = "") : + error(CL_INVALID_EVENT, what) {} + }; + + template<> + class invalid_object_error<soft_event> : public error { + public: + invalid_object_error(std::string what = "") : + error(CL_INVALID_EVENT, what) {} + }; + + template<> + class invalid_object_error<kernel> : public error { + public: + invalid_object_error(std::string what = "") : + error(CL_INVALID_KERNEL, what) {} + }; + + template<> + class invalid_object_error<memory_obj> : public error { + public: + invalid_object_error(std::string what = "") : + error(CL_INVALID_MEM_OBJECT, what) {} + }; + + template<> + class invalid_object_error<buffer> : public error { + public: + invalid_object_error(std::string what = "") : + error(CL_INVALID_MEM_OBJECT, what) {} + }; + + template<> + class invalid_object_error<root_buffer> : public error { + public: + invalid_object_error(std::string what = "") : + error(CL_INVALID_MEM_OBJECT, what) {} + }; + + template<> + class invalid_object_error<sub_buffer> : public error { + public: + invalid_object_error(std::string what = "") : + error(CL_INVALID_MEM_OBJECT, what) {} + }; + + template<> + class invalid_object_error<image> : public error { + public: + invalid_object_error(std::string what = "") : + error(CL_INVALID_MEM_OBJECT, what) {} + }; + + template<> + class invalid_object_error<image2d> : public error { + public: + invalid_object_error(std::string what = "") : + error(CL_INVALID_MEM_OBJECT, what) {} + }; + + template<> + class invalid_object_error<image3d> : public error { + public: + invalid_object_error(std::string what = "") : + error(CL_INVALID_MEM_OBJECT, what) {} + }; + + template<> + class invalid_object_error<platform> : public error { + public: + invalid_object_error(std::string what = "") : + error(CL_INVALID_PLATFORM, what) {} + }; + + template<> + class invalid_object_error<program> : public error { + public: + invalid_object_error(std::string what = "") : + error(CL_INVALID_PROGRAM, what) {} + }; + + template<> + class invalid_object_error<sampler> : public error { + public: + invalid_object_error(std::string what = "") : + error(CL_INVALID_SAMPLER, what) {} + }; + + class invalid_wait_list_error : public error { + public: + invalid_wait_list_error(std::string what = "") : + error(CL_INVALID_EVENT_WAIT_LIST, what) {} + }; +} + +#endif diff --git a/src/gallium/state_trackers/clover/core/event.hpp b/src/gallium/state_trackers/clover/core/event.hpp index b74db32293..d1f1dd4a5a 100644 --- a/src/gallium/state_trackers/clover/core/event.hpp +++ b/src/gallium/state_trackers/clover/core/event.hpp @@ -25,7 +25,7 @@ #include <functional> -#include "core/base.hpp" +#include "core/object.hpp" #include "core/queue.hpp" #include "core/timestamp.hpp" #include "util/lazy.hpp" diff --git a/src/gallium/state_trackers/clover/core/format.hpp b/src/gallium/state_trackers/clover/core/format.hpp index 804a84ddda..dd4e7a544f 100644 --- a/src/gallium/state_trackers/clover/core/format.hpp +++ b/src/gallium/state_trackers/clover/core/format.hpp @@ -25,7 +25,7 @@ #include <set> -#include "core/base.hpp" +#include "core/object.hpp" #include "pipe/p_defines.h" #include "pipe/p_format.h" diff --git a/src/gallium/state_trackers/clover/core/kernel.hpp b/src/gallium/state_trackers/clover/core/kernel.hpp index 44f7b4887e..984e2139c7 100644 --- a/src/gallium/state_trackers/clover/core/kernel.hpp +++ b/src/gallium/state_trackers/clover/core/kernel.hpp @@ -25,7 +25,7 @@ #include <memory> -#include "core/base.hpp" +#include "core/object.hpp" #include "core/program.hpp" #include "core/memory.hpp" #include "core/sampler.hpp" diff --git a/src/gallium/state_trackers/clover/core/memory.hpp b/src/gallium/state_trackers/clover/core/memory.hpp index a72ad8b066..f495c45ed3 100644 --- a/src/gallium/state_trackers/clover/core/memory.hpp +++ b/src/gallium/state_trackers/clover/core/memory.hpp @@ -27,7 +27,7 @@ #include <map> #include <memory> -#include "core/base.hpp" +#include "core/object.hpp" #include "core/queue.hpp" namespace clover { diff --git a/src/gallium/state_trackers/clover/core/object.hpp b/src/gallium/state_trackers/clover/core/object.hpp new file mode 100644 index 0000000000..807e2f91fc --- /dev/null +++ b/src/gallium/state_trackers/clover/core/object.hpp @@ -0,0 +1,182 @@ +// +// Copyright 2013 Francisco Jerez +// +// Permission is hereby granted, free of charge, to any person obtaining a +// copy of this software and associated documentation files (the "Software"), +// to deal in the Software without restriction, including without limitation +// the rights to use, copy, modify, merge, publish, distribute, sublicense, +// and/or sell copies of the Software, and to permit persons to whom the +// Software is furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL +// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR +// OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, +// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR +// OTHER DEALINGS IN THE SOFTWARE. +// + +#ifndef CLOVER_CORE_OBJECT_HPP +#define CLOVER_CORE_OBJECT_HPP + +#include <cassert> +#include <functional> +#include <vector> + +#include "CL/cl.h" + +#include "core/error.hpp" +#include "core/property.hpp" + +/// +/// Main namespace of the CL state tracker. +/// +namespace clover { + /// + /// Class that represents a CL API object. + /// + template<typename T, typename S> + struct descriptor { + typedef T object_type; + typedef S descriptor_type; + }; + + struct default_tag; + struct wait_list_tag; + struct property_list_tag; + + namespace detail { + template<typename T, typename D> + struct descriptor_traits { + typedef T object_type; + + static void + validate(D *d) { + auto o = static_cast<typename D::object_type *>(d); + if (!o || !dynamic_cast<object_type *>(o)) + throw invalid_object_error<T>(); + } + + static void + validate_list(D * const *ds, size_t n) { + if (!ds || !n) + throw error(CL_INVALID_VALUE); + } + }; + + template<typename D> + struct descriptor_traits<default_tag, D> { + typedef typename D::object_type object_type; + + static void + validate(D *d) { + if (!d) + throw invalid_object_error<object_type>(); + } + + static void + validate_list(D *const *ds, size_t n) { + if (!ds || !n) + throw error(CL_INVALID_VALUE); + } + }; + + template<typename D> + struct descriptor_traits<wait_list_tag, D> { + typedef typename D::object_type object_type; + + static void + validate(D *d) { + if (!d) + throw invalid_wait_list_error(); + } + + static void + validate_list(D *const *ds, size_t n) { + if (bool(ds) != bool(n)) + throw invalid_wait_list_error(); + } + }; + } + + /// + /// Get a Clover object from an API object. + /// + /// \a T can either be the Clover object type to return or a \a tag + /// object to select some special validation behavior by means of a + /// specialization of the detail::descriptor_traits template. The + /// default behavior is to infer the most general Clover object + /// type for the given API object. + /// + template<typename T = default_tag, typename D> + typename detail::descriptor_traits<T, D>::object_type & + obj(D *d) { + detail::descriptor_traits<T, D>::validate(d); + + return static_cast< + typename detail::descriptor_traits<T, D>::object_type &>(*d); + } + + /// + /// Get a pointer to a Clover object from an API object. Returns + /// \c NULL if its argument is \c NULL. + /// + /// \sa obj + /// + template<typename T = default_tag, typename D> + typename detail::descriptor_traits<T, D>::object_type * + pobj(D *d) { + if (d) + detail::descriptor_traits<T, D>::validate(d); + + return static_cast< + typename detail::descriptor_traits<T, D>::object_type *>(d); + } + + /// + /// Get an API object from a Clover object. + /// + template<typename O> + typename O::descriptor_type * + desc(O &o) { + return static_cast<typename O::descriptor_type *>(&o); + } + + /// + /// Get an API object from a pointer to a Clover object. + /// + template<typename O> + typename O::descriptor_type * + desc(O *o) { + return static_cast<typename O::descriptor_type *>(o); + } + + /// + /// Get a range of Clover objects from a range of API objects. + /// + /// \sa obj + /// + template<typename T = default_tag, typename D> + ref_vector<typename detail::descriptor_traits<T, D>::object_type> + objs(D *const *ds, size_t n) { + detail::descriptor_traits<T, D>::validate_list(ds, n); + return map(obj<T, D>, range(ds, n)); + } + + /// + /// Get a range of API objects from a range of Clover objects. + /// + template<typename Os> + std::vector<typename Os::value_type::descriptor_type *> + descs(const Os &os) { + return map([](typename Os::value_type &o) { + return desc(o); + }, os); + } +} + +#endif diff --git a/src/gallium/state_trackers/clover/core/platform.hpp b/src/gallium/state_trackers/clover/core/platform.hpp index d0e521ad91..af588cc570 100644 --- a/src/gallium/state_trackers/clover/core/platform.hpp +++ b/src/gallium/state_trackers/clover/core/platform.hpp @@ -25,7 +25,7 @@ #include <vector> -#include "core/base.hpp" +#include "core/object.hpp" #include "core/device.hpp" namespace clover { diff --git a/src/gallium/state_trackers/clover/core/program.cpp b/src/gallium/state_trackers/clover/core/program.cpp index 8cf1337f7a..42b301497b 100644 --- a/src/gallium/state_trackers/clover/core/program.cpp +++ b/src/gallium/state_trackers/clover/core/program.cpp @@ -60,9 +60,7 @@ _cl_program::build(const std::vector<clover::device *> &devs, } catch (build_error &e) { _logs.insert({ dev, e.what() }); - throw error(CL_BUILD_PROGRAM_FAILURE); - } catch (invalid_option_error &e) { - throw error(CL_INVALID_BUILD_OPTIONS); + throw; } } } diff --git a/src/gallium/state_trackers/clover/core/program.hpp b/src/gallium/state_trackers/clover/core/program.hpp index ddebe2068d..0d7bf372f6 100644 --- a/src/gallium/state_trackers/clover/core/program.hpp +++ b/src/gallium/state_trackers/clover/core/program.hpp @@ -25,7 +25,7 @@ #include <map> -#include "core/base.hpp" +#include "core/object.hpp" #include "core/context.hpp" #include "core/module.hpp" diff --git a/src/gallium/state_trackers/clover/core/queue.hpp b/src/gallium/state_trackers/clover/core/queue.hpp index fb6f656cb4..a02de95cf8 100644 --- a/src/gallium/state_trackers/clover/core/queue.hpp +++ b/src/gallium/state_trackers/clover/core/queue.hpp @@ -23,7 +23,7 @@ #ifndef CLOVER_CORE_QUEUE_HPP #define CLOVER_CORE_QUEUE_HPP -#include "core/base.hpp" +#include "core/object.hpp" #include "core/context.hpp" #include "core/timestamp.hpp" #include "pipe/p_context.h" diff --git a/src/gallium/state_trackers/clover/core/resource.hpp b/src/gallium/state_trackers/clover/core/resource.hpp index 90a68d099a..b2eddc08fa 100644 --- a/src/gallium/state_trackers/clover/core/resource.hpp +++ b/src/gallium/state_trackers/clover/core/resource.hpp @@ -25,7 +25,7 @@ #include <list> -#include "core/base.hpp" +#include "core/object.hpp" #include "core/memory.hpp" #include "util/algebra.hpp" #include "pipe/p_state.h" diff --git a/src/gallium/state_trackers/clover/core/sampler.hpp b/src/gallium/state_trackers/clover/core/sampler.hpp index 26fcadb9e6..9716aabd22 100644 --- a/src/gallium/state_trackers/clover/core/sampler.hpp +++ b/src/gallium/state_trackers/clover/core/sampler.hpp @@ -23,7 +23,7 @@ #ifndef CLOVER_CORE_SAMPLER_HPP #define CLOVER_CORE_SAMPLER_HPP -#include "core/base.hpp" +#include "core/object.hpp" #include "core/queue.hpp" namespace clover { diff --git a/src/gallium/state_trackers/clover/core/timestamp.hpp b/src/gallium/state_trackers/clover/core/timestamp.hpp index 7fbc6e4c27..bf9e204108 100644 --- a/src/gallium/state_trackers/clover/core/timestamp.hpp +++ b/src/gallium/state_trackers/clover/core/timestamp.hpp @@ -23,7 +23,7 @@ #ifndef CLOVER_CORE_TIMESTAMP_HPP #define CLOVER_CORE_TIMESTAMP_HPP -#include "core/base.hpp" +#include "core/object.hpp" struct pipe_query; diff --git a/src/gallium/state_trackers/clover/llvm/invocation.cpp b/src/gallium/state_trackers/clover/llvm/invocation.cpp index f14222b44e..4ae496ffb2 100644 --- a/src/gallium/state_trackers/clover/llvm/invocation.cpp +++ b/src/gallium/state_trackers/clover/llvm/invocation.cpp @@ -157,7 +157,7 @@ namespace { opts_carray.data() + opts_carray.size(), Diags); if (!Success) { - throw invalid_option_error(); + throw error(CL_INVALID_BUILD_OPTIONS); } c.getFrontendOpts().ProgramAction = clang::frontend::EmitLLVMOnly; c.getHeaderSearchOpts().UseBuiltinIncludes = true; |