Bonjour,

je suis en train de porter de linux a windows une bibliotheque nommee evas, qui est plus precisement un canvas (canevas en francais). Celle-ci a plusieurs moteurs disponible, en fonction de l'api graphique utilisee (xlib, xcb, opengl, directdraw direct3d, sdl, etc...).

J'ai, comme indique ci-dessus, deja ecris un moteur pour direct3d pour evas, mais il est , en erme de vitesse, comparable a directdraw et tres lent compare a opengl (j'utilise une surface et une texture pour chaque objet du canevas).

J'ai decide d'essayer d'utiliser les vertex buffers. Je ne suis pas tres cale en programmation windows, encore moins en ce qui concerne direct3d.

J'ai commence par ecrire un programme de test qui utilise les vertex buffers:

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
//g++ -g -Wall -o d3d d3d.cpp -ld3d9
 
#include <cstdlib>
#include <cstdio>
 
#include <d3d9.h>
 
#define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZRHW | D3DFVF_DIFFUSE)
 
struct D3DVERTEX
{
  float x;
  float y;
  float z;
  float rhw;
  DWORD color;
};
 
static int res = 1;
 
int
d3d_init (HWND window,
	  LPDIRECT3D9 *o,
	  LPDIRECT3DDEVICE9 *d)
{
  D3DPRESENT_PARAMETERS pp;
  D3DDISPLAYMODE        dm;
  D3DCAPS9              caps;
  RECT                  rect;
  LPDIRECT3D9           object;
  LPDIRECT3DDEVICE9     device;
  DWORD                 flag;
 
  object = Direct3DCreate9 (D3D_SDK_VERSION);
  if (!object)
    return 0;
 
   if (FAILED (object->GetAdapterDisplayMode (D3DADAPTER_DEFAULT,
					      &dm)))
     goto no_get_adapter;
 
   ZeroMemory (&caps, sizeof(caps));
   if (FAILED (object->GetDeviceCaps (D3DADAPTER_DEFAULT,
				      D3DDEVTYPE_HAL,
				      &caps)))
     goto no_caps;
 
   flag = (caps.VertexProcessingCaps != 0)
     ? D3DCREATE_HARDWARE_VERTEXPROCESSING
     : D3DCREATE_SOFTWARE_VERTEXPROCESSING;
 
   if (!GetClientRect(window, &rect))
     goto no_get_client;
 
   ZeroMemory(&pp, sizeof(pp));
   pp.BackBufferWidth = rect.right - rect.left;
   pp.BackBufferHeight = rect.bottom - rect.top;
   pp.BackBufferFormat = D3DFMT_UNKNOWN;
   pp.BackBufferCount = 1;
   pp.SwapEffect = D3DSWAPEFFECT_FLIP;
   pp.hDeviceWindow = window;
   pp.Windowed  = TRUE;
   pp.EnableAutoDepthStencil = FALSE;
   pp.FullScreen_RefreshRateInHz = 0;
   pp.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE;
 
   if (FAILED(object->CreateDevice (D3DADAPTER_DEFAULT,
				    D3DDEVTYPE_HAL,
				    window,
				    flag,
				    &pp,
				    &device)))
     goto no_device;
 
   if (FAILED (device->SetVertexShader (NULL)))
     goto canot_set_device;
   if (FAILED (device->SetFVF (D3DFVF_CUSTOMVERTEX)))
     goto canot_set_device;
 
   if (FAILED (device->SetRenderState (D3DRS_AMBIENT, RGB (255, 255, 255))))
     goto canot_set_device;
   if (FAILED (device->SetRenderState (D3DRS_LIGHTING, FALSE)))
     goto canot_set_device;
   if (FAILED (device->SetRenderState (D3DRS_CULLMODE, D3DCULL_NONE)))
     goto canot_set_device;
   if (FAILED (device->SetRenderState (D3DRS_ZENABLE, D3DZB_FALSE)))
     goto canot_set_device;
 
   *o = object;
   *d = device;
 
   return 1;
 
 canot_set_device:
   printf ("canot_set_device\n");
   device->Release ();
 no_device:
   printf ("no_device\n");
 no_get_client:
   printf ("no_get_client\n");
 no_caps:
   printf ("no_caps\n");
 no_get_adapter:
   printf ("no_get_adapter\n");
   object->Release ();
 
   return 0;
}
 
void
d3d_shutdown (LPDIRECT3D9       o,
	      LPDIRECT3DDEVICE9 d)
{
  d->Release ();
  o->Release ();
}
 
LRESULT CALLBACK
window_procedure(HWND hWindow,
		 UINT uMessage,
		 WPARAM wparam,
		 LPARAM lparam)
{
  switch (uMessage) {
  case WM_KEYDOWN:
    {
      switch (wparam) {
      case VK_ESCAPE:
	DestroyWindow (hWindow);
	break;
      }
      return 0;
    }
  case WM_DESTROY:
    res = 0;
    break;
  }
 
  return DefWindowProc(hWindow,uMessage,wparam,lparam);
}
 
HWND
create_window (int width, int height)
{
  WNDCLASS wc;
  RECT       rect;
  HWND       window;
 
  memset (&wc, 0, sizeof (wc));
  wc.style = CS_HREDRAW | CS_VREDRAW;
  wc.lpfnWndProc = window_procedure;
  wc.cbClsExtra = 0;
  wc.cbWndExtra = 0;
  wc.hInstance = GetModuleHandle(NULL);
  wc.hIcon = LoadIcon (NULL, IDI_APPLICATION);
  wc.hCursor = LoadCursor (NULL, IDC_ARROW);
  wc.hbrBackground = GetSysColorBrush(COLOR_BTNFACE);
  wc.lpszMenuName =  NULL;
  wc.lpszClassName = "class name";
 
  RegisterClass(&wc);
 
  rect.left = 0;
  rect.top = 0;
  rect.right = width;
  rect.bottom = height;
  if (!AdjustWindowRect(&rect, WS_OVERLAPPEDWINDOW | WS_SIZEBOX, FALSE))
    return NULL;
 
  window = CreateWindow(  "class name", "",
			  WS_OVERLAPPEDWINDOW | WS_SIZEBOX,
			  100, 100,
			  rect.right - rect.left,
			  rect.bottom - rect.top,
			  NULL,
			  NULL,
			  GetModuleHandle(NULL),
			  NULL);
  return window;
}
 
void
destroy_window (HWND w)
{
  DestroyWindow (w);
}
 
void
triangle_draw (LPDIRECT3DDEVICE9 device,
	       LPDIRECT3DVERTEXBUFFER9 vb_triangle)
{
  D3DVERTEX vertex_triangle[] = {
    { 16.0f, 40.0f,  0.0f, 1.0f, D3DCOLOR_ARGB(255, 255, 0, 0) },
    { 14.0f, 27.0f, 0.0f, 1.0f, D3DCOLOR_ARGB(255, 255, 0, 0) },
    { 18.0f, 35.0f, 0.0f, 1.0f, D3DCOLOR_ARGB(255, 255, 0, 0) }};
  void *data;
 
  if (FAILED (vb_triangle->Lock (0, 0, (void **)&data, 0))) {
    printf ("vb lock failed\n");
    return;
  }
 
  memcpy (data, vertex_triangle, sizeof (vertex_triangle));
 
  if (FAILED (vb_triangle->Unlock ())) {
    printf ("vb unlock failed\n");
    return;
  }
 
  device->SetStreamSource (0, vb_triangle, 0, sizeof (D3DVERTEX));
  if (FAILED (device->DrawPrimitive (D3DPT_TRIANGLELIST, 0, 1))) {
    printf ("draw pritive failed\n");
  }
}
 
void
rectangle_draw (LPDIRECT3DDEVICE9 device,
		LPDIRECT3DVERTEXBUFFER9 vb_rectangle)
{
  D3DVERTEX vertex_rectangle[] = {
    { 50.0f, 100.0f, 0.0f, 1.0f, D3DCOLOR_ARGB(255, 255, 255, 0) },
    { 70.0f, 100.0f, 0.0f, 1.0f, D3DCOLOR_ARGB(255, 255, 255, 0) },
    { 70.0f, 200.0f, 0.0f, 1.0f, D3DCOLOR_ARGB(255, 255, 255, 0) },
    { 50.0f, 200.0f, 0.0f, 1.0f, D3DCOLOR_ARGB(255, 255, 255, 0) }};
  void *data;
 
  if (FAILED (vb_rectangle->Lock (0, 0, (void **)&data, 0))) {
    printf ("vb lock failed\n");
    return;
  }
 
  memcpy (data, vertex_rectangle, sizeof (vertex_rectangle));
 
  if (FAILED (vb_rectangle->Unlock ())) {
    printf ("vb unlock failed\n");
    return;
  }
 
  device->SetStreamSource (0, vb_rectangle, 0, sizeof (D3DVERTEX));
  if (FAILED (device->DrawPrimitive (D3DPT_TRIANGLEFAN, 0, 2))) {
    printf ("draw pritive failed\n");
  }
}
 
int
main ()
{
  LPDIRECT3D9       object;
  LPDIRECT3DDEVICE9 device;
  LPDIRECT3DVERTEXBUFFER9 vb_triangle;
  LPDIRECT3DVERTEXBUFFER9 vb_rectangle;
  HWND window;
 
  window = create_window (800, 600);
  if (!window) {
    printf ("window init failed\n");
    return EXIT_FAILURE;
  }
 
  ShowWindow(window, SW_SHOWNORMAL);
  UpdateWindow(window);
 
  if (!d3d_init (window, &object, &device)) {
    printf ("d3d init failed\n");
    return EXIT_FAILURE;
  }
 
  if (FAILED(device->CreateVertexBuffer (sizeof (D3DVERTEX) * 3,
					 D3DUSAGE_WRITEONLY,
					 D3DFVF_CUSTOMVERTEX,
					 D3DPOOL_MANAGED,
					 &vb_triangle, NULL))) {
    printf ("vertex buffer create failed\n");
    d3d_shutdown (object, device);
    destroy_window (window);
    return EXIT_FAILURE;
  }
 
  if (FAILED(device->CreateVertexBuffer (sizeof (D3DVERTEX) * 4,
					 D3DUSAGE_WRITEONLY,
					 D3DFVF_CUSTOMVERTEX,
					 D3DPOOL_MANAGED,
					 &vb_rectangle, NULL))) {
    printf ("vertex buffer create failed\n");
    d3d_shutdown (object, device);
    destroy_window (window);
    return EXIT_FAILURE;
  }
 
  while (res) {
    MSG msg;
 
    if (PeekMessage (&msg, NULL, 0, 0, PM_REMOVE)) {
      TranslateMessage (&msg);
      DispatchMessage (&msg);
    }
 
    device->Clear (0, NULL, D3DCLEAR_TARGET,
		   D3DCOLOR_XRGB (0, 0, 0),
		   0.0f, 0);
    device->BeginScene ();
 
    rectangle_draw (device, vb_rectangle);
    triangle_draw (device, vb_triangle);
 
    device->EndScene ();
    device->Present (NULL, NULL, NULL, NULL);
  }
 
  d3d_shutdown (object, device);
  destroy_window (window);
 
  return EXIT_SUCCESS;
}
Comme vous pouvez le voir, il y a :
  • une fonction qui initialise direct3d (l'object et un device)
  • une fonction qui cree une fenetre
  • deux fonctions qui affichent un triangle et un rectangle a l'aide de vertex buffers
  • la fonction principale avec la boucle des messages et la fonction qui gere les messages


Mon but est d'avoir la vitesse de rendu la plus rapide possible en 2d.

Voici mes questions
  1. Est-ce que le choix des vertex buffers est le meilleur ? (je pense aux vertex shaders, bien que je ne sache pas comment les utiliser, je connais peu de choses sur eux)
  2. Est-ce que le choix du vertex personalise est correct pour faire de la 2d uniquement ?
  3. Est-ce que l'initialisation du device est optimale pour faire de la 2d ?
  4. Est-ce qu'il est possible d'utiliser certaines techniques utilisant plusieurs back buffers pour accelerer le rendu ?
  5. J'ai vu sur Gamedev la technique "round robin". Est-ce qu'elle permettra d'accelerer largement le rendu, ou bien est-ce que ca sera negligeable ?
  6. Y a-t-il d'autres techniques faisant intervenir direct3d pour afficher rapidement de la 2d ?


merci