Path: blob/21.2-virgl/src/gallium/frontends/clover/core/queue.cpp
4572 views
//1// Copyright 2012 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#include "core/queue.hpp"23#include "core/event.hpp"24#include "pipe/p_screen.h"25#include "pipe/p_context.h"26#include "pipe/p_state.h"2728using namespace clover;2930namespace {31void32debug_notify_callback(void *data,33unsigned *id,34enum pipe_debug_type type,35const char *fmt,36va_list args) {37const command_queue *queue = (const command_queue *)data;38char buffer[1024];39vsnprintf(buffer, sizeof(buffer), fmt, args);40queue->context().notify(buffer);41}42}4344command_queue::command_queue(clover::context &ctx, clover::device &dev,45cl_command_queue_properties props) :46context(ctx), device(dev), _props(props) {47pipe = dev.pipe->context_create(dev.pipe, NULL, PIPE_CONTEXT_COMPUTE_ONLY);48if (!pipe)49throw error(CL_INVALID_DEVICE);5051if (ctx.notify) {52struct pipe_debug_callback cb;53memset(&cb, 0, sizeof(cb));54cb.debug_message = &debug_notify_callback;55cb.data = this;56if (pipe->set_debug_callback)57pipe->set_debug_callback(pipe, &cb);58}59}60command_queue::command_queue(clover::context &ctx, clover::device &dev,61std::vector<cl_queue_properties> properties) :62context(ctx), device(dev), _properties(properties), _props(0) {6364for(std::vector<cl_queue_properties>::size_type i = 0; i != properties.size(); i += 2) {65if (properties[i] == 0)66break;67if (properties[i] == CL_QUEUE_PROPERTIES)68_props |= properties[i + 1];69else if (properties[i] != CL_QUEUE_SIZE)70throw error(CL_INVALID_VALUE);71}7273pipe = dev.pipe->context_create(dev.pipe, NULL, PIPE_CONTEXT_COMPUTE_ONLY);74if (!pipe)75throw error(CL_INVALID_DEVICE);7677if (ctx.notify) {78struct pipe_debug_callback cb;79memset(&cb, 0, sizeof(cb));80cb.debug_message = &debug_notify_callback;81cb.data = this;82if (pipe->set_debug_callback)83pipe->set_debug_callback(pipe, &cb);84}85}8687command_queue::~command_queue() {88pipe->destroy(pipe);89}9091void92command_queue::flush() {93std::lock_guard<std::mutex> lock(queued_events_mutex);94flush_unlocked();95}9697void98command_queue::flush_unlocked() {99pipe_screen *screen = device().pipe;100pipe_fence_handle *fence = NULL;101102if (!queued_events.empty()) {103pipe->flush(pipe, &fence, 0);104105while (!queued_events.empty() &&106queued_events.front()().signalled()) {107queued_events.front()().fence(fence);108queued_events.pop_front();109}110111screen->fence_reference(screen, &fence, NULL);112}113}114115void116command_queue::svm_migrate(const std::vector<void const*> &svm_pointers,117const std::vector<size_t> &sizes,118cl_mem_migration_flags flags) {119if (!pipe->svm_migrate)120return;121122bool to_device = !(flags & CL_MIGRATE_MEM_OBJECT_HOST);123bool mem_undefined = flags & CL_MIGRATE_MEM_OBJECT_CONTENT_UNDEFINED;124pipe->svm_migrate(pipe, svm_pointers.size(), svm_pointers.data(),125sizes.data(), to_device, mem_undefined);126}127128cl_command_queue_properties129command_queue::props() const {130return _props;131}132133std::vector<cl_queue_properties>134command_queue::properties() const {135return _properties;136}137138bool139command_queue::profiling_enabled() const {140return _props & CL_QUEUE_PROFILING_ENABLE;141}142143void144command_queue::sequence(hard_event &ev) {145std::lock_guard<std::mutex> lock(queued_events_mutex);146if (!queued_events.empty())147queued_events.back()().chain(ev);148149queued_events.push_back(ev);150151// Arbitrary threshold.152// The CTS tends to run a lot of subtests without flushing with the image153// tests, so flush regularly to prevent stack overflows.154if (queued_events.size() > 1000)155flush_unlocked();156}157158159