Skip to content

Commit

Permalink
Merge pull request #101 from takahiroharada/feature/ORO-0-ogl-test
Browse files Browse the repository at this point in the history
Fix oroCreateTextureObject
  • Loading branch information
takahiroharada authored Oct 15, 2024
2 parents 834d057 + e4c1fc5 commit ecfbfc9
Show file tree
Hide file tree
Showing 46 changed files with 200 additions and 9 deletions.
152 changes: 152 additions & 0 deletions Test/OpenGL/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
#define GLEW_STATIC
#include <contrib/glew/include/glew/glew.h>
#include <contrib/glfw/include/GLFW/glfw3.h>

#include <Orochi/Orochi.h>
#include <iostream>
#include <assert.h>

inline void checkError( const oroError result )
{
if( result != oroSuccess )
{
const char* errorNamePtr = oroGetErrorName( result );
const std::string_view errorName{ ( errorNamePtr != nullptr ) ? errorNamePtr : "NoErrorName" };
std::cerr << "oroError '" << errorName << "': ";
const char* errorDescriptionPtr = nullptr;
oroGetErrorString( result, &errorDescriptionPtr );
const std::string_view errorDescription{ ( errorDescriptionPtr != nullptr ) ? errorDescriptionPtr : "No description." };
std::cerr << errorDescription << '\n' << std::flush;
std::abort();
}
}

inline void checkError( const orortcResult result )
{
if( result != ORORTC_SUCCESS )
{
const std::string_view errorDescription = orortcGetErrorString( result );
std::cerr << "orortcError: " << errorDescription << '\n' << std::flush;
std::abort();
}
}

#define ERROR_CHECK( e ) checkError( e )

int main( int argc, char** argv )
{
GLFWwindow* window;
if( !glfwInit() ) return 0;

glfwWindowHint( GLFW_RED_BITS, 32 );
glfwWindowHint( GLFW_GREEN_BITS, 32 );
glfwWindowHint( GLFW_BLUE_BITS, 32 );

glfwWindowHint( GLFW_RESIZABLE, GL_FALSE );

window = glfwCreateWindow( 1280, 720, "orochiOglInterop", NULL, NULL );
if( !window )
{
glfwTerminate();
return 0;
}
glfwMakeContextCurrent( window );

if( glewInit() != GLEW_OK )
{
glfwTerminate();
return 0;
}

oroDevice m_device = 0;
oroCtx m_ctx = nullptr;
oroStream m_stream = nullptr;

{
const int deviceIndex = 0;
oroApi api = (oroApi)( ORO_API_CUDA | ORO_API_HIP );
int a = oroInitialize( api, 0 );
assert( a == 0 );

ERROR_CHECK( oroInit( 0 ) );
ERROR_CHECK( oroDeviceGet( &m_device, deviceIndex ) );
ERROR_CHECK( oroCtxCreate( &m_ctx, 0, m_device ) );
ERROR_CHECK( oroCtxSetCurrent( m_ctx ) );
ERROR_CHECK( oroStreamCreate( &m_stream ) );
}

{
const uint32_t imageSize = 64u;
GLuint texture = 0;
oroGraphicsResource_t oroResource = nullptr;
oroArray_t interopArray = nullptr;
oroTextureObject_t interopTexture = nullptr;

// Work around for AMD driver crash when calling `oroGLRegister*` functions
const bool isAmd = oroGetCurAPI( 0 ) == ORO_API_HIP;
if( isAmd )
{
uint32_t deviceCount = 16;
int glDevices[16];
ERROR_CHECK( hipGLGetDevices( &deviceCount, glDevices, deviceCount, hipGLDeviceListAll ) );
}

// Create texture object
glGenTextures( 1, &texture );
glBindTexture( GL_TEXTURE_2D, texture );
glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA8, imageSize, imageSize, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0 );
glBindTexture( GL_TEXTURE_2D, 0 );

// Register framebuffer attachment that will be passed to Oro
{
ERROR_CHECK( oroGraphicsGLRegisterImage( &oroResource, texture, GL_TEXTURE_2D, oroGraphicsRegisterFlagsReadOnly ) );
oroDeviceSynchronize();
ERROR_CHECK( oroGetLastError() );
// Map buffer objects to get device pointers
ERROR_CHECK( oroGraphicsMapResources( 1, &oroResource, 0 ) );
oroDeviceSynchronize();
ERROR_CHECK( oroGetLastError() );
ERROR_CHECK( oroGraphicsSubResourceGetMappedArray( &interopArray, oroResource, 0, 0 ) );
oroDeviceSynchronize();
ERROR_CHECK( oroGetLastError() );

// Create texture interop object to be passed to kernel
{
oroChannelFormatDesc desc;
ERROR_CHECK( oroGetChannelDesc( &desc, interopArray ) );
oroDeviceSynchronize();
ERROR_CHECK( oroGetLastError() );

oroResourceDesc texRes;
memset( &texRes, 0, sizeof( oroResourceDesc ) );

texRes.resType = oroResourceTypeArray;
texRes.res.array.array = interopArray;

oroTextureDesc texDescr;
memset( &texDescr, 0, sizeof( oroTextureDesc ) );

texDescr.normalizedCoords = false;
texDescr.filterMode = oroFilterModePoint;
texDescr.addressMode[0] = oroAddressModeWrap;
texDescr.readMode = oroReadModeElementType;

ERROR_CHECK( oroCreateTextureObject( &interopTexture, &texRes, &texDescr, NULL ) );
}
}

ERROR_CHECK( oroGraphicsUnmapResources( 1, &oroResource, 0 ) );
ERROR_CHECK( oroDestroyTextureObject( interopTexture ) );
ERROR_CHECK( oroGraphicsUnregisterResource( oroResource ) );

glDeleteTextures( 1, &texture );
}

ERROR_CHECK( oroStreamDestroy( m_stream ) );
ERROR_CHECK( oroCtxDestroy( m_ctx ) );


std::cout << "Success!\n";
return 0;
}

17 changes: 17 additions & 0 deletions Test/OpenGL/premake5.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
project "OpenGL"
kind "ConsoleApp"

targetdir "../../dist/bin/%{cfg.buildcfg}"
location "../../build/"

if os.istarget("windows") then
links{ "version" }
libdirs{ "../../contrib/glew", "../../contrib/glfw/" }
links{ "glew32s", "glfw3", "opengl32" }
end

includedirs { "../../" }
files { "../../Orochi/**.h", "../../Orochi/**.cpp" }
files { "../../contrib/**.h", "../../contrib/**.cpp" }
files { "../../contrib/**.h", "../../contrib/**.cpp" }
files { "*.cpp" }
27 changes: 23 additions & 4 deletions UnitTest/basicTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,11 @@
#include "basicTests.h"
#include "common.h"

#if defined(_WIN32)
#define GLEW_STATIC
#include "contrib/glew/include/glew/glew.h"
#include "contrib/glfw/include/GLFW/glfw3.h"
#endif

TEST_F( OroTestBase, init )
{
Expand Down Expand Up @@ -986,6 +988,15 @@ TEST_F( OroTestBase, glRegisterBuffer )
GLuint buf = 0;
oroGraphicsResource* oroResource = nullptr;

// Work around for AMD driver crash when calling `oroGLRegister*` functions
const bool isAmd = oroGetCurAPI( 0 ) == ORO_API_HIP;
if( isAmd )
{
uint32_t deviceCount = 16;
int glDevices[16];
OROCHECK( hipGLGetDevices( &deviceCount, glDevices, deviceCount, hipGLDeviceListAll ) );
}

// Create buffer object
glGenBuffers( 1, &buf );
glBindBuffer( GL_ARRAY_BUFFER, buf );
Expand All @@ -1002,7 +1013,7 @@ TEST_F( OroTestBase, glRegisterBuffer )
window = nullptr;
glfwTerminate();
}

#if defined( _WIN32 )
TEST_F( OroTestBase, glRegisterImage )
{
ASSERT_EQ( glfwInit(), GLFW_TRUE );
Expand All @@ -1014,12 +1025,19 @@ TEST_F( OroTestBase, glRegisterImage )
GLuint texture = 0;
oroGraphicsResource* oroResource = nullptr;

// Work around for AMD driver crash when calling `oroGLRegister*` functions
const bool isAmd = oroGetCurAPI( 0 ) == ORO_API_HIP;
if( isAmd )
{
uint32_t deviceCount = 16;
int glDevices[16];
OROCHECK( hipGLGetDevices( &deviceCount, glDevices, deviceCount, hipGLDeviceListAll ) );
}

// Create texture object
glGenTextures( 1, &texture );
glBindTexture( GL_TEXTURE_2D, texture );
glTexImage2D( GL_TEXTURE_2D, 0, GL_RGB, imageSize, imageSize, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0 );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST );
glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA8, imageSize, imageSize, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0 );
glBindTexture( GL_TEXTURE_2D, 0 );

OROCHECK( oroGraphicsGLRegisterImage( &oroResource, texture, GL_TEXTURE_2D, oroGraphicsRegisterFlagsNone ) );
Expand All @@ -1031,4 +1049,5 @@ TEST_F( OroTestBase, glRegisterImage )
window = nullptr;
glfwTerminate();
}
#endif

10 changes: 5 additions & 5 deletions UnitTest/premake5.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ project "Unittest"
location "../build/"

if os.istarget("windows") then
libdirs{ "../contrib/glew", "../contrib/glfw/" }
links{ "glew32s", "glfw3", "opengl32" }
links{ "version" }
end
if os.istarget("linux") then
Expand All @@ -15,12 +17,10 @@ project "Unittest"
files { "*.cpp", "*.h" }
removefiles { "moduleTestFunc.cpp", "moduleTestKernel.cpp" }
files { "../contrib/**.h", "../contrib/**.cpp" }
files { "../UnitTest/contrib/**.h", "../UnitTest/contrib/**.cpp" }
libdirs{ "../UnitTest/contrib/glew", "../UnitTest/contrib/glfw/" }
links{ "glew32s", "glfw3", "opengl32" }
files { "../contrib/**.h", "../contrib/**.cpp" }

files { "../UnitTest/contrib/gtest-1.6.0/gtest-all.cc" }
externalincludedirs{ "../UnitTest/contrib/gtest-1.6.0/" }
files { "../contrib/gtest-1.6.0/gtest-all.cc" }
externalincludedirs{ "../contrib/gtest-1.6.0/" }
defines { "GTEST_HAS_TR1_TUPLE=0" }
if _OPTIONS["kernelcompile"] then
os.execute( "cd ./bitcodes/ && generate_bitcodes.bat" )
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 2 additions & 0 deletions contrib/hipew/include/hipew.h
Original file line number Diff line number Diff line change
Expand Up @@ -931,6 +931,8 @@ struct hipTextureDesc
float mipmapLevelBias;
float minMipmapLevelClamp;
float maxMipmapLevelClamp;
int disableTrilinearOptimization;
int seamlessCubemap;
};
typedef struct hipTextureDesc hipTextureDesc;
struct __hip_surface;
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
1 change: 1 addition & 0 deletions premake5.lua
Original file line number Diff line number Diff line change
Expand Up @@ -122,4 +122,5 @@ workspace "YamatanoOrochi"
include "./Test/VulkanComputeSimple"
include "./Test/RadixSort"
include "./Test/simpleD3D12"
include "./Test/OpenGL"
end

0 comments on commit ecfbfc9

Please sign in to comment.