Previous 199869 Revisions Next

r34789 Sunday 1st February, 2015 at 13:02:38 UTC by Miodrag Milanović
Added BGFX skeleton to SDL build as well (nw)
[src/osd/sdl]drawbgfx.c* osdsdl.h sdl.mak sdlmain.c video.c video.h window.c window.h
[src/osd/windows]drawbgfx.c

trunk/src/osd/sdl/drawbgfx.c
r0r243301
1// license:BSD-3-Clause
2// copyright-holders:Miodrag Milanovic
3//============================================================
4//
5//  drawbgfx.c - BGFX drawer
6//
7//============================================================
8
9#ifdef SDLMAME_WIN32
10// standard windows headers
11#define __STDC_LIMIT_MACROS
12#define __STDC_FORMAT_MACROS
13#define __STDC_CONSTANT_MACROS
14#define WIN32_LEAN_AND_MEAN
15#include <windows.h>
16#endif
17
18// standard C headers
19#include <math.h>
20#include <stdio.h>
21
22// MAME headers
23#include "osdcomm.h"
24#include "emu.h"
25#include "options.h"
26#include "emuopts.h"
27
28// standard SDL headers
29#include "sdlinc.h"
30#include "modules/lib/osdlib.h"
31
32// OSD headers
33#include "osdsdl.h"
34#include "window.h"
35
36#include <bgfxplatform.h>
37#include <bgfx.h>
38
39//============================================================
40//  DEBUGGING
41//============================================================
42
43//============================================================
44//  CONSTANTS
45//============================================================
46
47//============================================================
48//  MACROS
49//============================================================
50
51//============================================================
52//  TYPES
53//============================================================
54
55//============================================================
56//  INLINES
57//============================================================
58
59
60//============================================================
61//  PROTOTYPES
62//============================================================
63
64// core functions
65
66static void drawbgfx_exit(void);
67static void drawbgfx_attach(sdl_draw_info *info, sdl_window_info *window);
68static int drawbgfx_window_create(sdl_window_info *window, int width, int height);
69static void drawbgfx_window_resize(sdl_window_info *window, int width, int height);
70static void drawbgfx_window_destroy(sdl_window_info *window);
71static int drawbgfx_window_draw(sdl_window_info *window, UINT32 dc, int update);
72static void drawbgfx_set_target_bounds(sdl_window_info *window);
73static void drawbgfx_destroy_all_textures(sdl_window_info *window);
74static void drawbgfx_window_clear(sdl_window_info *window);
75static int drawbgfx_xy_to_render_target(sdl_window_info *window, int x, int y, int *xt, int *yt);
76
77//============================================================
78//  Textures
79//============================================================
80
81/* sdl_info is the information about SDL for the current screen */
82struct sdl_info13
83{
84    sdl_info13()
85    : m_blittimer(0), m_renderer(NULL),
86      m_hofs(0), m_vofs(0),
87      m_resize_pending(0), m_resize_width(0), m_resize_height(0),
88      m_last_blit_time(0), m_last_blit_pixels(0)
89    {}
90
91   // void render_quad(texture_info *texture, const render_primitive *prim, const int x, const int y);
92
93    //texture_info *texture_find(const render_primitive &prim, const quad_setup_data &setup);
94    //texture_info *texture_update(const render_primitive &prim);
95
96   INT32           m_blittimer;
97
98   SDL_Renderer *  m_renderer;
99   //simple_list<texture_info>  m_texlist;                // list of active textures
100
101   float           m_hofs;
102   float           m_vofs;
103
104   // resize information
105
106   UINT8           m_resize_pending;
107   UINT32          m_resize_width;
108   UINT32          m_resize_height;
109
110   // Stats
111   INT64           m_last_blit_time;
112   INT64           m_last_blit_pixels;
113
114   // Original display_mode
115   SDL_DisplayMode m_original_mode;
116};
117
118//============================================================
119//  Static Variables
120//============================================================
121
122//============================================================
123//  drawbgfx_init
124//============================================================
125
126int drawbgfx_init(running_machine &machine, sdl_draw_info *callbacks)
127{
128   // fill in the callbacks
129   callbacks->exit = drawbgfx_exit;
130   callbacks->attach = drawbgfx_attach;
131     
132   return 0;
133}
134
135
136//============================================================
137//  drawbgfx_attach
138//============================================================
139
140static void drawbgfx_attach(sdl_draw_info *info, sdl_window_info *window)
141{
142   // fill in the callbacks
143   window->create = drawbgfx_window_create;
144   window->resize = drawbgfx_window_resize;
145   window->set_target_bounds = drawbgfx_set_target_bounds;
146   window->draw = drawbgfx_window_draw;
147   window->destroy = drawbgfx_window_destroy;
148   window->destroy_all_textures = drawbgfx_destroy_all_textures;
149   window->clear = drawbgfx_window_clear;
150   window->xy_to_render_target = drawbgfx_xy_to_render_target;
151}
152
153//============================================================
154//  drawbgfx_window_create
155//============================================================
156
157static int drawbgfx_window_create(sdl_window_info *window, int width, int height)
158{
159   // allocate memory for our structures
160   sdl_info13 *sdl = global_alloc(sdl_info13);
161
162   /* FIXME: On Ubuntu and potentially other Linux OS you should use
163    * to disable panning. This has to be done before every invocation of mame.
164    *
165    * xrandr --output HDMI-0 --panning 0x0+0+0 --fb 0x0
166    *
167    */
168
169   osd_printf_verbose("Enter drawsdl2_window_create\n");
170
171   window->dxdata = sdl;
172
173   UINT32 extra_flags = (window->fullscreen() ?
174         SDL_WINDOW_BORDERLESS | SDL_WINDOW_INPUT_FOCUS | SDL_WINDOW_FULLSCREEN : SDL_WINDOW_RESIZABLE);
175
176#if defined(SDLMAME_WIN32)
177   SDL_SetHint(SDL_HINT_VIDEO_MINIMIZE_ON_FOCUS_LOSS, "0");
178#endif
179   // create the SDL window
180   window->sdl_window = SDL_CreateWindow(window->title,
181         window->monitor()->position_size().x, window->monitor()->position_size().y,
182         width, height, extra_flags);
183
184   if (window->fullscreen() && video_config.switchres)
185   {
186      SDL_DisplayMode mode;
187      //SDL_GetCurrentDisplayMode(window->monitor()->handle, &mode);
188      SDL_GetWindowDisplayMode(window->sdl_window, &mode);
189      sdl->m_original_mode = mode;
190      mode.w = width;
191      mode.h = height;
192      if (window->refresh)
193         mode.refresh_rate = window->refresh;
194
195      SDL_SetWindowDisplayMode(window->sdl_window, &mode);    // Try to set mode
196#ifndef SDLMAME_WIN32
197      /* FIXME: Warp the mouse to 0,0 in case a virtual desktop resolution
198       * is in place after the mode switch - which will most likely be the case
199       * This is a hack to work around a deficiency in SDL2
200       */
201      SDL_WarpMouseInWindow(window->sdl_window, 1, 1);
202#endif
203   }
204   else
205   {
206      //SDL_SetWindowDisplayMode(window->sdl_window, NULL); // Use desktop
207   }
208   // create renderer
209
210   if (video_config.waitvsync)
211      sdl->m_renderer = SDL_CreateRenderer(window->sdl_window, -1, SDL_RENDERER_PRESENTVSYNC | SDL_RENDERER_ACCELERATED);
212   else
213      sdl->m_renderer = SDL_CreateRenderer(window->sdl_window, -1, SDL_RENDERER_ACCELERATED);
214
215   if (!sdl->m_renderer)
216   {
217      fatalerror("Error on creating renderer: %s\n", SDL_GetError());
218   }
219
220   //SDL_SelectRenderer(window->sdl_window);
221   SDL_ShowWindow(window->sdl_window);
222   //SDL_SetWindowFullscreen(window->window_id, window->fullscreen);
223   SDL_RaiseWindow(window->sdl_window);
224
225   SDL_GetWindowSize(window->sdl_window, &window->width, &window->height);
226
227   sdl->m_blittimer = 3;
228
229   SDL_RenderPresent(sdl->m_renderer);
230   
231   bgfx::sdlSetWindow(window->sdl_window);
232   bgfx::init();
233   bgfx::reset(window->width, window->height, BGFX_RESET_VSYNC);
234   
235   // Enable debug text.
236   bgfx::setDebug(BGFX_DEBUG_STATS);// BGFX_DEBUG_TEXT);
237   osd_printf_verbose("Leave drawsdl2_window_create\n");
238   return 0;
239}
240
241//============================================================
242//  drawbgfx_window_resize
243//============================================================
244
245static void drawbgfx_window_resize(sdl_window_info *window, int width, int height)
246{
247   sdl_info13 *sdl = (sdl_info13 *) window->dxdata;
248
249   sdl->m_resize_pending = 1;
250   sdl->m_resize_height = height;
251   sdl->m_resize_width = width;
252
253   window->width = width;
254   window->height = height;
255
256   sdl->m_blittimer = 3;
257}
258
259//============================================================
260//  drawsdl_xy_to_render_target
261//============================================================
262
263static int drawbgfx_xy_to_render_target(sdl_window_info *window, int x, int y, int *xt, int *yt)
264{
265   sdl_info13 *sdl = (sdl_info13 *) window->dxdata;
266
267   *xt = x - sdl->m_hofs;
268   *yt = y - sdl->m_vofs;
269   if (*xt<0 || *xt >= window->blitwidth)
270      return 0;
271   if (*yt<0 || *yt >= window->blitheight)
272      return 0;
273   return 1;
274}
275
276//============================================================
277//  drawbgfx_window_get_primitives
278//============================================================
279
280static void drawbgfx_set_target_bounds(sdl_window_info *window)
281{
282   window->target->set_bounds(window->blitwidth, window->blitheight, window->monitor()->aspect());
283}
284
285//============================================================
286//  drawbgfx_window_draw
287//============================================================
288
289static int drawbgfx_window_draw(sdl_window_info *window, UINT32 dc, int update)
290{
291   bgfx::setViewClear(0
292      , BGFX_CLEAR_COLOR|BGFX_CLEAR_DEPTH
293      , 0x000000ff
294      , 1.0f
295      , 0
296      );
297   // Set view 0 default viewport.
298   bgfx::setViewRect(0, 0, 0, window->blitwidth, window->blitheight);
299
300   // This dummy draw call is here to make sure that view 0 is cleared
301   // if no other draw calls are submitted to view 0.
302   bgfx::submit(0);
303
304   window->primlist->acquire_lock();
305   window->primlist->release_lock();   
306   // Advance to next frame. Rendering thread will be kicked to
307   // process submitted rendering primitives.
308   bgfx::frame();
309   
310   return 0;
311}
312
313//============================================================
314//  drawbgfx_exit
315//============================================================
316
317static void drawbgfx_exit(void)
318{
319}
320
321//============================================================
322//  drawbgfx_window_destroy
323//============================================================
324
325static void drawbgfx_window_destroy(sdl_window_info *window)
326{
327   sdl_info13 *sdl = (sdl_info13 *) window->dxdata;
328
329   // skip if nothing
330   if (sdl == NULL)
331      return;
332
333   // free the memory in the window
334
335   drawbgfx_destroy_all_textures(window);
336
337   if (window->fullscreen() && video_config.switchres)
338   {
339      SDL_SetWindowFullscreen(window->sdl_window, 0);    // Try to set mode
340      SDL_SetWindowDisplayMode(window->sdl_window, &sdl->m_original_mode);    // Try to set mode
341      SDL_SetWindowFullscreen(window->sdl_window, SDL_WINDOW_FULLSCREEN);    // Try to set mode
342   }
343
344   SDL_DestroyWindow(window->sdl_window);
345
346   global_free(sdl);
347   window->dxdata = NULL;
348   
349   // Shutdown bgfx.
350   bgfx::shutdown();
351}
352
353static void drawbgfx_destroy_all_textures(sdl_window_info *window)
354{
355}
356
357//============================================================
358//  TEXCOPY FUNCS
359//============================================================
360
361static void drawbgfx_window_clear(sdl_window_info *window)
362{
363   sdl_info13 *sdl = (sdl_info13 *) window->dxdata;
364
365   sdl->m_blittimer = 2;
366}
trunk/src/osd/sdl/osdsdl.h
r243300r243301
8787#define SDLOPTVAL_OPENGL                "opengl"
8888#define SDLOPTVAL_SOFT                  "soft"
8989#define SDLOPTVAL_SDL2ACCEL             "accel"
90#define SDLOPTVAL_BGFX                  "bgfx"
9091
9192#define SDLMAME_LED(x)                  "led" #x
9293
trunk/src/osd/sdl/sdl.mak
r243300r243301
776776endif
777777
778778#-------------------------------------------------
779# BGFX
780#-------------------------------------------------
781
782OSDOBJS += $(SDLOBJ)/drawbgfx.o
783INCPATH += -I$(3RDPARTY)/bgfx/include -I$(3RDPARTY)/bx/include
784
785#-------------------------------------------------
779786# X11
780787#-------------------------------------------------
781788
trunk/src/osd/sdl/sdlmain.c
r243300r243301
520520{
521521   video_options_add("soft", NULL);
522522   video_options_add("opengl", NULL);
523   video_options_add("bgfx", NULL);
523524   //video_options_add("auto", NULL); // making d3d video default one
524525}
525526
trunk/src/osd/sdl/video.c
r243300r243301
636636   {
637637      video_config.mode = VIDEO_MODE_SDL2ACCEL;
638638   }
639   else if (strcmp(stemp, SDLOPTVAL_BGFX) == 0)
640   {
641      video_config.mode = VIDEO_MODE_BGFX;
642   }
639643   else
640644   {
641645      osd_printf_warning("Invalid video value %s; reverting to software\n", stemp);
trunk/src/osd/sdl/video.h
r243300r243301
2323enum {
2424   VIDEO_MODE_SOFT = 0,
2525   VIDEO_MODE_OPENGL,
26   VIDEO_MODE_SDL2ACCEL
26   VIDEO_MODE_SDL2ACCEL,
27   VIDEO_MODE_BGFX
2728};
2829
2930#define VIDEO_SCALE_MODE_NONE       (0)
trunk/src/osd/sdl/window.c
r243300r243301
259259         video_config.mode = VIDEO_MODE_SOFT;
260260   }
261261#endif
262   if (video_config.mode == VIDEO_MODE_BGFX)
263   {
264      if (drawbgfx_init(machine(), &draw))
265         video_config.mode = VIDEO_MODE_SOFT;
266   }
262267   if (video_config.mode == VIDEO_MODE_SOFT)
263268   {
264269      if (drawsdl_init(&draw))
trunk/src/osd/sdl/window.h
r243300r243301
194194
195195int drawsdl2_init(running_machine &machine, sdl_draw_info *callbacks);
196196
197//============================================================
198// PROTOTYPES - drawbgfx.c
199//============================================================
200
201int drawbgfx_init(running_machine &machine, sdl_draw_info *callbacks);
202
197203#endif /* __SDLWINDOW__ */
trunk/src/osd/windows/drawbgfx.c
r243300r243301
134134   // if no other draw calls are submitted to view 0.
135135   bgfx::submit(0);
136136
137   // Use debug font to print information about this example.
138   //bgfx::dbgTextClear();
139   //bgfx::dbgTextPrintf(0, 1, 0x4f, "MAME BGFX test");
140   //bgfx::dbgTextPrintf(0, 2, 0x6f, "Description: Initialization and debug text.");
141
142137   window->m_primlist->acquire_lock();
143138
144139   // now draw


Previous 199869 Revisions Next


© 1997-2024 The MAME Team