Load cuda at runtime. Include cuda header files inside the project

This commit is contained in:
dec05eba 2022-09-26 01:26:45 +02:00
parent 0059724fdc
commit 4fead183fe
10 changed files with 21480 additions and 47 deletions

View File

@ -18,7 +18,7 @@ Using NvFBC (recording the monitor/screen) is not faster than not using NvFBC (r
If you are running an Arch Linux based distro, then you can find gpu screen recorder on aur under the name gpu-screen-recorder-git (`yay -S gpu-screen-recorder-git`).\ If you are running an Arch Linux based distro, then you can find gpu screen recorder on aur under the name gpu-screen-recorder-git (`yay -S gpu-screen-recorder-git`).\
If you are running an Ubuntu based distro then run `install_ubuntu.sh` as root: `sudo ./install_ubuntu.sh`.\ If you are running an Ubuntu based distro then run `install_ubuntu.sh` as root: `sudo ./install_ubuntu.sh`.\
On other distros you need to install dependencies manually and run `build.sh`. Dependencies: `glew glfw3 cuda ffmpeg libx11 libxcomposite libpulse-simple`.\ On other distros you need to install dependencies manually and run `build.sh`. Dependencies: `glew glfw3 ffmpeg libx11 libxcomposite libpulse-simple`. You need to additionally have `cuda` installed when you run `gpu-screen-recorder`.\
If you use a distro that isn't user friendly, such as fedora, then you can also install gpu-screen-recorder-gtk with flatpak here: [gpu-screen-recorder-flatpak](https://git.dec05eba.com/gpu-screen-recorder-flatpak/about/) (Note: this install method is slow).\ If you use a distro that isn't user friendly, such as fedora, then you can also install gpu-screen-recorder-gtk with flatpak here: [gpu-screen-recorder-flatpak](https://git.dec05eba.com/gpu-screen-recorder-flatpak/about/) (Note: this install method is slow).\
Recording monitors requires a gpu with NvFBC support (note: this is not required when recording a single window!). Normally only tesla and quadro gpus support this, but by using [nvidia-patch](https://github.com/keylase/nvidia-patch) or [nvlax](https://github.com/illnyang/nvlax) you can do this on all gpus that support nvenc as well (gpus as old as the nvidia 600 series), provided you are not using outdated gpu drivers. Recording monitors requires a gpu with NvFBC support (note: this is not required when recording a single window!). Normally only tesla and quadro gpus support this, but by using [nvidia-patch](https://github.com/keylase/nvidia-patch) or [nvlax](https://github.com/illnyang/nvlax) you can do this on all gpus that support nvenc as well (gpus as old as the nvidia 600 series), provided you are not using outdated gpu drivers.
@ -41,7 +41,7 @@ This gpu-screen-recorder keeps the window image on the GPU and sends it directly
FFMPEG only uses the GPU with CUDA when doing transcoding from an input video to an output video, and not when recording the screen when using x11grab. So FFMPEG has the same fps drop issues that OBS has. FFMPEG only uses the GPU with CUDA when doing transcoding from an input video to an output video, and not when recording the screen when using x11grab. So FFMPEG has the same fps drop issues that OBS has.
# TODO # TODO
* Support AMD and Intel, using VAAPI. cuda and vaapi should be loaded at runtime using dlopen instead of linking to those * Support AMD and Intel, using VAAPI.
libraries at compile-time. libraries at compile-time.
* Clean up the code! * Clean up the code!
* Dynamically change bitrate/resolution to match desired fps. This would be helpful when streaming for example, where the encode output speed also depends on upload speed to the streaming service. * Dynamically change bitrate/resolution to match desired fps. This would be helpful when streaming for example, where the encode output speed also depends on upload speed to the streaming service.

1
TODO
View File

@ -1,6 +1,5 @@
Check for reparent. Check for reparent.
Only add window to list if its the window is a topmost window. Only add window to list if its the window is a topmost window.
Load cuda at runtime with dlopen.
Track window damages and only update then. That is better for output file size. Track window damages and only update then. That is better for output file size.
Getting the texture of a window when using a compositor is an nvidia specific limitation. When gpu-screen-recorder supports other gpus then this can be ignored. Getting the texture of a window when using a compositor is an nvidia specific limitation. When gpu-screen-recorder supports other gpus then this can be ignored.
Remove dependency on glfw (and glew?). Remove dependency on glfw (and glew?).

View File

@ -1,8 +1,8 @@
#!/bin/sh -e #!/bin/sh -e
dependencies="glew libavcodec libavformat libavutil x11 xcomposite glfw3 libpulse libswresample" dependencies="glew libavcodec libavformat libavutil x11 xcomposite glfw3 libpulse libswresample"
includes="$(pkg-config --cflags $dependencies) -I/opt/cuda/targets/x86_64-linux/include" includes="$(pkg-config --cflags $dependencies) -Iinclude"
libs="$(pkg-config --libs $dependencies) /usr/lib64/libcuda.so -ldl -pthread -lm" libs="$(pkg-config --libs $dependencies) -ldl -pthread -lm"
g++ -c src/sound.cpp -O2 $includes g++ -c src/sound.cpp -O2 $includes
g++ -c src/main.cpp -O2 $includes g++ -c src/main.cpp -O2 $includes
g++ -o gpu-screen-recorder -O2 sound.o main.o -s $libs g++ -o gpu-screen-recorder -O2 sound.o main.o -s $libs

129
include/CudaLibrary.hpp Normal file
View File

@ -0,0 +1,129 @@
#pragma once
#include <cuda.h>
#include <cudaGL.h>
#include <dlfcn.h>
#include <stdio.h>
typedef CUresult CUDAAPI (*CUINIT)(unsigned int Flags);
typedef CUresult CUDAAPI (*CUDEVICEGETCOUNT)(int *count);
typedef CUresult CUDAAPI (*CUDEVICEGET)(CUdevice *device, int ordinal);
typedef CUresult CUDAAPI (*CUCTXCREATE_V2)(CUcontext *pctx, unsigned int flags, CUdevice dev);
typedef CUresult CUDAAPI (*CUCTXPUSHCURRENT_V2)(CUcontext ctx);
typedef CUresult CUDAAPI (*CUCTXPOPCURRENT_V2)(CUcontext *pctx);
typedef CUresult CUDAAPI (*CUGETERRORSTRING)(CUresult error, const char **pStr);
typedef CUresult CUDAAPI (*CUMEMSETD8_V2)(CUdeviceptr dstDevice, unsigned char uc, size_t N);
typedef CUresult CUDAAPI (*CUMEMCPY2D_V2)(const CUDA_MEMCPY2D *pCopy);
typedef CUresult CUDAAPI (*CUGRAPHICSGLREGISTERIMAGE)(CUgraphicsResource *pCudaResource, GLuint image, GLenum target, unsigned int Flags);
typedef CUresult CUDAAPI (*CUGRAPHICSRESOURCESETMAPFLAGS)(CUgraphicsResource resource, unsigned int flags);
typedef CUresult CUDAAPI (*CUGRAPHICSMAPRESOURCES)(unsigned int count, CUgraphicsResource *resources, CUstream hStream);
typedef CUresult CUDAAPI (*CUGRAPHICSUNREGISTERRESOURCE)(CUgraphicsResource resource);
typedef CUresult CUDAAPI (*CUGRAPHICSSUBRESOURCEGETMAPPEDARRAY)(CUarray *pArray, CUgraphicsResource resource, unsigned int arrayIndex, unsigned int mipLevel);
struct Cuda {
CUINIT cuInit;
CUDEVICEGETCOUNT cuDeviceGetCount;
CUDEVICEGET cuDeviceGet;
CUCTXCREATE_V2 cuCtxCreate_v2;
CUCTXPUSHCURRENT_V2 cuCtxPushCurrent_v2;
CUCTXPOPCURRENT_V2 cuCtxPopCurrent_v2;
CUGETERRORSTRING cuGetErrorString;
CUMEMSETD8_V2 cuMemsetD8_v2;
CUMEMCPY2D_V2 cuMemcpy2D_v2;
CUGRAPHICSGLREGISTERIMAGE cuGraphicsGLRegisterImage;
CUGRAPHICSRESOURCESETMAPFLAGS cuGraphicsResourceSetMapFlags;
CUGRAPHICSMAPRESOURCES cuGraphicsMapResources;
CUGRAPHICSUNREGISTERRESOURCE cuGraphicsUnregisterResource;
CUGRAPHICSSUBRESOURCEGETMAPPEDARRAY cuGraphicsSubResourceGetMappedArray;
~Cuda() {
if(library)
dlclose(library);
}
bool load() {
if(library)
return true;
dlerror(); // clear
void *lib = dlopen("libcuda.so", RTLD_LAZY);
if(!lib) {
fprintf(stderr, "Error: failed to load libcuda.so, error: %s\n", dlerror());
return false;
}
cuInit = (CUINIT)load_symbol(lib, "cuInit");
if(!cuInit)
goto fail;
cuDeviceGetCount = (CUDEVICEGETCOUNT)load_symbol(lib, "cuDeviceGetCount");
if(!cuDeviceGetCount)
goto fail;
cuDeviceGet = (CUDEVICEGET)load_symbol(lib, "cuDeviceGet");
if(!cuDeviceGet)
goto fail;
cuCtxCreate_v2 = (CUCTXCREATE_V2)load_symbol(lib, "cuCtxCreate_v2");
if(!cuCtxCreate_v2)
goto fail;
cuCtxPushCurrent_v2 = (CUCTXPUSHCURRENT_V2)load_symbol(lib, "cuCtxPushCurrent_v2");
if(!cuCtxPushCurrent_v2)
goto fail;
cuCtxPopCurrent_v2 = (CUCTXPOPCURRENT_V2)load_symbol(lib, "cuCtxPopCurrent_v2");
if(!cuCtxPopCurrent_v2)
goto fail;
cuGetErrorString = (CUGETERRORSTRING)load_symbol(lib, "cuGetErrorString");
if(!cuGetErrorString)
goto fail;
cuMemsetD8_v2 = (CUMEMSETD8_V2)load_symbol(lib, "cuMemsetD8_v2");
if(!cuMemsetD8_v2)
goto fail;
cuMemcpy2D_v2 = (CUMEMCPY2D_V2)load_symbol(lib, "cuMemcpy2D_v2");
if(!cuMemcpy2D_v2)
goto fail;
cuGraphicsGLRegisterImage = (CUGRAPHICSGLREGISTERIMAGE)load_symbol(lib, "cuGraphicsGLRegisterImage");
if(!cuGraphicsGLRegisterImage)
goto fail;
cuGraphicsResourceSetMapFlags = (CUGRAPHICSRESOURCESETMAPFLAGS)load_symbol(lib, "cuGraphicsResourceSetMapFlags");
if(!cuGraphicsResourceSetMapFlags)
goto fail;
cuGraphicsMapResources = (CUGRAPHICSMAPRESOURCES)load_symbol(lib, "cuGraphicsMapResources");
if(!cuGraphicsMapResources)
goto fail;
cuGraphicsUnregisterResource = (CUGRAPHICSUNREGISTERRESOURCE)load_symbol(lib, "cuGraphicsUnregisterResource");
if(!cuGraphicsUnregisterResource)
goto fail;
cuGraphicsSubResourceGetMappedArray = (CUGRAPHICSSUBRESOURCEGETMAPPEDARRAY)load_symbol(lib, "cuGraphicsSubResourceGetMappedArray");
if(!cuGraphicsSubResourceGetMappedArray)
goto fail;
library = lib;
return true;
fail:
dlclose(lib);
return false;
}
private:
void* load_symbol(void *library, const char *symbol) {
void *sym = dlsym(library, symbol);
if(!sym)
fprintf(stderr, "Error: missing required symbol %s from libcuda.so\n", symbol);
return sym;
}
private:
void *library = nullptr;
};

View File

@ -1,7 +1,6 @@
#pragma once #pragma once
#include "NvFBC.h" #include "NvFBC.h"
#include <cuda.h>
#include <dlfcn.h> #include <dlfcn.h>
#include <string.h> #include <string.h>
@ -174,7 +173,7 @@ public:
return false; return false;
} }
bool capture(/*out*/ CUdeviceptr *cu_device_ptr, uint32_t *byte_size) { bool capture(/*out*/ void *cu_device_ptr, uint32_t *byte_size) {
if(!library || !fbc_handle_created || !cu_device_ptr || !byte_size) if(!library || !fbc_handle_created || !cu_device_ptr || !byte_size)
return false; return false;

20692
include/cuda.h Normal file

File diff suppressed because it is too large Load Diff

605
include/cudaGL.h Normal file
View File

@ -0,0 +1,605 @@
/*
* Copyright 1993-2014 NVIDIA Corporation. All rights reserved.
*
* NOTICE TO LICENSEE:
*
* This source code and/or documentation ("Licensed Deliverables") are
* subject to NVIDIA intellectual property rights under U.S. and
* international Copyright laws.
*
* These Licensed Deliverables contained herein is PROPRIETARY and
* CONFIDENTIAL to NVIDIA and is being provided under the terms and
* conditions of a form of NVIDIA software license agreement by and
* between NVIDIA and Licensee ("License Agreement") or electronically
* accepted by Licensee. Notwithstanding any terms or conditions to
* the contrary in the License Agreement, reproduction or disclosure
* of the Licensed Deliverables to any third party without the express
* written consent of NVIDIA is prohibited.
*
* NOTWITHSTANDING ANY TERMS OR CONDITIONS TO THE CONTRARY IN THE
* LICENSE AGREEMENT, NVIDIA MAKES NO REPRESENTATION ABOUT THE
* SUITABILITY OF THESE LICENSED DELIVERABLES FOR ANY PURPOSE. IT IS
* PROVIDED "AS IS" WITHOUT EXPRESS OR IMPLIED WARRANTY OF ANY KIND.
* NVIDIA DISCLAIMS ALL WARRANTIES WITH REGARD TO THESE LICENSED
* DELIVERABLES, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY,
* NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE.
* NOTWITHSTANDING ANY TERMS OR CONDITIONS TO THE CONTRARY IN THE
* LICENSE AGREEMENT, IN NO EVENT SHALL NVIDIA BE LIABLE FOR ANY
* SPECIAL, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, OR ANY
* DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
* WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
* ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
* OF THESE LICENSED DELIVERABLES.
*
* U.S. Government End Users. These Licensed Deliverables are a
* "commercial item" as that term is defined at 48 C.F.R. 2.101 (OCT
* 1995), consisting of "commercial computer software" and "commercial
* computer software documentation" as such terms are used in 48
* C.F.R. 12.212 (SEPT 1995) and is provided to the U.S. Government
* only as a commercial end item. Consistent with 48 C.F.R.12.212 and
* 48 C.F.R. 227.7202-1 through 227.7202-4 (JUNE 1995), all
* U.S. Government End Users acquire the Licensed Deliverables with
* only those rights set forth herein.
*
* Any use of the Licensed Deliverables in individual and commercial
* software must include, in the user documentation and internal
* comments to the code, the above Disclaimer and U.S. Government End
* Users Notice.
*/
#ifndef CUDAGL_H
#define CUDAGL_H
#include <cuda.h>
#include <GL/gl.h>
#if defined(__CUDA_API_VERSION_INTERNAL) || defined(__DOXYGEN_ONLY__) || defined(CUDA_ENABLE_DEPRECATED)
#define __CUDA_DEPRECATED
#elif defined(_MSC_VER)
#define __CUDA_DEPRECATED __declspec(deprecated)
#elif defined(__GNUC__)
#define __CUDA_DEPRECATED __attribute__((deprecated))
#else
#define __CUDA_DEPRECATED
#endif
#ifdef CUDA_FORCE_API_VERSION
#error "CUDA_FORCE_API_VERSION is no longer supported."
#endif
#if defined(__CUDA_API_VERSION_INTERNAL) || defined(CUDA_API_PER_THREAD_DEFAULT_STREAM)
#define __CUDA_API_PER_THREAD_DEFAULT_STREAM
#define __CUDA_API_PTDS(api) api ## _ptds
#define __CUDA_API_PTSZ(api) api ## _ptsz
#else
#define __CUDA_API_PTDS(api) api
#define __CUDA_API_PTSZ(api) api
#endif
#define cuGLCtxCreate cuGLCtxCreate_v2
#define cuGLMapBufferObject __CUDA_API_PTDS(cuGLMapBufferObject_v2)
#define cuGLMapBufferObjectAsync __CUDA_API_PTSZ(cuGLMapBufferObjectAsync_v2)
#define cuGLGetDevices cuGLGetDevices_v2
#ifdef __cplusplus
extern "C" {
#endif
/**
* \file cudaGL.h
* \brief Header file for the OpenGL interoperability functions of the
* low-level CUDA driver application programming interface.
*/
/**
* \defgroup CUDA_GL OpenGL Interoperability
* \ingroup CUDA_DRIVER
*
* ___MANBRIEF___ OpenGL interoperability functions of the low-level CUDA
* driver API (___CURRENT_FILE___) ___ENDMANBRIEF___
*
* This section describes the OpenGL interoperability functions of the
* low-level CUDA driver application programming interface. Note that mapping
* of OpenGL resources is performed with the graphics API agnostic, resource
* mapping interface described in \ref CUDA_GRAPHICS "Graphics Interoperability".
*
* @{
*/
#if defined(_WIN32)
#if !defined(WGL_NV_gpu_affinity)
typedef void* HGPUNV;
#endif
#endif /* _WIN32 */
/**
* \brief Registers an OpenGL buffer object
*
* Registers the buffer object specified by \p buffer for access by
* CUDA. A handle to the registered object is returned as \p
* pCudaResource. The register flags \p Flags specify the intended usage,
* as follows:
*
* - ::CU_GRAPHICS_REGISTER_FLAGS_NONE: Specifies no hints about how this
* resource will be used. It is therefore assumed that this resource will be
* read from and written to by CUDA. This is the default value.
* - ::CU_GRAPHICS_REGISTER_FLAGS_READ_ONLY: Specifies that CUDA
* will not write to this resource.
* - ::CU_GRAPHICS_REGISTER_FLAGS_WRITE_DISCARD: Specifies that
* CUDA will not read from this resource and will write over the
* entire contents of the resource, so none of the data previously
* stored in the resource will be preserved.
*
* \param pCudaResource - Pointer to the returned object handle
* \param buffer - name of buffer object to be registered
* \param Flags - Register flags
*
* \return
* ::CUDA_SUCCESS,
* ::CUDA_ERROR_INVALID_HANDLE,
* ::CUDA_ERROR_ALREADY_MAPPED,
* ::CUDA_ERROR_INVALID_CONTEXT,
* \notefnerr
*
* \sa
* ::cuGraphicsUnregisterResource,
* ::cuGraphicsMapResources,
* ::cuGraphicsResourceGetMappedPointer,
* ::cudaGraphicsGLRegisterBuffer
*/
CUresult CUDAAPI cuGraphicsGLRegisterBuffer(CUgraphicsResource *pCudaResource, GLuint buffer, unsigned int Flags);
/**
* \brief Register an OpenGL texture or renderbuffer object
*
* Registers the texture or renderbuffer object specified by \p image for access by CUDA.
* A handle to the registered object is returned as \p pCudaResource.
*
* \p target must match the type of the object, and must be one of ::GL_TEXTURE_2D,
* ::GL_TEXTURE_RECTANGLE, ::GL_TEXTURE_CUBE_MAP, ::GL_TEXTURE_3D, ::GL_TEXTURE_2D_ARRAY,
* or ::GL_RENDERBUFFER.
*
* The register flags \p Flags specify the intended usage, as follows:
*
* - ::CU_GRAPHICS_REGISTER_FLAGS_NONE: Specifies no hints about how this
* resource will be used. It is therefore assumed that this resource will be
* read from and written to by CUDA. This is the default value.
* - ::CU_GRAPHICS_REGISTER_FLAGS_READ_ONLY: Specifies that CUDA
* will not write to this resource.
* - ::CU_GRAPHICS_REGISTER_FLAGS_WRITE_DISCARD: Specifies that
* CUDA will not read from this resource and will write over the
* entire contents of the resource, so none of the data previously
* stored in the resource will be preserved.
* - ::CU_GRAPHICS_REGISTER_FLAGS_SURFACE_LDST: Specifies that CUDA will
* bind this resource to a surface reference.
* - ::CU_GRAPHICS_REGISTER_FLAGS_TEXTURE_GATHER: Specifies that CUDA will perform
* texture gather operations on this resource.
*
* The following image formats are supported. For brevity's sake, the list is abbreviated.
* For ex., {GL_R, GL_RG} X {8, 16} would expand to the following 4 formats
* {GL_R8, GL_R16, GL_RG8, GL_RG16} :
* - GL_RED, GL_RG, GL_RGBA, GL_LUMINANCE, GL_ALPHA, GL_LUMINANCE_ALPHA, GL_INTENSITY
* - {GL_R, GL_RG, GL_RGBA} X {8, 16, 16F, 32F, 8UI, 16UI, 32UI, 8I, 16I, 32I}
* - {GL_LUMINANCE, GL_ALPHA, GL_LUMINANCE_ALPHA, GL_INTENSITY} X
* {8, 16, 16F_ARB, 32F_ARB, 8UI_EXT, 16UI_EXT, 32UI_EXT, 8I_EXT, 16I_EXT, 32I_EXT}
*
* The following image classes are currently disallowed:
* - Textures with borders
* - Multisampled renderbuffers
*
* \param pCudaResource - Pointer to the returned object handle
* \param image - name of texture or renderbuffer object to be registered
* \param target - Identifies the type of object specified by \p image
* \param Flags - Register flags
*
* \return
* ::CUDA_SUCCESS,
* ::CUDA_ERROR_INVALID_HANDLE,
* ::CUDA_ERROR_ALREADY_MAPPED,
* ::CUDA_ERROR_INVALID_CONTEXT,
* \notefnerr
*
* \sa
* ::cuGraphicsUnregisterResource,
* ::cuGraphicsMapResources,
* ::cuGraphicsSubResourceGetMappedArray,
* ::cudaGraphicsGLRegisterImage
*/
CUresult CUDAAPI cuGraphicsGLRegisterImage(CUgraphicsResource *pCudaResource, GLuint image, GLenum target, unsigned int Flags);
#ifdef _WIN32
/**
* \brief Gets the CUDA device associated with hGpu
*
* Returns in \p *pDevice the CUDA device associated with a \p hGpu, if
* applicable.
*
* \param pDevice - Device associated with hGpu
* \param hGpu - Handle to a GPU, as queried via ::WGL_NV_gpu_affinity()
*
* \return
* ::CUDA_SUCCESS,
* ::CUDA_ERROR_DEINITIALIZED,
* ::CUDA_ERROR_NOT_INITIALIZED,
* ::CUDA_ERROR_INVALID_CONTEXT,
* ::CUDA_ERROR_INVALID_VALUE
* \notefnerr
*
* \sa ::cuGLMapBufferObject,
* ::cuGLRegisterBufferObject, ::cuGLUnmapBufferObject,
* ::cuGLUnregisterBufferObject, ::cuGLUnmapBufferObjectAsync,
* ::cuGLSetBufferObjectMapFlags,
* ::cudaWGLGetDevice
*/
CUresult CUDAAPI cuWGLGetDevice(CUdevice *pDevice, HGPUNV hGpu);
#endif /* _WIN32 */
/**
* CUDA devices corresponding to an OpenGL device
*/
typedef enum CUGLDeviceList_enum {
CU_GL_DEVICE_LIST_ALL = 0x01, /**< The CUDA devices for all GPUs used by the current OpenGL context */
CU_GL_DEVICE_LIST_CURRENT_FRAME = 0x02, /**< The CUDA devices for the GPUs used by the current OpenGL context in its currently rendering frame */
CU_GL_DEVICE_LIST_NEXT_FRAME = 0x03, /**< The CUDA devices for the GPUs to be used by the current OpenGL context in the next frame */
} CUGLDeviceList;
/**
* \brief Gets the CUDA devices associated with the current OpenGL context
*
* Returns in \p *pCudaDeviceCount the number of CUDA-compatible devices
* corresponding to the current OpenGL context. Also returns in \p *pCudaDevices
* at most cudaDeviceCount of the CUDA-compatible devices corresponding to
* the current OpenGL context. If any of the GPUs being used by the current OpenGL
* context are not CUDA capable then the call will return CUDA_ERROR_NO_DEVICE.
*
* The \p deviceList argument may be any of the following:
* - ::CU_GL_DEVICE_LIST_ALL: Query all devices used by the current OpenGL context.
* - ::CU_GL_DEVICE_LIST_CURRENT_FRAME: Query the devices used by the current OpenGL context to
* render the current frame (in SLI).
* - ::CU_GL_DEVICE_LIST_NEXT_FRAME: Query the devices used by the current OpenGL context to
* render the next frame (in SLI). Note that this is a prediction, it can't be guaranteed that
* this is correct in all cases.
*
* \param pCudaDeviceCount - Returned number of CUDA devices.
* \param pCudaDevices - Returned CUDA devices.
* \param cudaDeviceCount - The size of the output device array pCudaDevices.
* \param deviceList - The set of devices to return.
*
* \return
* ::CUDA_SUCCESS,
* ::CUDA_ERROR_NO_DEVICE,
* ::CUDA_ERROR_INVALID_VALUE,
* ::CUDA_ERROR_INVALID_CONTEXT,
* ::CUDA_ERROR_INVALID_GRAPHICS_CONTEXT
*
* \notefnerr
*
* \sa
* ::cuWGLGetDevice,
* ::cudaGLGetDevices
*/
CUresult CUDAAPI cuGLGetDevices(unsigned int *pCudaDeviceCount, CUdevice *pCudaDevices, unsigned int cudaDeviceCount, CUGLDeviceList deviceList);
/**
* \defgroup CUDA_GL_DEPRECATED OpenGL Interoperability [DEPRECATED]
*
* ___MANBRIEF___ deprecated OpenGL interoperability functions of the low-level
* CUDA driver API (___CURRENT_FILE___) ___ENDMANBRIEF___
*
* This section describes deprecated OpenGL interoperability functionality.
*
* @{
*/
/** Flags to map or unmap a resource */
typedef enum CUGLmap_flags_enum {
CU_GL_MAP_RESOURCE_FLAGS_NONE = 0x00,
CU_GL_MAP_RESOURCE_FLAGS_READ_ONLY = 0x01,
CU_GL_MAP_RESOURCE_FLAGS_WRITE_DISCARD = 0x02,
} CUGLmap_flags;
/**
* \brief Create a CUDA context for interoperability with OpenGL
*
* \deprecated This function is deprecated as of Cuda 5.0.
*
* This function is deprecated and should no longer be used. It is
* no longer necessary to associate a CUDA context with an OpenGL
* context in order to achieve maximum interoperability performance.
*
* \param pCtx - Returned CUDA context
* \param Flags - Options for CUDA context creation
* \param device - Device on which to create the context
*
* \return
* ::CUDA_SUCCESS,
* ::CUDA_ERROR_DEINITIALIZED,
* ::CUDA_ERROR_NOT_INITIALIZED,
* ::CUDA_ERROR_INVALID_CONTEXT,
* ::CUDA_ERROR_INVALID_VALUE,
* ::CUDA_ERROR_OUT_OF_MEMORY
* \notefnerr
*
* \sa ::cuCtxCreate, ::cuGLInit, ::cuGLMapBufferObject,
* ::cuGLRegisterBufferObject, ::cuGLUnmapBufferObject,
* ::cuGLUnregisterBufferObject, ::cuGLMapBufferObjectAsync,
* ::cuGLUnmapBufferObjectAsync, ::cuGLSetBufferObjectMapFlags,
* ::cuWGLGetDevice
*/
__CUDA_DEPRECATED CUresult CUDAAPI cuGLCtxCreate(CUcontext *pCtx, unsigned int Flags, CUdevice device );
/**
* \brief Initializes OpenGL interoperability
*
* \deprecated This function is deprecated as of Cuda 3.0.
*
* Initializes OpenGL interoperability. This function is deprecated
* and calling it is no longer required. It may fail if the needed
* OpenGL driver facilities are not available.
*
* \return
* ::CUDA_SUCCESS,
* ::CUDA_ERROR_DEINITIALIZED,
* ::CUDA_ERROR_NOT_INITIALIZED,
* ::CUDA_ERROR_INVALID_CONTEXT,
* ::CUDA_ERROR_UNKNOWN
* \notefnerr
*
* \sa ::cuGLMapBufferObject,
* ::cuGLRegisterBufferObject, ::cuGLUnmapBufferObject,
* ::cuGLUnregisterBufferObject, ::cuGLMapBufferObjectAsync,
* ::cuGLUnmapBufferObjectAsync, ::cuGLSetBufferObjectMapFlags,
* ::cuWGLGetDevice
*/
__CUDA_DEPRECATED CUresult CUDAAPI cuGLInit(void);
/**
* \brief Registers an OpenGL buffer object
*
* \deprecated This function is deprecated as of Cuda 3.0.
*
* Registers the buffer object specified by \p buffer for access by
* CUDA. This function must be called before CUDA can map the buffer
* object. There must be a valid OpenGL context bound to the current
* thread when this function is called, and the buffer name is
* resolved by that context.
*
* \param buffer - The name of the buffer object to register.
*
* \return
* ::CUDA_SUCCESS,
* ::CUDA_ERROR_DEINITIALIZED,
* ::CUDA_ERROR_NOT_INITIALIZED,
* ::CUDA_ERROR_INVALID_CONTEXT,
* ::CUDA_ERROR_ALREADY_MAPPED
* \notefnerr
*
* \sa ::cuGraphicsGLRegisterBuffer
*/
__CUDA_DEPRECATED CUresult CUDAAPI cuGLRegisterBufferObject(GLuint buffer);
/**
* \brief Maps an OpenGL buffer object
*
* \deprecated This function is deprecated as of Cuda 3.0.
*
* Maps the buffer object specified by \p buffer into the address space of the
* current CUDA context and returns in \p *dptr and \p *size the base pointer
* and size of the resulting mapping.
*
* There must be a valid OpenGL context bound to the current thread
* when this function is called. This must be the same context, or a
* member of the same shareGroup, as the context that was bound when
* the buffer was registered.
*
* All streams in the current CUDA context are synchronized with the
* current GL context.
*
* \param dptr - Returned mapped base pointer
* \param size - Returned size of mapping
* \param buffer - The name of the buffer object to map
*
* \return
* ::CUDA_SUCCESS,
* ::CUDA_ERROR_DEINITIALIZED,
* ::CUDA_ERROR_NOT_INITIALIZED,
* ::CUDA_ERROR_INVALID_CONTEXT,
* ::CUDA_ERROR_INVALID_VALUE,
* ::CUDA_ERROR_MAP_FAILED
* \notefnerr
*
* \sa ::cuGraphicsMapResources
*/
__CUDA_DEPRECATED CUresult CUDAAPI cuGLMapBufferObject(CUdeviceptr *dptr, size_t *size, GLuint buffer);
/**
* \brief Unmaps an OpenGL buffer object
*
* \deprecated This function is deprecated as of Cuda 3.0.
*
* Unmaps the buffer object specified by \p buffer for access by CUDA.
*
* There must be a valid OpenGL context bound to the current thread
* when this function is called. This must be the same context, or a
* member of the same shareGroup, as the context that was bound when
* the buffer was registered.
*
* All streams in the current CUDA context are synchronized with the
* current GL context.
*
* \param buffer - Buffer object to unmap
*
* \return
* ::CUDA_SUCCESS,
* ::CUDA_ERROR_DEINITIALIZED,
* ::CUDA_ERROR_NOT_INITIALIZED,
* ::CUDA_ERROR_INVALID_CONTEXT,
* ::CUDA_ERROR_INVALID_VALUE
* \notefnerr
*
* \sa ::cuGraphicsUnmapResources
*/
__CUDA_DEPRECATED CUresult CUDAAPI cuGLUnmapBufferObject(GLuint buffer);
/**
* \brief Unregister an OpenGL buffer object
*
* \deprecated This function is deprecated as of Cuda 3.0.
*
* Unregisters the buffer object specified by \p buffer. This
* releases any resources associated with the registered buffer.
* After this call, the buffer may no longer be mapped for access by
* CUDA.
*
* There must be a valid OpenGL context bound to the current thread
* when this function is called. This must be the same context, or a
* member of the same shareGroup, as the context that was bound when
* the buffer was registered.
*
* \param buffer - Name of the buffer object to unregister
*
* \return
* ::CUDA_SUCCESS,
* ::CUDA_ERROR_DEINITIALIZED,
* ::CUDA_ERROR_NOT_INITIALIZED,
* ::CUDA_ERROR_INVALID_CONTEXT,
* ::CUDA_ERROR_INVALID_VALUE
* \notefnerr
*
* \sa ::cuGraphicsUnregisterResource
*/
__CUDA_DEPRECATED CUresult CUDAAPI cuGLUnregisterBufferObject(GLuint buffer);
/**
* \brief Set the map flags for an OpenGL buffer object
*
* \deprecated This function is deprecated as of Cuda 3.0.
*
* Sets the map flags for the buffer object specified by \p buffer.
*
* Changes to \p Flags will take effect the next time \p buffer is mapped.
* The \p Flags argument may be any of the following:
* - ::CU_GL_MAP_RESOURCE_FLAGS_NONE: Specifies no hints about how this
* resource will be used. It is therefore assumed that this resource will be
* read from and written to by CUDA kernels. This is the default value.
* - ::CU_GL_MAP_RESOURCE_FLAGS_READ_ONLY: Specifies that CUDA kernels which
* access this resource will not write to this resource.
* - ::CU_GL_MAP_RESOURCE_FLAGS_WRITE_DISCARD: Specifies that CUDA kernels
* which access this resource will not read from this resource and will
* write over the entire contents of the resource, so none of the data
* previously stored in the resource will be preserved.
*
* If \p buffer has not been registered for use with CUDA, then
* ::CUDA_ERROR_INVALID_HANDLE is returned. If \p buffer is presently
* mapped for access by CUDA, then ::CUDA_ERROR_ALREADY_MAPPED is returned.
*
* There must be a valid OpenGL context bound to the current thread
* when this function is called. This must be the same context, or a
* member of the same shareGroup, as the context that was bound when
* the buffer was registered.
*
* \param buffer - Buffer object to unmap
* \param Flags - Map flags
*
* \return
* ::CUDA_SUCCESS,
* ::CUDA_ERROR_NOT_INITIALIZED,
* ::CUDA_ERROR_INVALID_HANDLE,
* ::CUDA_ERROR_ALREADY_MAPPED,
* ::CUDA_ERROR_INVALID_CONTEXT,
* \notefnerr
*
* \sa ::cuGraphicsResourceSetMapFlags
*/
__CUDA_DEPRECATED CUresult CUDAAPI cuGLSetBufferObjectMapFlags(GLuint buffer, unsigned int Flags);
/**
* \brief Maps an OpenGL buffer object
*
* \deprecated This function is deprecated as of Cuda 3.0.
*
* Maps the buffer object specified by \p buffer into the address space of the
* current CUDA context and returns in \p *dptr and \p *size the base pointer
* and size of the resulting mapping.
*
* There must be a valid OpenGL context bound to the current thread
* when this function is called. This must be the same context, or a
* member of the same shareGroup, as the context that was bound when
* the buffer was registered.
*
* Stream \p hStream in the current CUDA context is synchronized with
* the current GL context.
*
* \param dptr - Returned mapped base pointer
* \param size - Returned size of mapping
* \param buffer - The name of the buffer object to map
* \param hStream - Stream to synchronize
*
* \return
* ::CUDA_SUCCESS,
* ::CUDA_ERROR_DEINITIALIZED,
* ::CUDA_ERROR_NOT_INITIALIZED,
* ::CUDA_ERROR_INVALID_CONTEXT,
* ::CUDA_ERROR_INVALID_VALUE,
* ::CUDA_ERROR_MAP_FAILED
* \notefnerr
*
* \sa ::cuGraphicsMapResources
*/
__CUDA_DEPRECATED CUresult CUDAAPI cuGLMapBufferObjectAsync(CUdeviceptr *dptr, size_t *size, GLuint buffer, CUstream hStream);
/**
* \brief Unmaps an OpenGL buffer object
*
* \deprecated This function is deprecated as of Cuda 3.0.
*
* Unmaps the buffer object specified by \p buffer for access by CUDA.
*
* There must be a valid OpenGL context bound to the current thread
* when this function is called. This must be the same context, or a
* member of the same shareGroup, as the context that was bound when
* the buffer was registered.
*
* Stream \p hStream in the current CUDA context is synchronized with
* the current GL context.
*
* \param buffer - Name of the buffer object to unmap
* \param hStream - Stream to synchronize
*
* \return
* ::CUDA_SUCCESS,
* ::CUDA_ERROR_DEINITIALIZED,
* ::CUDA_ERROR_NOT_INITIALIZED,
* ::CUDA_ERROR_INVALID_CONTEXT,
* ::CUDA_ERROR_INVALID_VALUE
* \notefnerr
*
* \sa ::cuGraphicsUnmapResources
*/
__CUDA_DEPRECATED CUresult CUDAAPI cuGLUnmapBufferObjectAsync(GLuint buffer, CUstream hStream);
/** @} */ /* END CUDA_GL_DEPRECATED */
/** @} */ /* END CUDA_GL */
#if defined(__CUDA_API_VERSION_INTERNAL)
#undef cuGLCtxCreate
#undef cuGLMapBufferObject
#undef cuGLMapBufferObjectAsync
#undef cuGLGetDevices
CUresult CUDAAPI cuGLGetDevices(unsigned int *pCudaDeviceCount, CUdevice *pCudaDevices, unsigned int cudaDeviceCount, CUGLDeviceList deviceList);
CUresult CUDAAPI cuGLMapBufferObject_v2(CUdeviceptr *dptr, size_t *size, GLuint buffer);
CUresult CUDAAPI cuGLMapBufferObjectAsync_v2(CUdeviceptr *dptr, size_t *size, GLuint buffer, CUstream hStream);
CUresult CUDAAPI cuGLCtxCreate(CUcontext *pCtx, unsigned int Flags, CUdevice device );
CUresult CUDAAPI cuGLMapBufferObject(CUdeviceptr_v1 *dptr, unsigned int *size, GLuint buffer);
CUresult CUDAAPI cuGLMapBufferObjectAsync(CUdeviceptr_v1 *dptr, unsigned int *size, GLuint buffer, CUstream hStream);
#endif /* __CUDA_API_VERSION_INTERNAL */
#ifdef __cplusplus
};
#endif
#undef __CUDA_DEPRECATED
#endif

View File

@ -5,19 +5,19 @@ cd "$script_dir"
[ $(id -u) -ne 0 ] && echo "You need root privileges to run the install script" && exit 1 [ $(id -u) -ne 0 ] && echo "You need root privileges to run the install script" && exit 1
dpkg -l nvidia-cuda-dev > /dev/null 2>&1 dpkg -l cuda > /dev/null 2>&1
cuda_missing="$?" cuda_missing="$?"
set -e set -e
apt-get -y install build-essential nvidia-cuda-dev\ apt-get -y install build-essential cuda\
libswresample-dev libavformat-dev libavcodec-dev libavutil-dev\ libswresample-dev libavformat-dev libavcodec-dev libavutil-dev\
libx11-dev libxcomposite-dev\ libx11-dev libxcomposite-dev\
libglew-dev libglfw3-dev\ libglew-dev libglfw3-dev\
libpulse-dev libpulse-dev
dependencies="glew libavcodec libavformat libavutil x11 xcomposite glfw3 libpulse libswresample" dependencies="glew libavcodec libavformat libavutil x11 xcomposite glfw3 libpulse libswresample"
includes="$(pkg-config --cflags $dependencies) -I/opt/cuda/targets/x86_64-linux/include" includes="$(pkg-config --cflags $dependencies) -Iinclude"
libs="$(pkg-config --libs $dependencies) /usr/lib/x86_64-linux-gnu/stubs/libcuda.so -ldl -pthread -lm" libs="$(pkg-config --libs $dependencies) -ldl -pthread -lm"
g++ -c src/sound.cpp -O2 $includes g++ -c src/sound.cpp -O2 $includes
g++ -c src/main.cpp -O2 $includes g++ -c src/main.cpp -O2 $includes
g++ -o gpu-screen-recorder -O2 sound.o main.o -s $libs g++ -o gpu-screen-recorder -O2 sound.o main.o -s $libs

View File

@ -5,8 +5,7 @@ version = "1.1.0"
platforms = ["posix"] platforms = ["posix"]
[config] [config]
include_dirs = ["/opt/cuda/targets/x86_64-linux/include"] include_dirs = ["include"]
libs = ["/usr/lib64/libcuda.so"]
[dependencies] [dependencies]
glew = ">=2" glew = ">=2"

View File

@ -30,14 +30,16 @@
#include <unistd.h> #include <unistd.h>
#include <fcntl.h> #include <fcntl.h>
#include "../include/sound.hpp"
#define GLX_GLXEXT_PROTOTYPES #define GLX_GLXEXT_PROTOTYPES
#include <GL/glew.h> #include <GL/glew.h>
#include <GL/glx.h> #include <GL/glx.h>
#include <GL/glxext.h> #include <GL/glxext.h>
#include <GLFW/glfw3.h> #include <GLFW/glfw3.h>
#include "../include/sound.hpp"
#include "../include/NvFBCLibrary.hpp"
#include "../include/CudaLibrary.hpp"
#include <X11/extensions/Xcomposite.h> #include <X11/extensions/Xcomposite.h>
//#include <X11/Xatom.h> //#include <X11/Xatom.h>
@ -52,14 +54,11 @@ extern "C" {
#include <libavutil/avutil.h> #include <libavutil/avutil.h>
#include <libavutil/time.h> #include <libavutil/time.h>
} }
#include <cudaGL.h>
extern "C" { extern "C" {
#include <libavutil/hwcontext.h> #include <libavutil/hwcontext.h>
} }
#include "../include/NvFBCLibrary.hpp"
#include <deque> #include <deque>
#include <future> #include <future>
@ -71,6 +70,8 @@ static const int VIDEO_STREAM_INDEX = 0;
static thread_local char av_error_buffer[AV_ERROR_MAX_STRING_SIZE]; static thread_local char av_error_buffer[AV_ERROR_MAX_STRING_SIZE];
static Cuda cuda;
static char* av_error_to_string(int err) { static char* av_error_to_string(int err) {
if(av_strerror(err, av_error_buffer, sizeof(av_error_buffer)) < 0) if(av_strerror(err, av_error_buffer, sizeof(av_error_buffer)) < 0)
strcpy(av_error_buffer, "Unknown error"); strcpy(av_error_buffer, "Unknown error");
@ -692,22 +693,22 @@ static void open_video(AVCodecContext *codec_context,
if(window_pixmap.target_texture_id != 0) { if(window_pixmap.target_texture_id != 0) {
CUresult res; CUresult res;
CUcontext old_ctx; CUcontext old_ctx;
res = cuCtxPopCurrent(&old_ctx); res = cuda.cuCtxPopCurrent_v2(&old_ctx);
res = cuCtxPushCurrent(cuda_context); res = cuda.cuCtxPushCurrent_v2(cuda_context);
res = cuGraphicsGLRegisterImage( res = cuda.cuGraphicsGLRegisterImage(
cuda_graphics_resource, window_pixmap.target_texture_id, GL_TEXTURE_2D, cuda_graphics_resource, window_pixmap.target_texture_id, GL_TEXTURE_2D,
CU_GRAPHICS_REGISTER_FLAGS_READ_ONLY); CU_GRAPHICS_REGISTER_FLAGS_READ_ONLY);
// cuGraphicsUnregisterResource(*cuda_graphics_resource); // cuda.cuGraphicsUnregisterResource(*cuda_graphics_resource);
if (res != CUDA_SUCCESS) { if (res != CUDA_SUCCESS) {
const char *err_str; const char *err_str;
cuGetErrorString(res, &err_str); cuda.cuGetErrorString(res, &err_str);
fprintf(stderr, fprintf(stderr,
"Error: cuGraphicsGLRegisterImage failed, error %s, texture " "Error: cuda.cuGraphicsGLRegisterImage failed, error %s, texture "
"id: %u\n", "id: %u\n",
err_str, window_pixmap.target_texture_id); err_str, window_pixmap.target_texture_id);
exit(1); exit(1);
} }
res = cuCtxPopCurrent(&old_ctx); res = cuda.cuCtxPopCurrent_v2(&old_ctx);
} }
} }
@ -1090,37 +1091,42 @@ int main(int argc, char **argv) {
replay_buffer_size_secs += 5; // Add a few seconds to account of lost packets because of non-keyframe packets skipped replay_buffer_size_secs += 5; // Add a few seconds to account of lost packets because of non-keyframe packets skipped
} }
if(!cuda.load()) {
fprintf(stderr, "Error: failed to load cuda\n");
return 2;
}
CUresult res; CUresult res;
res = cuInit(0); res = cuda.cuInit(0);
if(res != CUDA_SUCCESS) { if(res != CUDA_SUCCESS) {
const char *err_str; const char *err_str;
cuGetErrorString(res, &err_str); cuda.cuGetErrorString(res, &err_str);
fprintf(stderr, "Error: cuInit failed, error %s (result: %d)\n", err_str, res); fprintf(stderr, "Error: cuInit failed, error %s (result: %d)\n", err_str, res);
return 1; return 1;
} }
int nGpu = 0; int nGpu = 0;
cuDeviceGetCount(&nGpu); cuda.cuDeviceGetCount(&nGpu);
if (nGpu <= 0) { if (nGpu <= 0) {
fprintf(stderr, "Error: no cuda supported devices found\n"); fprintf(stderr, "Error: no cuda supported devices found\n");
return 1; return 1;
} }
CUdevice cu_dev; CUdevice cu_dev;
res = cuDeviceGet(&cu_dev, 0); res = cuda.cuDeviceGet(&cu_dev, 0);
if(res != CUDA_SUCCESS) { if(res != CUDA_SUCCESS) {
const char *err_str; const char *err_str;
cuGetErrorString(res, &err_str); cuda.cuGetErrorString(res, &err_str);
fprintf(stderr, "Error: unable to get CUDA device, error: %s (result: %d)\n", err_str, res); fprintf(stderr, "Error: unable to get CUDA device, error: %s (result: %d)\n", err_str, res);
return 1; return 1;
} }
CUcontext cu_ctx; CUcontext cu_ctx;
res = cuCtxCreate_v2(&cu_ctx, CU_CTX_SCHED_AUTO, cu_dev); res = cuda.cuCtxCreate_v2(&cu_ctx, CU_CTX_SCHED_AUTO, cu_dev);
if(res != CUDA_SUCCESS) { if(res != CUDA_SUCCESS) {
const char *err_str; const char *err_str;
cuGetErrorString(res, &err_str); cuda.cuGetErrorString(res, &err_str);
fprintf(stderr, "Error: unable to create CUDA context, error: %s (result: %d)\n", err_str, res); fprintf(stderr, "Error: unable to create CUDA context, error: %s (result: %d)\n", err_str, res);
return 1; return 1;
} }
@ -1386,16 +1392,16 @@ int main(int argc, char **argv) {
CUcontext old_ctx; CUcontext old_ctx;
CUarray mapped_array; CUarray mapped_array;
if(src_window_id) { if(src_window_id) {
res = cuCtxPopCurrent(&old_ctx); res = cuda.cuCtxPopCurrent_v2(&old_ctx);
res = cuCtxPushCurrent(cu_ctx); res = cuda.cuCtxPushCurrent_v2(cu_ctx);
// Get texture // Get texture
res = cuGraphicsResourceSetMapFlags( res = cuda.cuGraphicsResourceSetMapFlags(
cuda_graphics_resource, CU_GRAPHICS_MAP_RESOURCE_FLAGS_READ_ONLY); cuda_graphics_resource, CU_GRAPHICS_MAP_RESOURCE_FLAGS_READ_ONLY);
res = cuGraphicsMapResources(1, &cuda_graphics_resource, 0); res = cuda.cuGraphicsMapResources(1, &cuda_graphics_resource, 0);
// Map texture to cuda array // Map texture to cuda array
res = cuGraphicsSubResourceGetMappedArray(&mapped_array, res = cuda.cuGraphicsSubResourceGetMappedArray(&mapped_array,
cuda_graphics_resource, 0, 0); cuda_graphics_resource, 0, 0);
} }
@ -1561,6 +1567,8 @@ int main(int argc, char **argv) {
while(XCheckTypedWindowEvent(dpy, src_window_id, ConfigureNotify, &e)) {} while(XCheckTypedWindowEvent(dpy, src_window_id, ConfigureNotify, &e)) {}
window_x = e.xconfigure.x; window_x = e.xconfigure.x;
window_y = e.xconfigure.y; window_y = e.xconfigure.y;
Window c;
XTranslateCoordinates(dpy, src_window_id, DefaultRootWindow(dpy), 0, 0, &window_x, &window_y, &c);
// Window resize // Window resize
if(e.xconfigure.width != (int)window_width || e.xconfigure.height != (int)window_height) { if(e.xconfigure.width != (int)window_width || e.xconfigure.height != (int)window_height) {
window_width = std::max(0, e.xconfigure.width); window_width = std::max(0, e.xconfigure.width);
@ -1579,25 +1587,25 @@ int main(int argc, char **argv) {
//video_stream->codec->width = window_pixmap.texture_width & ~1; //video_stream->codec->width = window_pixmap.texture_width & ~1;
//video_stream->codec->height = window_pixmap.texture_height & ~1; //video_stream->codec->height = window_pixmap.texture_height & ~1;
cuGraphicsUnregisterResource(cuda_graphics_resource); cuda.cuGraphicsUnregisterResource(cuda_graphics_resource);
res = cuGraphicsGLRegisterImage( res = cuda.cuGraphicsGLRegisterImage(
&cuda_graphics_resource, window_pixmap.target_texture_id, GL_TEXTURE_2D, &cuda_graphics_resource, window_pixmap.target_texture_id, GL_TEXTURE_2D,
CU_GRAPHICS_REGISTER_FLAGS_READ_ONLY); CU_GRAPHICS_REGISTER_FLAGS_READ_ONLY);
if (res != CUDA_SUCCESS) { if (res != CUDA_SUCCESS) {
const char *err_str; const char *err_str;
cuGetErrorString(res, &err_str); cuda.cuGetErrorString(res, &err_str);
fprintf(stderr, fprintf(stderr,
"Error: cuGraphicsGLRegisterImage failed, error %s, texture " "Error: cuda.cuGraphicsGLRegisterImage failed, error %s, texture "
"id: %u\n", "id: %u\n",
err_str, window_pixmap.target_texture_id); err_str, window_pixmap.target_texture_id);
running = false; running = false;
break; break;
} }
res = cuGraphicsResourceSetMapFlags( res = cuda.cuGraphicsResourceSetMapFlags(
cuda_graphics_resource, CU_GRAPHICS_MAP_RESOURCE_FLAGS_READ_ONLY); cuda_graphics_resource, CU_GRAPHICS_MAP_RESOURCE_FLAGS_READ_ONLY);
res = cuGraphicsMapResources(1, &cuda_graphics_resource, 0); res = cuda.cuGraphicsMapResources(1, &cuda_graphics_resource, 0);
res = cuGraphicsSubResourceGetMappedArray(&mapped_array, cuda_graphics_resource, 0, 0); res = cuda.cuGraphicsSubResourceGetMappedArray(&mapped_array, cuda_graphics_resource, 0, 0);
av_frame_free(&frame); av_frame_free(&frame);
frame = av_frame_alloc(); frame = av_frame_alloc();
@ -1626,7 +1634,9 @@ int main(int argc, char **argv) {
else else
frame->height = record_height & ~1; frame->height = record_height & ~1;
cuMemsetD8((CUdeviceptr)frame->data[0], 0, record_width * record_height * 4); // Make the new completely black to clear unused parts
// TODO: cuMemsetD32?
cuda.cuMemsetD8_v2((CUdeviceptr)frame->data[0], 0, record_width * record_height * 4);
} }
} }
@ -1732,7 +1742,7 @@ int main(int argc, char **argv) {
memcpy_struct.dstPitch = frame->linesize[0]; memcpy_struct.dstPitch = frame->linesize[0];
memcpy_struct.WidthInBytes = frame->width * 4; memcpy_struct.WidthInBytes = frame->width * 4;
memcpy_struct.Height = frame->height; memcpy_struct.Height = frame->height;
cuMemcpy2D(&memcpy_struct); cuda.cuMemcpy2D_v2(&memcpy_struct);
frame_captured = true; frame_captured = true;
} else { } else {
@ -1744,7 +1754,7 @@ int main(int argc, char **argv) {
frame_captured = nv_fbc_library.capture(&src_cu_device_ptr, &byte_size); frame_captured = nv_fbc_library.capture(&src_cu_device_ptr, &byte_size);
frame->data[0] = (uint8_t*)src_cu_device_ptr; frame->data[0] = (uint8_t*)src_cu_device_ptr;
} }
// res = cuCtxPopCurrent(&old_ctx); // res = cuda.cuCtxPopCurrent_v2(&old_ctx);
} }
const double this_video_frame_time = clock_get_monotonic_seconds(); const double this_video_frame_time = clock_get_monotonic_seconds();