clover: Migrate a bunch of pointers and references in the object tree to smart refere...
[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 throw error(CL_INVALID_DEVICE);
47 }
48
49 device::~device() {
50 if (pipe)
51 pipe->destroy(pipe);
52 if (ldev)
53 pipe_loader_release(&ldev, 1);
54 }
55
56 bool
57 device::operator==(const device &dev) const {
58 return this == &dev;
59 }
60
61 cl_device_type
62 device::type() const {
63 switch (ldev->type) {
64 case PIPE_LOADER_DEVICE_SOFTWARE:
65 return CL_DEVICE_TYPE_CPU;
66 case PIPE_LOADER_DEVICE_PCI:
67 case PIPE_LOADER_DEVICE_PLATFORM:
68 return CL_DEVICE_TYPE_GPU;
69 default:
70 assert(0);
71 return 0;
72 }
73 }
74
75 cl_uint
76 device::vendor_id() const {
77 switch (ldev->type) {
78 case PIPE_LOADER_DEVICE_SOFTWARE:
79 case PIPE_LOADER_DEVICE_PLATFORM:
80 return 0;
81 case PIPE_LOADER_DEVICE_PCI:
82 return ldev->u.pci.vendor_id;
83 default:
84 assert(0);
85 return 0;
86 }
87 }
88
89 size_t
90 device::max_images_read() const {
91 return PIPE_MAX_SHADER_RESOURCES;
92 }
93
94 size_t
95 device::max_images_write() const {
96 return PIPE_MAX_SHADER_RESOURCES;
97 }
98
99 cl_uint
100 device::max_image_levels_2d() const {
101 return pipe->get_param(pipe, PIPE_CAP_MAX_TEXTURE_2D_LEVELS);
102 }
103
104 cl_uint
105 device::max_image_levels_3d() const {
106 return pipe->get_param(pipe, PIPE_CAP_MAX_TEXTURE_3D_LEVELS);
107 }
108
109 cl_uint
110 device::max_samplers() const {
111 return pipe->get_shader_param(pipe, PIPE_SHADER_COMPUTE,
112 PIPE_SHADER_CAP_MAX_TEXTURE_SAMPLERS);
113 }
114
115 cl_ulong
116 device::max_mem_global() const {
117 return get_compute_param<uint64_t>(pipe,
118 PIPE_COMPUTE_CAP_MAX_GLOBAL_SIZE)[0];
119 }
120
121 cl_ulong
122 device::max_mem_local() const {
123 return get_compute_param<uint64_t>(pipe,
124 PIPE_COMPUTE_CAP_MAX_LOCAL_SIZE)[0];
125 }
126
127 cl_ulong
128 device::max_mem_input() const {
129 return get_compute_param<uint64_t>(pipe,
130 PIPE_COMPUTE_CAP_MAX_INPUT_SIZE)[0];
131 }
132
133 cl_ulong
134 device::max_const_buffer_size() const {
135 return pipe->get_shader_param(pipe, PIPE_SHADER_COMPUTE,
136 PIPE_SHADER_CAP_MAX_CONSTS) * 16;
137 }
138
139 cl_uint
140 device::max_const_buffers() const {
141 return pipe->get_shader_param(pipe, PIPE_SHADER_COMPUTE,
142 PIPE_SHADER_CAP_MAX_CONST_BUFFERS);
143 }
144
145 size_t
146 device::max_threads_per_block() const {
147 return get_compute_param<uint64_t>(
148 pipe, PIPE_COMPUTE_CAP_MAX_THREADS_PER_BLOCK)[0];
149 }
150
151 cl_ulong
152 device::max_mem_alloc_size() const {
153 return get_compute_param<uint64_t>(pipe,
154 PIPE_COMPUTE_CAP_MAX_MEM_ALLOC_SIZE)[0];
155 }
156
157 std::vector<size_t>
158 device::max_block_size() const {
159 auto v = get_compute_param<uint64_t>(pipe, PIPE_COMPUTE_CAP_MAX_BLOCK_SIZE);
160 return { v.begin(), v.end() };
161 }
162
163 std::string
164 device::device_name() const {
165 return pipe->get_name(pipe);
166 }
167
168 std::string
169 device::vendor_name() const {
170 return pipe->get_vendor(pipe);
171 }
172
173 enum pipe_shader_ir
174 device::ir_format() const {
175 return (enum pipe_shader_ir) pipe->get_shader_param(
176 pipe, PIPE_SHADER_COMPUTE, PIPE_SHADER_CAP_PREFERRED_IR);
177 }
178
179 std::string
180 device::ir_target() const {
181 std::vector<char> target = get_compute_param<char>(
182 pipe, PIPE_COMPUTE_CAP_IR_TARGET);
183 return { target.data() };
184 }
185
186 enum pipe_endian
187 device::endianness() const {
188 return (enum pipe_endian)pipe->get_param(pipe, PIPE_CAP_ENDIANNESS);
189 }