Skip to content

Commit

Permalink
fix: In Linux CL/GL sharing
Browse files Browse the repository at this point in the history
- always issue flush request before export

Apparently it's expected to flush the object (which might convert them
from one format to another for export, or remove aux buffer uses or
anything not supported by export).

- use modifier to select tiling mode

Previously we just assumed that whatever tiling mode was picked by mesa
will match the one picked by GMMLIB but that's not always the case
and in particular on Arc and Xe it doesn't work ... Mesa picks Tile4
and GMMLIB picks Tile64 ...

Fixes: #761
Fixes: #736

Signed-off-by: Sylvain Munaut <[email protected]>
Signed-off-by: Mateusz Jablonski <[email protected]>
  • Loading branch information
JablonskiMateusz authored and Compute-Runtime-Automation committed Sep 6, 2024
1 parent 9d5f8eb commit 54bda0e
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 11 deletions.
19 changes: 15 additions & 4 deletions opencl/source/sharings/gl/linux/gl_buffer_linux.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,11 @@ using namespace NEO;
Buffer *GlBuffer::createSharedGlBuffer(Context *context, cl_mem_flags flags, unsigned int bufferId, cl_int *errcodeRet) {
ErrorCodeHelper errorCode(errcodeRet, CL_SUCCESS);

/* Prepare export request */
/* Prepare flush & export request */
struct mesa_glinterop_export_in objIn = {};
struct mesa_glinterop_export_out objOut = {};
struct mesa_glinterop_flush_out flushOut = {};
int fenceFd = -1;

objIn.version = 2;
objIn.target = GL_ARRAY_BUFFER;
Expand All @@ -48,13 +50,22 @@ Buffer *GlBuffer::createSharedGlBuffer(Context *context, cl_mem_flags flags, uns
return nullptr;
}

flushOut.version = 1;
flushOut.fence_fd = &fenceFd;

objOut.version = 2;

/* Call MESA interop */
GLSharingFunctionsLinux *sharingFunctions = context->getSharing<GLSharingFunctionsLinux>();
bool success;
int retValue;

success = sharingFunctions->flushObjectsAndWait(1, &objIn, &flushOut, &retValue);
if (success) {
retValue = sharingFunctions->exportObject(&objIn, &objOut);
}

int retValue = sharingFunctions->exportObject(&objIn, &objOut);
if ((retValue != MESA_GLINTEROP_SUCCESS) || (objOut.version != 2)) {
if (!success || (retValue != MESA_GLINTEROP_SUCCESS) || (objOut.version != 2)) {
switch (retValue) {
case MESA_GLINTEROP_INVALID_DISPLAY:
case MESA_GLINTEROP_INVALID_CONTEXT:
Expand Down Expand Up @@ -235,4 +246,4 @@ void GlBuffer::releaseResource(MemObj *memObject, uint32_t rootDeviceIndex) {
memoryManager->closeSharedHandle(memObject->getGraphicsAllocation(rootDeviceIndex));
}

void GlBuffer::callReleaseResource(bool createOrDestroy) {}
void GlBuffer::callReleaseResource(bool createOrDestroy) {}
7 changes: 5 additions & 2 deletions opencl/source/sharings/gl/linux/gl_sharing_linux.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -122,10 +122,13 @@ GLboolean GLSharingFunctionsLinux::initGLFunctions() {

return 1;
}
bool GLSharingFunctionsLinux::flushObjectsAndWait(unsigned count, struct mesa_glinterop_export_in *resources, struct mesa_glinterop_flush_out *out) {
bool GLSharingFunctionsLinux::flushObjectsAndWait(unsigned count, struct mesa_glinterop_export_in *resources, struct mesa_glinterop_flush_out *out, int *retValPtr) {
/* Call MESA interop */
int retValue = flushObjects(1, resources, out);
if (retValue != MESA_GLINTEROP_SUCCESS) {
if (retValPtr) {
*retValPtr = retValue;
}
if ((retValue != MESA_GLINTEROP_SUCCESS) && (out->version == 1)) {
return false;
}
auto fenceFd = *out->fence_fd;
Expand Down
2 changes: 1 addition & 1 deletion opencl/source/sharings/gl/linux/gl_sharing_linux.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ class GLSharingFunctionsLinux : public GLSharingFunctions {
return -ENOTSUP;
}
}
bool flushObjectsAndWait(unsigned count, struct mesa_glinterop_export_in *resources, struct mesa_glinterop_flush_out *out);
bool flushObjectsAndWait(unsigned count, struct mesa_glinterop_export_in *resources, struct mesa_glinterop_flush_out *out, int *retValPtr = nullptr);
GLContext getBackupContextHandle() {
return glHGLRCHandleBkpCtx;
}
Expand Down
41 changes: 37 additions & 4 deletions opencl/source/sharings/gl/linux/gl_texture_linux.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

#include "CL/cl_gl.h"
#include "config.h"
#include "third_party/uapi/upstream/drm/drm_fourcc.h"
#include <GL/gl.h>

namespace NEO {
Expand All @@ -39,9 +40,11 @@ Image *GlTexture::createSharedGlTexture(Context *context, cl_mem_flags flags, cl
cl_image_format imgFormat = {};
McsSurfaceInfo mcsSurfaceInfo = {};

/* Prepare export request */
/* Prepare flush & export request */
struct mesa_glinterop_export_in texIn = {};
struct mesa_glinterop_export_out texOut = {};
struct mesa_glinterop_flush_out flushOut = {};
int fenceFd = -1;

texIn.version = 2;
texIn.target = getBaseTargetType(target);
Expand Down Expand Up @@ -69,13 +72,22 @@ Image *GlTexture::createSharedGlTexture(Context *context, cl_mem_flags flags, cl
return nullptr;
}

flushOut.version = 1;
flushOut.fence_fd = &fenceFd;

texOut.version = 2;

/* Call MESA interop */
GLSharingFunctionsLinux *sharingFunctions = context->getSharing<GLSharingFunctionsLinux>();
bool success;
int retValue;

success = sharingFunctions->flushObjectsAndWait(1, &texIn, &flushOut, &retValue);
if (success) {
retValue = sharingFunctions->exportObject(&texIn, &texOut);
}

int retValue = sharingFunctions->exportObject(&texIn, &texOut);
if ((retValue != MESA_GLINTEROP_SUCCESS) || (texOut.version != 2)) {
if (!success || (retValue != MESA_GLINTEROP_SUCCESS) || (texOut.version != 2)) {
switch (retValue) {
case MESA_GLINTEROP_INVALID_DISPLAY:
case MESA_GLINTEROP_INVALID_CONTEXT:
Expand Down Expand Up @@ -124,7 +136,28 @@ Image *GlTexture::createSharedGlTexture(Context *context, cl_mem_flags flags, cl
imgInfo.imgDesc.imageHeight = imgDesc.image_height;
imgInfo.imgDesc.imageDepth = imgDesc.image_depth;
imgInfo.imgDesc.imageRowPitch = imgDesc.image_row_pitch;
imgInfo.linearStorage = (texOut.modifier == 0);

switch (texOut.modifier) {
case DRM_FORMAT_MOD_LINEAR:
imgInfo.linearStorage = true;
break;
case I915_FORMAT_MOD_X_TILED:
imgInfo.forceTiling = ImageTilingMode::tiledX;
break;
case I915_FORMAT_MOD_Y_TILED:
imgInfo.forceTiling = ImageTilingMode::tiledY;
break;
case I915_FORMAT_MOD_Yf_TILED:
imgInfo.forceTiling = ImageTilingMode::tiledYf;
break;
case I915_FORMAT_MOD_4_TILED:
imgInfo.forceTiling = ImageTilingMode::tiled4;
break;
default:
printDebugString(debugManager.flags.PrintDebugMessages.get(), stderr,
"Unexpected format in CL-GL sharing");
return nullptr;
}

errorCode.set(CL_SUCCESS);

Expand Down
26 changes: 26 additions & 0 deletions shared/source/gmm_helper/gmm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,32 @@ void Gmm::setupImageResourceParams(ImageInfo &imgInfo, bool preferCompressed) {

resourceParams.Flags.Info.Linear = imgInfo.linearStorage;

switch (imgInfo.forceTiling) {
case ImageTilingMode::tiledW:
resourceParams.Flags.Info.TiledW = true;
break;
case ImageTilingMode::tiledX:
resourceParams.Flags.Info.TiledX = true;
break;
case ImageTilingMode::tiledY:
resourceParams.Flags.Info.TiledY = true;
break;
case ImageTilingMode::tiledYf:
resourceParams.Flags.Info.TiledYf = true;
break;
case ImageTilingMode::tiledYs:
resourceParams.Flags.Info.TiledYs = true;
break;
case ImageTilingMode::tiled4:
resourceParams.Flags.Info.Tile4 = true;
break;
case ImageTilingMode::tiled64:
resourceParams.Flags.Info.Tile64 = true;
break;
default:
break;
}

auto &gfxCoreHelper = gmmHelper->getRootDeviceEnvironment().getHelper<GfxCoreHelper>();
auto &productHelper = gmmHelper->getRootDeviceEnvironment().getHelper<ProductHelper>();
resourceParams.NoGfxMemory = 1; // dont allocate, only query for params
Expand Down
12 changes: 12 additions & 0 deletions shared/source/helpers/surface_format_info.h
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,17 @@ struct ImageDescriptor {
bool fromParent;
};

enum class ImageTilingMode {
tiledAuto = 0,
tiledW,
tiledX,
tiledY,
tiledYf,
tiledYs,
tiled4,
tiled64,
};

struct ImageInfo {
ImageDescriptor imgDesc;
const SurfaceFormatInfo *surfaceFormat;
Expand All @@ -247,6 +258,7 @@ struct ImageInfo {
bool linearStorage;
bool useLocalMemory;
bool isDisplayable;
ImageTilingMode forceTiling;
};

struct ImageImplicitArgs {
Expand Down

0 comments on commit 54bda0e

Please sign in to comment.