RenderInterfaceDirectX.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  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* vertices, int num_vertices, int* indices, int num_indices, const Rocket::Core::TextureHandle texture, const Rocket::Core::Vector2f& translation)
  60. {
  61. /// @TODO We've chosen to not support non-compiled geometry in the DirectX renderer. If you wanted to render non-compiled
  62. /// geometry, for example for very small sections of geometry, you could use DrawIndexedPrimitiveUP or write to a
  63. /// dynamic vertex buffer which is flushed when either the texture changes or compiled geometry is drawn.
  64. if(g_pd3dDevice == NULL)
  65. {
  66. return;
  67. }
  68. /// @TODO, HACK, just use the compiled geometry framework for now, this is inefficient but better than absolutely nothing
  69. /// for the time being
  70. Rocket::Core::CompiledGeometryHandle geom = this->CompileGeometry(vertices, num_vertices, indices, num_indices, texture);
  71. this->RenderCompiledGeometry(geom, translation);
  72. this->ReleaseCompiledGeometry(geom);
  73. }
  74. // Called by Rocket when it wants to compile geometry it believes will be static for the forseeable future.
  75. Rocket::Core::CompiledGeometryHandle RenderInterfaceDirectX::CompileGeometry(Rocket::Core::Vertex* vertices, int num_vertices, int* indices, int num_indices, Rocket::Core::TextureHandle texture)
  76. {
  77. if(g_pd3dDevice == NULL)
  78. {
  79. return false;
  80. }
  81. // Construct a new RocketD3D9CompiledGeometry structure, which will be returned as the handle, and the buffers to
  82. // store the geometry.
  83. RocketD3D9CompiledGeometry* geometry = new RocketD3D9CompiledGeometry();
  84. g_pd3dDevice->CreateVertexBuffer(num_vertices * sizeof(RocketD3D9Vertex), D3DUSAGE_WRITEONLY, vertex_fvf, D3DPOOL_DEFAULT, &geometry->vertices, NULL);
  85. g_pd3dDevice->CreateIndexBuffer(num_indices * sizeof(unsigned int), D3DUSAGE_WRITEONLY, D3DFMT_INDEX32, D3DPOOL_DEFAULT, &geometry->indices, NULL);
  86. // Fill the vertex buffer.
  87. RocketD3D9Vertex* d3d9_vertices;
  88. geometry->vertices->Lock(0, 0, (void**) &d3d9_vertices, 0);
  89. for (int i = 0; i < num_vertices; ++i)
  90. {
  91. d3d9_vertices[i].x = vertices[i].position.x;
  92. d3d9_vertices[i].y = vertices[i].position.y;
  93. d3d9_vertices[i].z = 0;
  94. d3d9_vertices[i].colour = D3DCOLOR_RGBA(vertices[i].colour.red, vertices[i].colour.green, vertices[i].colour.blue, vertices[i].colour.alpha);
  95. d3d9_vertices[i].u = vertices[i].tex_coord[0];
  96. d3d9_vertices[i].v = vertices[i].tex_coord[1];
  97. }
  98. geometry->vertices->Unlock();
  99. // Fill the index buffer.
  100. unsigned int* d3d9_indices;
  101. geometry->indices->Lock(0, 0, (void**) &d3d9_indices, 0);
  102. memcpy(d3d9_indices, indices, sizeof(unsigned int) * num_indices);
  103. geometry->indices->Unlock();
  104. geometry->num_vertices = (DWORD) num_vertices;
  105. geometry->num_primitives = (DWORD) num_indices / 3;
  106. geometry->texture = texture == NULL ? NULL : (LPDIRECT3DTEXTURE9) texture;
  107. return (Rocket::Core::CompiledGeometryHandle)geometry;
  108. }
  109. // Called by Rocket when it wants to render application-compiled geometry.
  110. void RenderInterfaceDirectX::RenderCompiledGeometry(Rocket::Core::CompiledGeometryHandle geometry, const Rocket::Core::Vector2f& translation)
  111. {
  112. if(g_pd3dDevice == NULL)
  113. {
  114. return;
  115. }
  116. // Build and set the transform matrix.
  117. D3DXMATRIX world_transform;
  118. D3DXMatrixTranslation(&world_transform, translation.x, translation.y, 0);
  119. g_pd3dDevice->SetTransform(D3DTS_WORLD, &world_transform);
  120. RocketD3D9CompiledGeometry* d3d9_geometry = (RocketD3D9CompiledGeometry*) geometry;
  121. // Set the vertex format for the Rocket vertices, and bind the vertex and index buffers.
  122. g_pd3dDevice->SetFVF(vertex_fvf);
  123. g_pd3dDevice->SetStreamSource(0, d3d9_geometry->vertices, 0, sizeof(RocketD3D9Vertex));
  124. g_pd3dDevice->SetIndices(d3d9_geometry->indices);
  125. // Set the texture, if this geometry has one.
  126. if (d3d9_geometry->texture != NULL)
  127. g_pd3dDevice->SetTexture(0, d3d9_geometry->texture);
  128. else
  129. g_pd3dDevice->SetTexture(0, NULL);
  130. // Draw the primitives.
  131. g_pd3dDevice->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, 0, d3d9_geometry->num_vertices, 0, d3d9_geometry->num_primitives);
  132. }
  133. // Called by Rocket when it wants to release application-compiled geometry.
  134. void RenderInterfaceDirectX::ReleaseCompiledGeometry(Rocket::Core::CompiledGeometryHandle geometry)
  135. {
  136. RocketD3D9CompiledGeometry* d3d9_geometry = (RocketD3D9CompiledGeometry*) geometry;
  137. d3d9_geometry->vertices->Release();
  138. d3d9_geometry->indices->Release();
  139. delete d3d9_geometry;
  140. }
  141. // Called by Rocket when it wants to enable or disable scissoring to clip content.
  142. void RenderInterfaceDirectX::EnableScissorRegion(bool enable)
  143. {
  144. if(g_pd3dDevice == NULL)
  145. {
  146. return;
  147. }
  148. g_pd3dDevice->SetRenderState(D3DRS_SCISSORTESTENABLE, enable);
  149. }
  150. // Called by Rocket when it wants to change the scissor region.
  151. void RenderInterfaceDirectX::SetScissorRegion(int x, int y, int width, int height)
  152. {
  153. if(g_pd3dDevice == NULL)
  154. {
  155. return;
  156. }
  157. RECT scissor_rect;
  158. scissor_rect.left = x;
  159. scissor_rect.right = x + width;
  160. scissor_rect.top = y;
  161. scissor_rect.bottom = y + height;
  162. g_pd3dDevice->SetScissorRect(&scissor_rect);
  163. }
  164. // Set to byte packing, or the compiler will expand our struct, which means it won't read correctly from file
  165. #pragma pack(1)
  166. struct TGAHeader
  167. {
  168. char idLength;
  169. char colourMapType;
  170. char dataType;
  171. short int colourMapOrigin;
  172. short int colourMapLength;
  173. char colourMapDepth;
  174. short int xOrigin;
  175. short int yOrigin;
  176. short int width;
  177. short int height;
  178. char bitsPerPixel;
  179. char imageDescriptor;
  180. };
  181. // Restore packing
  182. #pragma pack()
  183. // Called by Rocket when a texture is required by the library.
  184. bool RenderInterfaceDirectX::LoadTexture(Rocket::Core::TextureHandle& texture_handle, Rocket::Core::Vector2i& texture_dimensions, const Rocket::Core::String& source)
  185. {
  186. if(g_pd3dDevice == NULL)
  187. {
  188. return false;
  189. }
  190. Rocket::Core::FileInterface* file_interface = Rocket::Core::GetFileInterface();
  191. Rocket::Core::FileHandle file_handle = file_interface->Open(source);
  192. if (file_handle == NULL)
  193. return false;
  194. file_interface->Seek(file_handle, 0, SEEK_END);
  195. size_t buffer_size = file_interface->Tell(file_handle);
  196. file_interface->Seek(file_handle, 0, SEEK_SET);
  197. char* buffer = new char[buffer_size];
  198. file_interface->Read(buffer, buffer_size, file_handle);
  199. file_interface->Close(file_handle);
  200. TGAHeader header;
  201. memcpy(&header, buffer, sizeof(TGAHeader));
  202. int color_mode = header.bitsPerPixel / 8;
  203. int image_size = header.width * header.height * 4; // We always make 32bit textures
  204. if (header.dataType != 2)
  205. {
  206. Rocket::Core::Log::Message(Rocket::Core::Log::LT_ERROR, "Only 24/32bit uncompressed TGAs are supported.");
  207. return false;
  208. }
  209. // Ensure we have at least 3 colors
  210. if (color_mode < 3)
  211. {
  212. Rocket::Core::Log::Message(Rocket::Core::Log::LT_ERROR, "Only 24 and 32bit textures are supported");
  213. return false;
  214. }
  215. const char* image_src = buffer + sizeof(TGAHeader);
  216. unsigned char* image_dest = new unsigned char[image_size];
  217. // Targa is BGR, swap to RGB and flip Y axis
  218. for (long y = 0; y < header.height; y++)
  219. {
  220. long read_index = y * header.width * color_mode;
  221. long write_index = ((header.imageDescriptor & 32) != 0) ? read_index : (header.height - y - 1) * header.width * color_mode;
  222. for (long x = 0; x < header.width; x++)
  223. {
  224. image_dest[write_index] = image_src[read_index+2];
  225. image_dest[write_index+1] = image_src[read_index+1];
  226. image_dest[write_index+2] = image_src[read_index];
  227. if (color_mode == 4)
  228. image_dest[write_index+3] = image_src[read_index+3];
  229. else
  230. image_dest[write_index+3] = 255;
  231. write_index += 4;
  232. read_index += color_mode;
  233. }
  234. }
  235. texture_dimensions.x = header.width;
  236. texture_dimensions.y = header.height;
  237. bool success = GenerateTexture(texture_handle, image_dest, texture_dimensions);
  238. delete [] image_dest;
  239. delete [] buffer;
  240. return success;
  241. }
  242. // Called by Rocket when a texture is required to be built from an internally-generated sequence of pixels.
  243. bool RenderInterfaceDirectX::GenerateTexture(Rocket::Core::TextureHandle& texture_handle, const byte* source, const Rocket::Core::Vector2i& source_dimensions)
  244. {
  245. if(g_pd3dDevice == NULL)
  246. {
  247. return false;
  248. }
  249. // Create a Direct3DTexture9, which will be set as the texture handle. Note that we only create one surface for
  250. // this texture; because we're rendering in a 2D context, mip-maps are not required.
  251. LPDIRECT3DTEXTURE9 d3d9_texture;
  252. if (g_pd3dDevice->CreateTexture(source_dimensions.x, source_dimensions.y, 1, 0, D3DFMT_A8R8G8B8, D3DPOOL_MANAGED, &d3d9_texture, NULL) != D3D_OK)
  253. return false;
  254. // Lock the top surface and write the pixel data onto it.
  255. D3DLOCKED_RECT locked_rect;
  256. d3d9_texture->LockRect(0, &locked_rect, NULL, 0);
  257. for (int y = 0; y < source_dimensions.y; ++y)
  258. {
  259. for (int x = 0; x < source_dimensions.x; ++x)
  260. {
  261. const byte* source_pixel = source + (source_dimensions.x * 4 * y) + (x * 4);
  262. byte* destination_pixel = ((byte*) locked_rect.pBits) + locked_rect.Pitch * y + x * 4;
  263. destination_pixel[0] = source_pixel[2];
  264. destination_pixel[1] = source_pixel[1];
  265. destination_pixel[2] = source_pixel[0];
  266. destination_pixel[3] = source_pixel[3];
  267. }
  268. }
  269. d3d9_texture->UnlockRect(0);
  270. // Set the handle on the Rocket texture structure.
  271. texture_handle = (Rocket::Core::TextureHandle)d3d9_texture;
  272. return true;
  273. }
  274. // Called by Rocket when a loaded texture is no longer required.
  275. void RenderInterfaceDirectX::ReleaseTexture(Rocket::Core::TextureHandle texture_handle)
  276. {
  277. ((LPDIRECT3DTEXTURE9) texture_handle)->Release();
  278. }
  279. // Returns the native horizontal texel offset for the renderer.
  280. float RenderInterfaceDirectX::GetHorizontalTexelOffset()
  281. {
  282. return -0.5f;
  283. }
  284. // Returns the native vertical texel offset for the renderer.
  285. float RenderInterfaceDirectX::GetVerticalTexelOffset()
  286. {
  287. return -0.5f;
  288. }