RenderInterfaceDirectX.cpp 11 KB

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