RenderInterfaceDirectX.cpp 11 KB

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