Expose window during device probing (#462)

This commit is contained in:
Anders Jenbo 2025-07-01 02:23:13 +02:00 committed by GitHub
parent ab48ce60b0
commit e2ff65cf91
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 63 additions and 108 deletions

View File

@ -56,9 +56,17 @@ bool CConfigApp::InitInstance()
return false; return false;
} }
m_device_enumerator = new LegoDeviceEnumerate; m_device_enumerator = new LegoDeviceEnumerate;
if (m_device_enumerator->DoEnumerate()) { SDL_Window* window = SDL_CreateWindow("Test window", 640, 480, SDL_WINDOW_HIDDEN | SDL_WINDOW_OPENGL);
HWND hWnd;
#ifdef MINIWIN
hWnd = reinterpret_cast<HWND>(window);
#else
hWnd = (HWND) SDL_GetPointerProperty(SDL_GetWindowProperties(window), SDL_PROP_WINDOW_WIN32_HWND_POINTER, NULL);
#endif
if (m_device_enumerator->DoEnumerate(hWnd)) {
return FALSE; return FALSE;
} }
SDL_DestroyWindow(window);
m_driver = NULL; m_driver = NULL;
m_device = NULL; m_device = NULL;
m_full_screen = TRUE; m_full_screen = TRUE;

View File

@ -108,7 +108,7 @@ MxResult LegoVideoManager::Create(MxVideoParam& p_videoParam, MxU32 p_frequencyM
goto done; goto done;
} }
if (deviceEnumerate.DoEnumerate() != SUCCESS) { if (deviceEnumerate.DoEnumerate(hwnd) != SUCCESS) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "LegoDeviceEnumerate::DoEnumerate failed"); SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "LegoDeviceEnumerate::DoEnumerate failed");
goto done; goto done;
} }

View File

@ -216,27 +216,33 @@ BOOL MxDeviceEnumerate::EnumDirectDrawCallback(LPGUID p_guid, LPSTR p_driverDesc
BuildErrorString("DirectDraw Create failed: %s\n", EnumerateErrorToString(result)); BuildErrorString("DirectDraw Create failed: %s\n", EnumerateErrorToString(result));
} }
else { else {
newDevice.m_ddCaps.dwSize = sizeof(newDevice.m_ddCaps); result = lpDD->SetCooperativeLevel(m_hWnd, DDSCL_NORMAL);
result = lpDD->GetCaps(&newDevice.m_ddCaps, NULL);
if (result != DD_OK) { if (result != DD_OK) {
BuildErrorString("GetCaps failed: %s\n", EnumerateErrorToString(result)); BuildErrorString("SetCooperativeLevel failed: %s\n", EnumerateErrorToString(result));
} }
else { else {
result = lpDD->QueryInterface(IID_IDirect3D2, (LPVOID*) &lpDirect3d2); newDevice.m_ddCaps.dwSize = sizeof(newDevice.m_ddCaps);
result = lpDD->GetCaps(&newDevice.m_ddCaps, NULL);
if (result != DD_OK) { if (result != DD_OK) {
BuildErrorString("D3D creation failed: %s\n", EnumerateErrorToString(result)); BuildErrorString("GetCaps failed: %s\n", EnumerateErrorToString(result));
} }
else { else {
result = lpDirect3d2->EnumDevices(DevicesEnumerateCallback, this); result = lpDD->QueryInterface(IID_IDirect3D2, (LPVOID*) &lpDirect3d2);
if (result != DD_OK) { if (result != DD_OK) {
BuildErrorString("D3D enum devices failed: %s\n", EnumerateErrorToString(result)); BuildErrorString("D3D creation failed: %s\n", EnumerateErrorToString(result));
} }
else { else {
if (!newDevice.m_devices.size()) { result = lpDirect3d2->EnumDevices(DevicesEnumerateCallback, this);
m_list.pop_back();
if (result != DD_OK) {
BuildErrorString("D3D enum devices failed: %s\n", EnumerateErrorToString(result));
}
else {
if (!newDevice.m_devices.size()) {
m_list.pop_back();
}
} }
} }
} }
@ -306,12 +312,14 @@ HRESULT MxDeviceEnumerate::EnumDevicesCallback(
// FUNCTION: CONFIG 0x00401dc0 // FUNCTION: CONFIG 0x00401dc0
// FUNCTION: LEGO1 0x1009c6c0 // FUNCTION: LEGO1 0x1009c6c0
// FUNCTION: BETA10 0x1011e3fa // FUNCTION: BETA10 0x1011e3fa
int MxDeviceEnumerate::DoEnumerate() int MxDeviceEnumerate::DoEnumerate(HWND hWnd)
{ {
if (IsInitialized()) { if (IsInitialized()) {
return -1; return -1;
} }
m_hWnd = hWnd;
HRESULT ret = DirectDrawEnumerate(DirectDrawEnumerateCallback, this); HRESULT ret = DirectDrawEnumerate(DirectDrawEnumerateCallback, this);
if (ret != DD_OK) { if (ret != DD_OK) {
BuildErrorString("DirectDrawEnumerate returned error %s\n", EnumerateErrorToString(ret)); BuildErrorString("DirectDrawEnumerate returned error %s\n", EnumerateErrorToString(ret));

View File

@ -194,7 +194,7 @@ class MxDeviceEnumerate {
MxDeviceEnumerate(); MxDeviceEnumerate();
~MxDeviceEnumerate(); ~MxDeviceEnumerate();
virtual int DoEnumerate(); // vtable+0x00 virtual int DoEnumerate(HWND hWnd); // vtable+0x00
BOOL EnumDirectDrawCallback(LPGUID p_guid, LPSTR p_driverDesc, LPSTR p_driverName); BOOL EnumDirectDrawCallback(LPGUID p_guid, LPSTR p_driverDesc, LPSTR p_driverName);
HRESULT EnumDevicesCallback( HRESULT EnumDevicesCallback(
@ -242,6 +242,7 @@ class MxDeviceEnumerate {
protected: protected:
list<MxDriver> m_list; // 0x04 list<MxDriver> m_list; // 0x04
unsigned char m_initialized; // 0x10 unsigned char m_initialized; // 0x10
HWND m_hWnd;
}; };
// TEMPLATE: BETA10 0x1011c1b0 // TEMPLATE: BETA10 0x1011c1b0

View File

@ -28,40 +28,25 @@ Direct3DRMRenderer* OpenGL1Renderer::Create(DWORD width, DWORD height)
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 1); SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 1);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 1); SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 1);
SDL_Window* window = DDWindow; if (!DDWindow) {
bool testWindow = false; SDL_Log("No window handler");
if (!window) {
window = SDL_CreateWindow("OpenGL 1.1 test", width, height, SDL_WINDOW_HIDDEN | SDL_WINDOW_OPENGL);
if (!window) {
SDL_Log("SDL_CreateWindow: %s", SDL_GetError());
return nullptr;
}
testWindow = true;
}
SDL_GLContext context = SDL_GL_CreateContext(window);
if (!context) {
SDL_Log("SDL_GL_CreateContext: %s", SDL_GetError());
if (testWindow) {
SDL_DestroyWindow(window);
}
return nullptr; return nullptr;
} }
if (!SDL_GL_MakeCurrent(window, context)) { SDL_GLContext context = SDL_GL_CreateContext(DDWindow);
if (!context) {
SDL_Log("SDL_GL_CreateContext: %s", SDL_GetError());
return nullptr;
}
if (!SDL_GL_MakeCurrent(DDWindow, context)) {
SDL_GL_DestroyContext(context);
SDL_Log("SDL_GL_MakeCurrent: %s", SDL_GetError()); SDL_Log("SDL_GL_MakeCurrent: %s", SDL_GetError());
if (testWindow) {
SDL_DestroyWindow(window);
}
return nullptr; return nullptr;
} }
GL11_InitState(); GL11_InitState();
if (testWindow) {
SDL_DestroyWindow(window);
}
return new OpenGL1Renderer(width, height, context); return new OpenGL1Renderer(width, height, context);
} }
@ -78,6 +63,7 @@ OpenGL1Renderer::OpenGL1Renderer(DWORD width, DWORD height, SDL_GLContext contex
OpenGL1Renderer::~OpenGL1Renderer() OpenGL1Renderer::~OpenGL1Renderer()
{ {
SDL_DestroySurface(m_renderedImage); SDL_DestroySurface(m_renderedImage);
SDL_GL_DestroyContext(m_context);
} }
void OpenGL1Renderer::PushLights(const SceneLight* lightsArray, size_t count) void OpenGL1Renderer::PushLights(const SceneLight* lightsArray, size_t count)

View File

@ -40,25 +40,18 @@ Direct3DRMRenderer* OpenGLES2Renderer::Create(DWORD width, DWORD height)
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2); SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0); SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0);
SDL_Window* window = DDWindow; if (!DDWindow) {
bool testWindow = false; SDL_Log("No window handler");
if (!window) {
window = SDL_CreateWindow("OpenGL ES 2.0 test", width, height, SDL_WINDOW_HIDDEN | SDL_WINDOW_OPENGL);
testWindow = true;
}
SDL_GLContext context = SDL_GL_CreateContext(window);
if (!context) {
if (testWindow) {
SDL_DestroyWindow(window);
}
return nullptr; return nullptr;
} }
if (!SDL_GL_MakeCurrent(window, context)) { SDL_GLContext context = SDL_GL_CreateContext(DDWindow);
if (testWindow) { if (!context) {
SDL_DestroyWindow(window); return nullptr;
} }
if (!SDL_GL_MakeCurrent(DDWindow, context)) {
SDL_GL_DestroyContext(context);
return nullptr; return nullptr;
} }
@ -174,10 +167,6 @@ Direct3DRMRenderer* OpenGLES2Renderer::Create(DWORD width, DWORD height)
glDeleteShader(vs); glDeleteShader(vs);
glDeleteShader(fs); glDeleteShader(fs);
if (testWindow) {
SDL_DestroyWindow(window);
}
return new OpenGLES2Renderer(width, height, context, shaderProgram); return new OpenGLES2Renderer(width, height, context, shaderProgram);
} }
@ -285,6 +274,7 @@ OpenGLES2Renderer::OpenGLES2Renderer(DWORD width, DWORD height, SDL_GLContext co
OpenGLES2Renderer::~OpenGLES2Renderer() OpenGLES2Renderer::~OpenGLES2Renderer()
{ {
SDL_DestroySurface(m_renderedImage); SDL_DestroySurface(m_renderedImage);
SDL_GL_DestroyContext(m_context);
glDeleteProgram(m_shaderProgram); glDeleteProgram(m_shaderProgram);
} }

View File

@ -203,49 +203,31 @@ Direct3DRMRenderer* Direct3DRMSDL3GPURenderer::Create(DWORD width, DWORD height)
return nullptr; return nullptr;
} }
SDL_Window* window = DDWindow; if (!DDWindow) {
bool testWindow = false; SDL_Log("No window handler");
if (!window) {
window = SDL_CreateWindow("SDL_GPU test", width, height, SDL_WINDOW_HIDDEN);
if (!window) {
SDL_Log("SDL_CreateWindow: %s", SDL_GetError());
return nullptr;
}
testWindow = true;
}
if (!SDL_ClaimWindowForGPUDevice(device.ptr, window)) {
SDL_LogError(LOG_CATEGORY_MINIWIN, "SDL_ClaimWindowForGPUDevice: %s", SDL_GetError());
if (testWindow) {
SDL_DestroyWindow(window);
}
return nullptr; return nullptr;
} }
ScopedPipeline opaquePipeline{device.ptr, InitializeGraphicsPipeline(device.ptr, window, true, true)}; if (!SDL_ClaimWindowForGPUDevice(device.ptr, DDWindow)) {
SDL_LogError(LOG_CATEGORY_MINIWIN, "SDL_ClaimWindowForGPUDevice: %s", SDL_GetError());
return nullptr;
}
ScopedPipeline opaquePipeline{device.ptr, InitializeGraphicsPipeline(device.ptr, DDWindow, true, true)};
if (!opaquePipeline.ptr) { if (!opaquePipeline.ptr) {
SDL_LogError(LOG_CATEGORY_MINIWIN, "InitializeGraphicsPipeline for opaquePipeline"); SDL_LogError(LOG_CATEGORY_MINIWIN, "InitializeGraphicsPipeline for opaquePipeline");
if (testWindow) {
SDL_DestroyWindow(window);
}
return nullptr; return nullptr;
} }
ScopedPipeline transparentPipeline{device.ptr, InitializeGraphicsPipeline(device.ptr, window, true, false)}; ScopedPipeline transparentPipeline{device.ptr, InitializeGraphicsPipeline(device.ptr, DDWindow, true, false)};
if (!transparentPipeline.ptr) { if (!transparentPipeline.ptr) {
SDL_LogError(LOG_CATEGORY_MINIWIN, "InitializeGraphicsPipeline for transparentPipeline"); SDL_LogError(LOG_CATEGORY_MINIWIN, "InitializeGraphicsPipeline for transparentPipeline");
if (testWindow) {
SDL_DestroyWindow(window);
}
return nullptr; return nullptr;
} }
ScopedPipeline uiPipeline{device.ptr, InitializeGraphicsPipeline(device.ptr, window, false, false)}; ScopedPipeline uiPipeline{device.ptr, InitializeGraphicsPipeline(device.ptr, DDWindow, false, false)};
if (!uiPipeline.ptr) { if (!uiPipeline.ptr) {
SDL_LogError(LOG_CATEGORY_MINIWIN, "InitializeGraphicsPipeline for uiPipeline"); SDL_LogError(LOG_CATEGORY_MINIWIN, "InitializeGraphicsPipeline for uiPipeline");
if (testWindow) {
SDL_DestroyWindow(window);
}
return nullptr; return nullptr;
} }
@ -257,9 +239,6 @@ Direct3DRMRenderer* Direct3DRMSDL3GPURenderer::Create(DWORD width, DWORD height)
ScopedTransferBuffer uploadBuffer{device.ptr, SDL_CreateGPUTransferBuffer(device.ptr, &uploadBufferInfo)}; ScopedTransferBuffer uploadBuffer{device.ptr, SDL_CreateGPUTransferBuffer(device.ptr, &uploadBufferInfo)};
if (!uploadBuffer.ptr) { if (!uploadBuffer.ptr) {
SDL_LogError(LOG_CATEGORY_MINIWIN, "SDL_CreateGPUTransferBuffer filed for upload buffer (%s)", SDL_GetError()); SDL_LogError(LOG_CATEGORY_MINIWIN, "SDL_CreateGPUTransferBuffer filed for upload buffer (%s)", SDL_GetError());
if (testWindow) {
SDL_DestroyWindow(window);
}
return nullptr; return nullptr;
} }
@ -273,9 +252,6 @@ Direct3DRMRenderer* Direct3DRMSDL3GPURenderer::Create(DWORD width, DWORD height)
ScopedSampler sampler{device.ptr, SDL_CreateGPUSampler(device.ptr, &samplerInfo)}; ScopedSampler sampler{device.ptr, SDL_CreateGPUSampler(device.ptr, &samplerInfo)};
if (!sampler.ptr) { if (!sampler.ptr) {
SDL_LogError(LOG_CATEGORY_MINIWIN, "Failed to create sampler: %s", SDL_GetError()); SDL_LogError(LOG_CATEGORY_MINIWIN, "Failed to create sampler: %s", SDL_GetError());
if (testWindow) {
SDL_DestroyWindow(window);
}
return nullptr; return nullptr;
} }
@ -289,17 +265,9 @@ Direct3DRMRenderer* Direct3DRMSDL3GPURenderer::Create(DWORD width, DWORD height)
ScopedSampler uiSampler{device.ptr, SDL_CreateGPUSampler(device.ptr, &uiSamplerInfo)}; ScopedSampler uiSampler{device.ptr, SDL_CreateGPUSampler(device.ptr, &uiSamplerInfo)};
if (!uiSampler.ptr) { if (!uiSampler.ptr) {
SDL_LogError(LOG_CATEGORY_MINIWIN, "Failed to create sampler: %s", SDL_GetError()); SDL_LogError(LOG_CATEGORY_MINIWIN, "Failed to create sampler: %s", SDL_GetError());
if (testWindow) {
SDL_DestroyWindow(window);
}
return nullptr; return nullptr;
} }
if (testWindow) {
SDL_ReleaseWindowFromGPUDevice(device.ptr, window);
SDL_DestroyWindow(window);
}
auto renderer = new Direct3DRMSDL3GPURenderer( auto renderer = new Direct3DRMSDL3GPURenderer(
width, width,
height, height,
@ -383,9 +351,7 @@ Direct3DRMSDL3GPURenderer::~Direct3DRMSDL3GPURenderer()
{ {
SDL_ReleaseGPUBuffer(m_device, m_uiMeshCache.vertexBuffer); SDL_ReleaseGPUBuffer(m_device, m_uiMeshCache.vertexBuffer);
SDL_ReleaseGPUBuffer(m_device, m_uiMeshCache.indexBuffer); SDL_ReleaseGPUBuffer(m_device, m_uiMeshCache.indexBuffer);
if (DDWindow) { SDL_ReleaseWindowFromGPUDevice(m_device, DDWindow);
SDL_ReleaseWindowFromGPUDevice(m_device, DDWindow);
}
if (m_downloadBuffer) { if (m_downloadBuffer) {
SDL_ReleaseGPUTransferBuffer(m_device, m_downloadBuffer); SDL_ReleaseGPUTransferBuffer(m_device, m_downloadBuffer);
} }
@ -839,10 +805,6 @@ void Direct3DRMSDL3GPURenderer::Resize(int width, int height, const ViewportTran
m_height = height; m_height = height;
m_viewportTransform = viewportTransform; m_viewportTransform = viewportTransform;
if (!DDWindow) {
return;
}
if (m_transferTexture) { if (m_transferTexture) {
SDL_ReleaseGPUTexture(m_device, m_transferTexture); SDL_ReleaseGPUTexture(m_device, m_transferTexture);
} }