Commit 6b15f4d6 authored by Zebediah Figura's avatar Zebediah Figura Committed by Alexandre Julliard

opencl: Move libOpenCL support to a new Unix library.

parent 4d582411
......@@ -2,5 +2,7 @@ MODULE = opencl.dll
EXTRALIBS = $(OPENCL_LIBS)
C_SRCS = \
opencl.c \
opencl_thunks.c
pe_thunks.c \
pe_wrappers.c \
unix_thunks.c \
unix_wrappers.c
......@@ -20,7 +20,9 @@ use XML::LibXML;
# Files to generate
my $spec_file = "opencl.spec";
my $thunks_file = "opencl_thunks.c";
my $pe_file = "pe_thunks.c";
my $unix_file = "unix_thunks.c";
my $unixheader_file = "unixlib.h";
# If set to 1, generate TRACEs for each OpenGL function
my $gen_traces = 1;
......@@ -49,7 +51,7 @@ my %arg_types =
"unsigned int" => [ "long", "%u" ],
);
sub generate_thunk($$)
sub generate_pe_thunk($$)
{
my ($name, $func_ref) = @_;
my $call_arg = "";
......@@ -86,6 +88,28 @@ sub generate_thunk($$)
$ret .= " TRACE( \"($trace_arg)\\n\"$trace_call_arg );\n" if $gen_traces;
$ret .= " ";
$ret .= "return " unless is_void_func( $func_ref );
$ret .= "opencl_funcs->p$name($call_arg);\n";
$ret .= "}\n";
return $ret;
}
sub generate_unix_thunk($$)
{
my ($name, $func_ref) = @_;
my $call_arg = "";
my $ret = get_func_proto( "static %s WINAPI wrap_%s(%s)", $name, $func_ref );
foreach my $arg (@{$func_ref->[1]})
{
my $ptype = get_arg_type( $arg );
next unless $arg->findnodes("./name");
my $pname = get_arg_name( $arg );
my $param = $arg->textContent();
$call_arg .= " " . $pname . ",";
}
$call_arg =~ s/,$/ /;
$ret .= "\n{\n ";
$ret .= "return " unless is_void_func( $func_ref );
$ret .= "$name($call_arg);\n";
$ret .= "}\n";
return $ret;
......@@ -122,6 +146,7 @@ sub get_func_proto($$$)
foreach my $arg (@{$func->[1]})
{
(my $argtext = $arg->textContent()) =~ s/ +/ /g;
$argtext =~ s/CL_CALLBACK/WINAPI/g;
$args .= " " . $argtext . ",";
}
$args =~ s/,$/ /;
......@@ -180,16 +205,10 @@ my %cl_enums;
my (%cl_types, @cl_types); # also use an array to preserve declaration order
# some functions need a hand-written wrapper
sub needs_wrapper($)
sub needs_pe_wrapper($)
{
my %funcs =
(
# need callback conversion
"clBuildProgram" => 1,
"clCreateContext" => 1,
"clCreateContextFromType" => 1,
"clEnqueueNativeKernel" => 1,
# need extension filtering
"clGetDeviceInfo" => 1,
"clGetPlatformInfo" => 1,
......@@ -202,6 +221,22 @@ sub needs_wrapper($)
return defined $funcs{$name};
}
# some functions need a hand-written wrapper
sub needs_unix_wrapper($)
{
my %funcs =
(
# need callback conversion
"clBuildProgram" => 1,
"clCreateContext" => 1,
"clCreateContextFromType" => 1,
"clEnqueueNativeKernel" => 1,
);
my $name = shift;
return defined $funcs{$name};
}
sub parse_file($)
{
my $file = shift;
......@@ -279,21 +314,66 @@ foreach (sort keys %core_functions)
close(SPEC);
my $file_header =
"/* Automatically generated from OpenCL registry files; DO NOT EDIT! */\n\n" .
"#include \"config.h\"\n" .
"#include \"opencl_private.h\"\n\n";
# generate the PE thunks
open(PE, ">$pe_file") or die "cannot create $pe_file";
print PE "/* Automatically generated from OpenCL registry files; DO NOT EDIT! */\n\n";
print PE "#include \"config.h\"\n";
print PE "#include \"opencl_private.h\"\n\n";
print PE "WINE_DEFAULT_DEBUG_CHANNEL(opencl);\n" if $gen_traces;
foreach (sort keys %core_functions)
{
next if needs_pe_wrapper( $_ );
print PE "\n", generate_pe_thunk( $_, $core_functions{$_} );
}
close(PE);
# generate the unix library thunks
open(UNIX, ">$unix_file") or die "cannot create $unix_file";
print UNIX <<EOF
/* Automatically generated from OpenCL registry files; DO NOT EDIT! */
#if 0
#pragma makedep unix
#endif
$file_header .= "WINE_DEFAULT_DEBUG_CHANNEL(opencl);\n" if $gen_traces;
#include "config.h"
#include "unix_private.h"
EOF
;
# generate the thunks file
open(THUNKS, ">$thunks_file") or die "cannot create $thunks_file";
print THUNKS $file_header;
foreach (sort keys %core_functions)
{
next if needs_unix_wrapper( $_ );
print UNIX "\n", generate_unix_thunk( $_, $core_functions{$_} );
}
print UNIX "\nconst struct opencl_funcs funcs =\n{\n";
foreach (sort keys %core_functions)
{
next if needs_wrapper( $_ );
print THUNKS "\n", generate_thunk( $_, $core_functions{$_} );
print UNIX " wrap_" . $_ . ",\n";
}
print UNIX "};\n";
close(UNIX);
# generate the unix library header
open(UNIXHEADER, ">$unixheader_file") or die "cannot create $unixheader_file";
print UNIXHEADER "/* Automatically generated from OpenCL registry files; DO NOT EDIT! */\n\n";
print UNIXHEADER "struct opencl_funcs\n{\n";
foreach (sort keys %core_functions)
{
print UNIXHEADER get_func_proto( " %s (WINAPI *p%s)(%s);\n", $_, $core_functions{$_} );
}
print UNIXHEADER "};\n\n";
print UNIXHEADER "extern const struct opencl_funcs *opencl_funcs;\n";
close(THUNKS);
close(UNIXHEADER);
......@@ -21,8 +21,11 @@
#include <stdarg.h>
#include "ntstatus.h"
#define WIN32_NO_STATUS
#include "windef.h"
#include "winbase.h"
#include "winternl.h"
#include "wine/debug.h"
......@@ -38,4 +41,6 @@
#include <OpenCL/opencl.h>
#endif
#include "unixlib.h"
#endif
......@@ -23,6 +23,8 @@
WINE_DEFAULT_DEBUG_CHANNEL(opencl);
const struct opencl_funcs *opencl_funcs = NULL;
cl_int WINAPI wine_clGetPlatformInfo(cl_platform_id platform, cl_platform_info param_name,
SIZE_T param_value_size, void * param_value, size_t * param_value_size_ret)
{
......@@ -51,7 +53,7 @@ cl_int WINAPI wine_clGetPlatformInfo(cl_platform_id platform, cl_platform_info p
}
else
{
ret = clGetPlatformInfo(platform, param_name, param_value_size, param_value, param_value_size_ret);
ret = opencl_funcs->pclGetPlatformInfo(platform, param_name, param_value_size, param_value, param_value_size_ret);
}
TRACE("(%p, 0x%x, %ld, %p, %p)=%d\n", platform, param_name, param_value_size, param_value, param_value_size_ret, ret);
......@@ -87,7 +89,7 @@ cl_int WINAPI wine_clGetDeviceInfo(cl_device_id device, cl_device_info param_nam
}
else
{
ret = clGetDeviceInfo(device, param_name, param_value_size, param_value, param_value_size_ret);
ret = opencl_funcs->pclGetDeviceInfo(device, param_name, param_value_size, param_value, param_value_size_ret);
}
/* Filter out the CL_EXEC_NATIVE_KERNEL flag */
......@@ -102,126 +104,6 @@ cl_int WINAPI wine_clGetDeviceInfo(cl_device_id device, cl_device_info param_nam
}
typedef struct
{
void WINAPI (*pfn_notify)(const char *errinfo, const void *private_info, size_t cb, void *user_data);
void *user_data;
} CONTEXT_CALLBACK;
static void context_fn_notify(const char *errinfo, const void *private_info, size_t cb, void *user_data)
{
CONTEXT_CALLBACK *ccb;
TRACE("(%s, %p, %ld, %p)\n", errinfo, private_info, (SIZE_T)cb, user_data);
ccb = (CONTEXT_CALLBACK *) user_data;
if(ccb->pfn_notify) ccb->pfn_notify(errinfo, private_info, cb, ccb->user_data);
TRACE("Callback COMPLETED\n");
}
cl_context WINAPI wine_clCreateContext(const cl_context_properties * properties, cl_uint num_devices, const cl_device_id * devices,
void WINAPI (*pfn_notify)(const char *errinfo, const void *private_info, size_t cb, void *user_data),
void * user_data, cl_int * errcode_ret)
{
cl_context ret;
CONTEXT_CALLBACK *ccb;
TRACE("(%p, %d, %p, %p, %p, %p)\n", properties, num_devices, devices, pfn_notify, user_data, errcode_ret);
/* FIXME: The CONTEXT_CALLBACK structure is currently leaked.
* Pointers to callback redirectors should be remembered and free()d when the context is destroyed.
* The problem is determining when a context is being destroyed. clReleaseContext only decrements
* the use count for a context, its destruction can come much later and therefore there is a risk
* that the callback could be invoked after the user_data memory has been free()d.
*/
ccb = HeapAlloc(GetProcessHeap(), 0, sizeof(CONTEXT_CALLBACK));
ccb->pfn_notify = pfn_notify;
ccb->user_data = user_data;
ret = clCreateContext(properties, num_devices, devices, context_fn_notify, ccb, errcode_ret);
TRACE("(%p, %d, %p, %p, %p, %p (%d)))=%p\n", properties, num_devices, devices, &pfn_notify, user_data, errcode_ret, errcode_ret ? *errcode_ret : 0, ret);
return ret;
}
cl_context WINAPI wine_clCreateContextFromType(const cl_context_properties * properties, cl_device_type device_type,
void WINAPI (*pfn_notify)(const char *errinfo, const void *private_info, size_t cb, void *user_data),
void * user_data, cl_int * errcode_ret)
{
cl_context ret;
CONTEXT_CALLBACK *ccb;
TRACE("(%p, 0x%lx, %p, %p, %p)\n", properties, (long unsigned int)device_type, pfn_notify, user_data, errcode_ret);
/* FIXME: The CONTEXT_CALLBACK structure is currently leaked.
* Pointers to callback redirectors should be remembered and free()d when the context is destroyed.
* The problem is determining when a context is being destroyed. clReleaseContext only decrements
* the use count for a context, its destruction can come much later and therefore there is a risk
* that the callback could be invoked after the user_data memory has been free()d.
*/
ccb = HeapAlloc(GetProcessHeap(), 0, sizeof(CONTEXT_CALLBACK));
ccb->pfn_notify = pfn_notify;
ccb->user_data = user_data;
ret = clCreateContextFromType(properties, device_type, context_fn_notify, ccb, errcode_ret);
TRACE("(%p, 0x%lx, %p, %p, %p (%d)))=%p\n", properties, (long unsigned int)device_type, pfn_notify, user_data, errcode_ret, errcode_ret ? *errcode_ret : 0, ret);
return ret;
}
typedef struct
{
void WINAPI (*pfn_notify)(cl_program program, void * user_data);
void *user_data;
} PROGRAM_CALLBACK;
static void program_fn_notify(cl_program program, void * user_data)
{
PROGRAM_CALLBACK *pcb;
TRACE("(%p, %p)\n", program, user_data);
pcb = (PROGRAM_CALLBACK *) user_data;
pcb->pfn_notify(program, pcb->user_data);
HeapFree(GetProcessHeap(), 0, pcb);
TRACE("Callback COMPLETED\n");
}
cl_int WINAPI wine_clBuildProgram(cl_program program, cl_uint num_devices, const cl_device_id * device_list, const char * options,
void WINAPI (*pfn_notify)(cl_program program, void * user_data),
void * user_data)
{
cl_int ret;
TRACE("\n");
if(pfn_notify)
{
/* When pfn_notify is provided, clBuildProgram is asynchronous */
PROGRAM_CALLBACK *pcb;
pcb = HeapAlloc(GetProcessHeap(), 0, sizeof(PROGRAM_CALLBACK));
pcb->pfn_notify = pfn_notify;
pcb->user_data = user_data;
ret = clBuildProgram(program, num_devices, device_list, options, program_fn_notify, pcb);
}
else
{
/* When pfn_notify is NULL, clBuildProgram is synchronous */
ret = clBuildProgram(program, num_devices, device_list, options, NULL, user_data);
}
return ret;
}
cl_int WINAPI wine_clEnqueueNativeKernel(cl_command_queue command_queue,
void WINAPI (*user_func)(void *args),
void * args, size_t cb_args,
cl_uint num_mem_objects, const cl_mem * mem_list, const void ** args_mem_loc,
cl_uint num_events_in_wait_list, const cl_event * event_wait_list, cl_event * event)
{
cl_int ret = CL_INVALID_OPERATION;
/* FIXME: There appears to be no obvious method for translating the ABI for user_func.
* There is no opaque user_data structure passed, that could encapsulate the return address.
* The OpenCL specification seems to indicate that args has an implementation specific
* structure that cannot be used to stash away a return address for the WINAPI user_func.
*/
#if 0
ret = clEnqueueNativeKernel(command_queue, user_func, args, cb_args, num_mem_objects, mem_list, args_mem_loc,
num_events_in_wait_list, event_wait_list, event);
#else
FIXME("not supported due to user_func ABI mismatch\n");
#endif
return ret;
}
void * WINAPI wine_clGetExtensionFunctionAddress(const char * func_name)
{
void * ret = 0;
......@@ -234,3 +116,13 @@ void * WINAPI wine_clGetExtensionFunctionAddress(const char * func_name)
TRACE("(%s)=%p\n",func_name, ret);
return ret;
}
BOOL WINAPI DllMain( HINSTANCE instance, DWORD reason, void *reserved )
{
if (reason == DLL_PROCESS_ATTACH)
{
DisableThreadLibraryCalls( instance );
return __wine_init_unix_lib( instance, reason, NULL, &opencl_funcs );
}
return TRUE;
}
/*
* Copyright 2021 Zebediah Figura
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
*/
#ifndef __WINE_UNIX_PRIVATE_H
#define __WINE_UNIX_PRIVATE_H
#include "opencl_private.h"
cl_int WINAPI wrap_clBuildProgram( cl_program program, cl_uint num_devices,
const cl_device_id *device_list, const char *options,
void (WINAPI *pfn_notify)(cl_program program, void *user_data),
void *user_data ) DECLSPEC_HIDDEN;
cl_context WINAPI wrap_clCreateContext( const cl_context_properties *properties,
cl_uint num_devices, const cl_device_id *devices,
void (WINAPI *pfn_notify)(const char *errinfo, const void *private_info, size_t cb, void *user_data),
void *user_data, cl_int *errcode_ret ) DECLSPEC_HIDDEN;
cl_context WINAPI wrap_clCreateContextFromType( const cl_context_properties *properties, cl_device_type device_type,
void (WINAPI *pfn_notify)(const char *errinfo, const void *private_info, size_t cb, void *user_data),
void *user_data, cl_int *errcode_ret ) DECLSPEC_HIDDEN;
cl_int WINAPI wrap_clEnqueueNativeKernel( cl_command_queue command_queue,
void (WINAPI *user_func)(void *),
void *args, size_t cb_args, cl_uint num_mem_objects, const cl_mem *mem_list, const void **args_mem_loc,
cl_uint num_events_in_wait_list, const cl_event *event_wait_list, cl_event *event ) DECLSPEC_HIDDEN;
extern const struct opencl_funcs funcs;
#endif
/*
* Copyright 2021 Zebediah Figura
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
*/
#if 0
#pragma makedep unix
#endif
#include "config.h"
#include <stdlib.h>
#include "unix_private.h"
WINE_DEFAULT_DEBUG_CHANNEL(opencl);
struct program_callback
{
void (WINAPI *pfn_notify)(cl_program program, void *user_data);
void *user_data;
};
static void CL_CALLBACK program_callback_wrapper(cl_program program, void *user_data)
{
struct program_callback *callback = user_data;
TRACE("(%p, %p)\n", program, user_data);
callback->pfn_notify(program, callback->user_data);
free(callback);
}
cl_int WINAPI wrap_clBuildProgram( cl_program program, cl_uint num_devices,
const cl_device_id *device_list, const char *options,
void (WINAPI *pfn_notify)(cl_program program, void *user_data),
void *user_data )
{
if (pfn_notify)
{
struct program_callback *callback;
cl_int ret;
if (!(callback = malloc(sizeof(*callback))))
return CL_OUT_OF_HOST_MEMORY;
callback->pfn_notify = pfn_notify;
callback->user_data = user_data;
if ((ret = clBuildProgram( program, num_devices, device_list, options,
program_callback_wrapper, callback )) != CL_SUCCESS)
free( callback );
return ret;
}
return clBuildProgram( program, num_devices, device_list, options, NULL, NULL );
}
struct context_callback
{
void (WINAPI *pfn_notify)(const char *errinfo, const void *private_info, size_t cb, void *user_data);
void *user_data;
};
static void CL_CALLBACK context_callback_wrapper(const char *errinfo,
const void *private_info, size_t cb, void *user_data)
{
struct context_callback *callback = user_data;
TRACE("(%s, %p, %zu, %p)\n", debugstr_a(errinfo), private_info, cb, user_data);
callback->pfn_notify(errinfo, private_info, cb, callback->user_data);
}
cl_context WINAPI wrap_clCreateContext( const cl_context_properties *properties,
cl_uint num_devices, const cl_device_id *devices,
void (WINAPI *pfn_notify)(const char *errinfo, const void *private_info, size_t cb, void *user_data),
void *user_data, cl_int *errcode_ret )
{
if (pfn_notify)
{
struct context_callback *callback;
cl_context ret;
/* FIXME: the callback structure is currently leaked */
if (!(callback = malloc(sizeof(*callback))))
{
*errcode_ret = CL_OUT_OF_HOST_MEMORY;
return NULL;
}
callback->pfn_notify = pfn_notify;
callback->user_data = user_data;
if (!(ret = clCreateContext( properties, num_devices, devices, context_callback_wrapper, callback, errcode_ret )))
free( callback );
return ret;
}
return clCreateContext( properties, num_devices, devices, NULL, NULL, errcode_ret );
}
cl_context WINAPI wrap_clCreateContextFromType( const cl_context_properties *properties, cl_device_type device_type,
void (WINAPI *pfn_notify)(const char *errinfo, const void *private_info, size_t cb, void *user_data),
void *user_data, cl_int *errcode_ret )
{
if (pfn_notify)
{
struct context_callback *callback;
cl_context ret;
/* FIXME: the callback structure is currently leaked */
if (!(callback = malloc(sizeof(*callback))))
{
*errcode_ret = CL_OUT_OF_HOST_MEMORY;
return NULL;
}
callback->pfn_notify = pfn_notify;
callback->user_data = user_data;
if (!(ret = clCreateContextFromType( properties, device_type, context_callback_wrapper, callback, errcode_ret )))
free( callback );
return ret;
}
return clCreateContextFromType( properties, device_type, NULL, NULL, errcode_ret );
}
cl_int WINAPI wrap_clEnqueueNativeKernel( cl_command_queue command_queue,
void (WINAPI *user_func)(void *),
void *args, size_t cb_args, cl_uint num_mem_objects, const cl_mem *mem_list, const void **args_mem_loc,
cl_uint num_events_in_wait_list, const cl_event *event_wait_list, cl_event *event )
{
/* we have no clear way to wrap user_func */
FIXME( "not implemented\n" );
return CL_INVALID_OPERATION;
}
NTSTATUS CDECL __wine_init_unix_lib( HMODULE module, DWORD reason, const void *ptr_in, void *ptr_out )
{
if (reason != DLL_PROCESS_ATTACH) return STATUS_SUCCESS;
*(const struct opencl_funcs **)ptr_out = &funcs;
return STATUS_SUCCESS;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment