clover: Use conversion operator to initialize build log from compat::string.
[mesa.git] / src / gallium / state_trackers / clover / core / device.cpp
1 //
2 // Copyright 2012 Francisco Jerez
3 //
4 // Permission is hereby granted, free of charge, to any person obtaining a
5 // copy of this software and associated documentation files (the "Software"),
6 // to deal in the Software without restriction, including without limitation
7 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 // and/or sell copies of the Software, and to permit persons to whom the
9 // Software is furnished to do so, subject to the following conditions:
10 //
11 // The above copyright notice and this permission notice shall be included in
12 // all copies or substantial portions of the Software.
13 //
14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
18 // OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19 // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20 // OTHER DEALINGS IN THE SOFTWARE.
21 //
22
23 #include "core/device.hpp"
24 #include "core/platform.hpp"
25 #include "pipe/p_screen.h"
26 #include "pipe/p_state.h"
27
28 using namespace clover;
29
30 namespace {
31 template<typename T>
32 std::vector<T>
33 get_compute_param(pipe_screen *pipe, pipe_compute_cap cap) {
34 int sz = pipe->get_compute_param(pipe, cap, NULL);
35 std::vector<T> v(sz / sizeof(T));
36
37 pipe->get_compute_param(pipe, cap, &v.front());
38 return v;
39 }
40 }
41
42 device::device(clover::platform &platform, pipe_loader_device *ldev) :
43 platform(platform), ldev(ldev) {
44 pipe = pipe_loader_create_screen(ldev, PIPE_SEARCH_DIR);
45 if (!pipe || !pipe->get_param(pipe, PIPE_CAP_COMPUTE)) {
46 if (pipe)
47 pipe->destroy(pipe);
48 throw error(CL_INVALID_DEVICE);
49 }
50 }
51
52 device::~device() {
53 if (pipe)
54 pipe->destroy(pipe);
55 if (ldev)
56 pipe_loader_release(&ldev, 1);
57 }
58
59 bool
60 device::operator==(const device &dev) const {
61 return this == &dev;
62 }
63
64 cl_device_type
65 device::type() const {
66 switch (ldev->type) {
67 case PIPE_LOADER_DEVICE_SOFTWARE:
68 return CL_DEVICE_TYPE_CPU;
69 case PIPE_LOADER_DEVICE_PCI:
70 case PIPE_LOADER_DEVICE_PLATFORM:
71 return CL_DEVICE_TYPE_GPU;
72 default:
73 assert(0);
74 return 0;
75 }
76 }
77
78 cl_uint
79 device::vendor_id() const {
80 switch (ldev->type) {
81 case PIPE_LOADER_DEVICE_SOFTWARE:
82 case PIPE_LOADER_DEVICE_PLATFORM:
83 return 0;
84 case PIPE_LOADER_DEVICE_PCI:
85 return ldev->u.pci.vendor_id;
86 default:
87 assert(0);
88 return 0;
89 }
90 }
91
92 size_t
93 device::max_images_read() const {
94 return PIPE_MAX_SHADER_RESOURCES;
95 }
96
97 size_t
98 device::max_images_write() const {
99 return PIPE_MAX_SHADER_RESOURCES;
100 }
101
102 cl_uint
103 device::max_image_levels_2d() const {
104 return pipe->get_param(pipe, PIPE_CAP_MAX_TEXTURE_2D_LEVELS);
105 }
106
107 cl_uint
108 device::max_image_levels_3d() const {
109 return pipe->get_param(pipe, PIPE_CAP_MAX_TEXTURE_3D_LEVELS);
110 }
111
112 cl_uint
113 device::max_samplers() const {
114 return pipe->get_shader_param(pipe, PIPE_SHADER_COMPUTE,
115 PIPE_SHADER_CAP_MAX_TEXTURE_SAMPLERS);
116 }
117
118 cl_ulong
119 device::max_mem_global() const {
120 return get_compute_param<uint64_t>(pipe,
121 PIPE_COMPUTE_CAP_MAX_GLOBAL_SIZE)[0];
122 }
123
124 cl_ulong
125 device::max_mem_local() const {
126 return get_compute_param<uint64_t>(pipe,
127 PIPE_COMPUTE_CAP_MAX_LOCAL_SIZE)[0];
128 }
129
130 cl_ulong
131 device::max_mem_input() const {
132 return get_compute_param<uint64_t>(pipe,
133 PIPE_COMPUTE_CAP_MAX_INPUT_SIZE)[0];
134 }
135
136 cl_ulong
137 device::max_const_buffer_size() const {
138 return pipe->get_shader_param(pipe, PIPE_SHADER_COMPUTE,
139 PIPE_SHADER_CAP_MAX_CONST_BUFFER_SIZE);
140 }
141
142 cl_uint
143 device::max_const_buffers() const {
144 return pipe->get_shader_param(pipe, PIPE_SHADER_COMPUTE,
145 PIPE_SHADER_CAP_MAX_CONST_BUFFERS);
146 }
147
148 size_t
149 device::max_threads_per_block() const {
150 return get_compute_param<uint64_t>(
151 pipe, PIPE_COMPUTE_CAP_MAX_THREADS_PER_BLOCK)[0];
152 }
153
154 cl_ulong
155 device::max_mem_alloc_size() const {
156 return get_compute_param<uint64_t>(pipe,
157 PIPE_COMPUTE_CAP_MAX_MEM_ALLOC_SIZE)[0];
158 }
159
160 cl_uint
161 device::max_clock_frequency() const {
162 return get_compute_param<uint32_t>(pipe,
163 PIPE_COMPUTE_CAP_MAX_CLOCK_FREQUENCY)[0];
164 }
165
166 cl_uint
167 device::max_compute_units() const {
168 return get_compute_param<uint32_t>(pipe,
169 PIPE_COMPUTE_CAP_MAX_COMPUTE_UNITS)[0];
170 }
171
172 bool
173 device::image_support() const {
174 return get_compute_param<uint32_t>(pipe,
175 PIPE_COMPUTE_CAP_IMAGES_SUPPORTED)[0];
176 }
177
178 std::vector<size_t>
179 device::max_block_size() const {
180 auto v = get_compute_param<uint64_t>(pipe, PIPE_COMPUTE_CAP_MAX_BLOCK_SIZE);
181 return { v.begin(), v.end() };
182 }
183
184 std::string
185 device::device_name() const {
186 return pipe->get_name(pipe);
187 }
188
189 std::string
190 device::vendor_name() const {
191 return pipe->get_vendor(pipe);
192 }
193
194 enum pipe_shader_ir
195 device::ir_format() const {
196 return (enum pipe_shader_ir) pipe->get_shader_param(
197 pipe, PIPE_SHADER_COMPUTE, PIPE_SHADER_CAP_PREFERRED_IR);
198 }
199
200 std::string
201 device::ir_target() const {
202 std::vector<char> target = get_compute_param<char>(
203 pipe, PIPE_COMPUTE_CAP_IR_TARGET);
204 return { target.data() };
205 }
206
207 enum pipe_endian
208 device::endianness() const {
209 return (enum pipe_endian)pipe->get_param(pipe, PIPE_CAP_ENDIANNESS);
210 }