OGLplus  (0.59.0) a C++ wrapper for rendering APIs

program_file.hpp
Go to the documentation of this file.
1 
9 #ifndef OGLPLUS_UTILS_PROGRAM_FILE_HPP
10 #define OGLPLUS_UTILS_PROGRAM_FILE_HPP
11 
12 #include "../gl_api/enum_types.hpp"
13 #include "../glsl/source_ref.hpp"
14 #include "program_file_hdr.hpp"
15 #include <eagine/assert.hpp>
16 #include <eagine/file_contents.hpp>
17 #include <eagine/memory/block.hpp>
18 #include <eagine/string_span.hpp>
19 
20 namespace eagine::oglp {
21 //------------------------------------------------------------------------------
22 class shader_source_block {
23 private:
24  structured_memory_block<const shader_source_header> _header;
25 
26 public:
27  shader_source_block(memory::const_block blk)
28  : _header(blk) {}
29 
30  shader_source_block(const shader_source_header* ptr)
31  : _header(as_bytes(view_one(ptr))) {}
32 
33  auto is_valid() const noexcept -> bool {
34  return _header->magic.is_valid();
35  }
36 
37  auto type() const noexcept -> shader_type {
38  EAGINE_ASSERT(is_valid());
39  return shader_type(_header->shader_type);
40  }
41 
42  auto source_text() const noexcept -> string_view {
43  EAGINE_ASSERT(is_valid());
44  return {
45  _header->source_text.data(), span_size(_header->source_text.size())};
46  }
47 
48  operator glsl_source_ref() const noexcept {
49  EAGINE_ASSERT(is_valid());
50  return glsl_source_ref(_header->source_text);
51  }
52 };
53 //------------------------------------------------------------------------------
54 class shader_source_file
55  : protected_member<file_contents>
56  , public shader_source_block {
57 public:
58  shader_source_file(file_contents&& fc)
59  : protected_member<file_contents>(std::move(fc))
60  , shader_source_block(get_the_member()) {}
61 
62  shader_source_file(string_view path)
63  : shader_source_file(file_contents(path)) {}
64 
65  shader_source_file(const std::string& path)
66  : shader_source_file(string_view(path)) {}
67 };
68 //------------------------------------------------------------------------------
69 class program_source_block {
70 private:
71  structured_memory_block<const program_source_header> _header;
72 
73 public:
74  program_source_block(memory::const_block blk)
75  : _header(blk) {}
76 
77  auto is_valid() const noexcept -> bool {
78  return _header->magic.is_valid();
79  }
80 
81  auto shader_source_count() const noexcept -> span_size_t {
82  EAGINE_ASSERT(is_valid());
83  return _header->shader_sources.size();
84  }
85 
86  auto shader_source(span_size_t index) const noexcept
87  -> shader_source_block {
88  EAGINE_ASSERT(is_valid());
89  EAGINE_ASSERT(index < _header->shader_sources.size());
90  return {_header->shader_sources[index]};
91  }
92 };
93 //------------------------------------------------------------------------------
94 class program_source_file
95  : protected_member<file_contents>
96  , public program_source_block {
97 public:
98  program_source_file(file_contents&& fc)
99  : protected_member<file_contents>(std::move(fc))
100  , program_source_block(get_the_member()) {}
101 
102  program_source_file(string_view path)
103  : program_source_file(file_contents(path)) {}
104 
105  program_source_file(const std::string& path)
106  : program_source_file(string_view(path)) {}
107 };
108 //------------------------------------------------------------------------------
109 } // namespace eagine::oglp
110 
111 #endif // OGLPLUS_UTILS_PROGRAM_FILE_HPP
std::ptrdiff_t span_size_t
Signed span size type used by eagine.
Definition: types.hpp:36
basic_string_span< const char > string_view
Alias for const string views.
Definition: string_span.hpp:116
static constexpr auto as_bytes(basic_span< T, P, S > spn) noexcept -> basic_block< std::is_const_v< T >>
Converts a span into a basic_block.
Definition: block.hpp:39
static constexpr auto span_size(T v) noexcept
Converts argument to span size type.
Definition: types.hpp:59
basic_block< true > const_block
Alias for const byte memory span.
Definition: block.hpp:32

Copyright © 2015-2021 Matúš Chochlík.
<chochlik -at -gmail.com>
Documentation generated on Tue Apr 13 2021 by Doxygen (version 1.8.17).