RenderInterfaceDirectX.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. /*
  2. * This source file is part of libRocket, the HTML/CSS Interface Middleware
  3. *
  4. * For the latest information, see http://www.librocket.com
  5. *
  6. * Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in
  16. * all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. * THE SOFTWARE.
  25. *
  26. */
  27. #include "RenderInterfaceDirectX.h"
  28. #include <Rocket/Core.h>
  29. #include <d3dx9.h>
  30. // This structure is created for each set of geometry that Rocket compiles. It stores the vertex and index buffers and
  31. // the texture associated with the geometry, if one was specified.
  32. struct RocketD3D9CompiledGeometry
  33. {
  34. LPDIRECT3DVERTEXBUFFER9 vertices;
  35. DWORD num_vertices;
  36. LPDIRECT3DINDEXBUFFER9 indices;
  37. DWORD num_primitives;
  38. LPDIRECT3DTEXTURE9 texture;
  39. };
  40. // The internal format of the vertex we use for rendering Rocket geometry. We could optimise space by having a second
  41. // untextured vertex for use when rendering coloured borders and backgrounds.
  42. struct RocketD3D9Vertex
  43. {
  44. FLOAT x, y, z;
  45. DWORD colour;
  46. FLOAT u, v;
  47. };
  48. DWORD vertex_fvf = D3DFVF_XYZ | D3DFVF_DIFFUSE | D3DFVF_TEX1;
  49. RenderInterfaceDirectX::RenderInterfaceDirectX()
  50. {
  51. g_pD3D = NULL;
  52. g_pd3dDevice = NULL;
  53. m_rocket_context = NULL;
  54. }
  55. RenderInterfaceDirectX::~RenderInterfaceDirectX()
  56. {
  57. }
  58. // Called by Rocket when it wants to render geometry that it does not wish to optimise.
  59. void RenderInterfaceDirectX::RenderGeometry(Rocket::Core::Vertex* ROCKET_UNUSED_PARAMETER(vertices), int ROCKET_UNUSED_PARAMETER(num_vertices), int* ROCKET_UNUSED_PARAMETER(indices), int ROCKET_UNUSED_PARAMETER(num_indices), const Rocket::Core::TextureHandle ROCKET_UNUSED_PARAMETER(texture), const Rocket::Core::Vector2f& ROCKET_UNUSED_PARAMETER(translation))
  60. {
  61. ROCKET_UNUSED(vertices);
  62. ROCKET_UNUSED(num_vertices);
  63. ROCKET_UNUSED(indices);
  64. ROCKET_UNUSED(num_indices);
  65. ROCKET_UNUSED(texture);
  66. ROCKET_UNUSED(translation);
  67. // We've chosen to not support non-compiled geometry in the DirectX renderer. If you wanted to render non-compiled
  68. // geometry, for example for very small sections of geometry, you could use DrawIndexedPrimitiveUP or write to a
  69. // dynamic vertex buffer which is flushed when either the texture changes or compiled geometry is drawn.
  70. }
  71. // Called by Rocket when it wants to compile geometry it believes will be static for the forseeable future.
  72. Rocket::Core::CompiledGeometryHandle RenderInterfaceDirectX::CompileGeometry(Rocket::Core::Vertex* vertices, int num_vertices, int* indices, int num_indices, Rocket::Core::TextureHandle texture)
  73. {
  74. if(g_pd3dDevice == NULL)
  75. {
  76. return false;
  77. }
  78. // Construct a new RocketD3D9CompiledGeometry structure, which will be returned as the handle, and the buffers to
  79. // store the geometry.
  80. RocketD3D9CompiledGeometry* geometry = new RocketD3D9CompiledGeometry();
  81. g_pd3dDevice->CreateVertexBuffer(num_vertices * sizeof(RocketD3D9Vertex), D3DUSAGE_WRITEONLY, vertex_fvf, D3DPOOL_DEFAULT, &geometry->vertices, NULL);
  82. g_pd3dDevice->CreateIndexBuffer(num_indices * sizeof(unsigned int), D3DUSAGE_WRITEONLY, D3DFMT_INDEX32, D3DPOOL_DEFAULT, &geometry->indices, NULL);
  83. // Fill the vertex buffer.
  84. RocketD3D9Vertex* d3d9_vertices;
  85. geometry->vertices->Lock(0, 0, (void**) &d3d9_vertices, 0);
  86. for (int i = 0; i < num_vertices; ++i)
  87. {
  88. d3d9_vertices[i].x = vertices[i].position.x;
  89. d3d9_vertices[i].y = vertices[i].position.y;
  90. d3d9_vertices[i].z = 0;
  91. d3d9_vertices[i].colour = D3DCOLOR_RGBA(vertices[i].colour.red, vertices[i].colour.green, vertices[i].colour.blue, vertices[i].colour.alpha);
  92. d3d9_vertices[i].u = vertices[i].tex_coord[0];
  93. d3d9_vertices[i].v = vertices[i].tex_coord[1];
  94. }
  95. geometry->vertices->Unlock();
  96. // Fill the index buffer.
  97. unsigned int* d3d9_indices;
  98. geometry->indices->Lock(0, 0, (void**) &d3d9_indices, 0);
  99. memcpy(d3d9_indices, indices, sizeof(unsigned int) * num_indices);
  100. geometry->indices->Unlock();
  101. geometry->num_vertices = (DWORD) num_vertices;
  102. geometry->num_primitives = (DWORD) num_indices / 3;
  103. geometry->texture = texture == NULL ? NULL : (LPDIRECT3DTEXTURE9) texture;
  104. return (Rocket::Core::CompiledGeometryHandle)geometry;
  105. }
  106. // Called by Rocket when it wants to render application-compiled geometry.
  107. void RenderInterfaceDirectX::RenderCompiledGeometry(Rocket::Core::CompiledGeometryHandle geometry, const Rocket::Core::Vector2f& translation)
  108. {
  109. if(g_pd3dDevice == NULL)
  110. {
  111. return;
  112. }
  113. // Build and set the transform matrix.
  114. D3DXMATRIX world_transform;
  115. D3DXMatrixTranslation(&world_transform, translation.x, translation.y, 0);
  116. g_pd3dDevice->SetTransform(D3DTS_WORLD, &world_transform);
  117. RocketD3D9CompiledGeometry* d3d9_geometry = (RocketD3D9CompiledGeometry*) geometry;
  118. // Set the vertex format for the Rocket vertices, and bind the vertex and index buffers.
  119. g_pd3dDevice->SetFVF(vertex_fvf);
  120. g_pd3dDevice->SetStreamSource(0, d3d9_geometry->vertices, 0, sizeof(RocketD3D9Vertex));
  121. g_pd3dDevice->SetIndices(d3d9_geometry->indices);
  122. // Set the texture, if this geometry has one.
  123. if (d3d9_geometry->texture != NULL)
  124. g_pd3dDevice->SetTexture(0, d3d9_geometry->texture);
  125. else
  126. g_pd3dDevice->SetTexture(0, NULL);
  127. // Draw the primitives.
  128. g_pd3dDevice->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, 0, d3d9_geometry->num_vertices, 0, d3d9_geometry->num_primitives);
  129. }
  130. // Called by Rocket when it wants to release application-compiled geometry.
  131. void RenderInterfaceDirectX::ReleaseCompiledGeometry(Rocket::Core::CompiledGeometryHandle geometry)
  132. {
  133. RocketD3D9CompiledGeometry* d3d9_geometry = (RocketD3D9CompiledGeometry*) geometry;
  134. d3d9_geometry->vertices->Release();
  135. d3d9_geometry->indices->Release();
  136. delete d3d9_geometry;
  137. }
  138. // Called by Rocket when it wants to enable or disable scissoring to clip content.
  139. void RenderInterfaceDirectX::EnableScissorRegion(bool enable)
  140. {
  141. if(g_pd3dDevice == NULL)
  142. {
  143. return;
  144. }
  145. g_pd3dDevice->SetRenderState(D3DRS_SCISSORTESTENABLE, enable);
  146. }
  147. // Called by Rocket when it wants to change the scissor region.
  148. void RenderInterfaceDirectX::SetScissorRegion(int x, int y, int width, int height)
  149. {
  150. if(g_pd3dDevice == NULL)
  151. {
  152. return;
  153. }
  154. RECT scissor_rect;
  155. scissor_rect.left = x;
  156. scissor_rect.right = x + width;
  157. scissor_rect.top = y;
  158. scissor_rect.bottom = y + height;
  159. g_pd3dDevice->SetScissorRect(&scissor_rect);
  160. }
  161. // Set to byte packing, or the compiler will expand our struct, which means it won't read correctly from file
  162. #pragma pack(1)
  163. struct TGAHeader
  164. {
  165. char idLength;
  166. char colourMapType;
  167. char dataType;
  168. short int colourMapOrigin;
  169. short int colourMapLength;
  170. char colourMapDepth;
  171. short int xOrigin;
  172. short int yOrigin;
  173. short int width;
  174. short int height;
  175. char bitsPerPixel;
  176. char imageDescriptor;
  177. };
  178. // Restore packing
  179. #pragma pack()
  180. // Called by Rocket when a texture is required by the library.
  181. bool RenderInterfaceDirectX::LoadTexture(Rocket::Core::TextureHandle& texture_handle, Rocket::Core::Vector2i& texture_dimensions, const Rocket::Core::String& source)
  182. {
  183. if(g_pd3dDevice == NULL)
  184. {
  185. return false;
  186. }
  187. Rocket::Core::FileInterface* file_interface = Rocket::Core::GetFileInterface();
  188. Rocket::Core::FileHandle file_handle = file_interface->Open(source);
  189. if (file_handle == NULL)
  190. return false;
  191. file_interface->Seek(file_handle, 0, SEEK_END);
  192. size_t buffer_size = file_interface->Tell(file_handle);
  193. file_interface->Seek(file_handle, 0, SEEK_SET);
  194. char* buffer = new char[buffer_size];
  195. file_interface->Read(buffer, buffer_size, file_handle);
  196. file_interface->Close(file_handle);
  197. TGAHeader header;
  198. memcpy(&header, buffer, sizeof(TGAHeader));
  199. int color_mode = header.bitsPerPixel / 8;
  200. int image_size = header.width * header.height * 4; // We always make 32bit textures
  201. if (header.dataType != 2)
  202. {
  203. Rocket::Core::Log::Message(Rocket::Core::Log::LT_ERROR, "Only 24/32bit uncompressed TGAs are supported.");
  204. return false;
  205. }
  206. // Ensure we have at least 3 colors
  207. if (color_mode < 3)
  208. {
  209. Rocket::Core::Log::Message(Rocket::Core::Log::LT_ERROR, "Only 24 and 32bit textures are supported");
  210. return false;
  211. }
  212. const char* image_src = buffer + sizeof(TGAHeader);
  213. unsigned char* image_dest = new unsigned char[image_size];
  214. // Targa is BGR, swap to RGB and flip Y axis
  215. for (long y = 0; y < header.height; y++)
  216. {
  217. long read_index = y * header.width * color_mode;
  218. long write_index = ((header.imageDescriptor & 32) != 0) ? read_index : (header.height - y - 1) * header.width * color_mode;
  219. for (long x = 0; x < header.width; x++)
  220. {
  221. image_dest[write_index] = image_src[read_index+2];
  222. image_dest[write_index+1] = image_src[read_index+1];
  223. image_dest[write_index+2] = image_src[read_index];
  224. if (color_mode == 4)
  225. image_dest[write_index+3] = image_src[read_index+3];
  226. else
  227. image_dest[write_index+3] = 255;
  228. write_index += 4;
  229. read_index += color_mode;
  230. }
  231. }
  232. texture_dimensions.x = header.width;
  233. texture_dimensions.y = header.height;
  234. bool success = GenerateTexture(texture_handle, image_dest, texture_dimensions);
  235. delete [] image_dest;
  236. delete [] buffer;
  237. return success;
  238. }
  239. // Called by Rocket when a texture is required to be built from an internally-generated sequence of pixels.
  240. bool RenderInterfaceDirectX::GenerateTexture(Rocket::Core::TextureHandle& texture_handle, const byte* source, const Rocket::Core::Vector2i& source_dimensions)
  241. {
  242. if(g_pd3dDevice == NULL)
  243. {
  244. return false;
  245. }
  246. // Create a Direct3DTexture9, which will be set as the texture handle. Note that we only create one surface for
  247. // this texture; because we're rendering in a 2D context, mip-maps are not required.
  248. LPDIRECT3DTEXTURE9 d3d9_texture;
  249. if (g_pd3dDevice->CreateTexture(source_dimensions.x, source_dimensions.y, 1, 0, D3DFMT_A8R8G8B8, D3DPOOL_MANAGED, &d3d9_texture, NULL) != D3D_OK)
  250. return false;
  251. // Lock the top surface and write the pixel data onto it.
  252. D3DLOCKED_RECT locked_rect;
  253. d3d9_texture->LockRect(0, &locked_rect, NULL, 0);
  254. for (int y = 0; y < source_dimensions.y; ++y)
  255. {
  256. for (int x = 0; x < source_dimensions.x; ++x)
  257. {
  258. const byte* source_pixel = source + (source_dimensions.x * 4 * y) + (x * 4);
  259. byte* destination_pixel = ((byte*) locked_rect.pBits) + locked_rect.Pitch * y + x * 4;
  260. destination_pixel[0] = source_pixel[2];
  261. destination_pixel[1] = source_pixel[1];
  262. destination_pixel[2] = source_pixel[0];
  263. destination_pixel[3] = source_pixel[3];
  264. }
  265. }
  266. d3d9_texture->UnlockRect(0);
  267. // Set the handle on the Rocket texture structure.
  268. texture_handle = (Rocket::Core::TextureHandle)d3d9_texture;
  269. return true;
  270. }
  271. // Called by Rocket when a loaded texture is no longer required.
  272. void RenderInterfaceDirectX::ReleaseTexture(Rocket::Core::TextureHandle texture_handle)
  273. {
  274. ((LPDIRECT3DTEXTURE9) texture_handle)->Release();
  275. }
  276. // Returns the native horizontal texel offset for the renderer.
  277. float RenderInterfaceDirectX::GetHorizontalTexelOffset()
  278. {
  279. return -0.5f;
  280. }
  281. // Returns the native vertical texel offset for the renderer.
  282. float RenderInterfaceDirectX::GetVerticalTexelOffset()
  283. {
  284. return -0.5f;
  285. }