RenderInterfaceDirectx10.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  1. #include "RenderInterfaceDirectX10.h"
  2. #include <Rocket/Core.h>
  3. #include "D3D10Effect.h"
  4. //RocketD3D10 Texture, this contains the actual texture and the resource view
  5. //for sending it to the effect
  6. struct RocketD3D10Texture
  7. {
  8. ID3D10ShaderResourceView * textureView;
  9. ID3D10Texture2D * texture2D;
  10. };
  11. // This structure is created for each set of geometry that Rocket compiles. It stores the vertex and index buffers and
  12. // the texture associated with the geometry, if one was specified.
  13. struct RocketD310DCompiledGeometry
  14. {
  15. //Vertex Buffer
  16. ID3D10Buffer * vertices;
  17. DWORD num_vertices;
  18. //Index buffer
  19. ID3D10Buffer * indices;
  20. DWORD num_primitives;
  21. //Texture
  22. RocketD3D10Texture * texture;
  23. };
  24. // The internal format of the vertex we use for rendering Rocket geometry. We could optimise space by having a second
  25. // untextured vertex for use when rendering coloured borders and backgrounds.
  26. struct RocketD3D10Vertex
  27. {
  28. FLOAT x, y, z;
  29. D3DXCOLOR colour;
  30. FLOAT u, v;
  31. };
  32. //The layout of the vertices
  33. const D3D10_INPUT_ELEMENT_DESC layout[] =
  34. {
  35. { "POSITION",0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D10_INPUT_PER_VERTEX_DATA, 0 },
  36. { "COLOR",0, DXGI_FORMAT_R32G32B32A32_FLOAT, 0, 12, D3D10_INPUT_PER_VERTEX_DATA, 0},
  37. {"TEXCOORD",0,DXGI_FORMAT_R32G32_FLOAT,0,28,D3D10_INPUT_PER_VERTEX_DATA,0},
  38. };
  39. //The constructor of the render
  40. RenderInterfaceDirectX10::RenderInterfaceDirectX10(ID3D10Device * pD3D10Device,float screenWidth,float screenHeight)
  41. {
  42. m_pD3D10Device=pD3D10Device;
  43. setupEffect();
  44. //Create our view and projection matrix
  45. D3DXMatrixOrthoOffCenterLH(&m_matProjection, 0, screenWidth, screenHeight, 0, -1, 1);
  46. m_pProjectionMatrixVariable->SetMatrix((float*)m_matProjection);
  47. D3D10_RASTERIZER_DESC rasterDesc;
  48. rasterDesc.FillMode=D3D10_FILL_SOLID;
  49. rasterDesc.CullMode=D3D10_CULL_NONE;
  50. rasterDesc.ScissorEnable=TRUE;
  51. rasterDesc.FrontCounterClockwise=TRUE;
  52. m_pD3D10Device->CreateRasterizerState(&rasterDesc,&m_pScissorTestEnable);
  53. rasterDesc.ScissorEnable=FALSE;
  54. m_pD3D10Device->CreateRasterizerState(&rasterDesc,&m_pScissorTestDisable);
  55. }
  56. //Loads the effect from memory and retrieves initial variables from the effect
  57. void RenderInterfaceDirectX10::setupEffect()
  58. {
  59. //The pass we are going to use
  60. ID3D10EffectPass *pass=NULL;
  61. DWORD dwShaderFlags = 0;
  62. #if defined( DEBUG ) || defined( _DEBUG )
  63. // Set the D3D10_SHADER_DEBUG flag to embed debug information in the shaders.
  64. // Setting this flag improves the shader debugging experience, but still allows
  65. // the shaders to be optimized and to run exactly the way they will run in
  66. // the release configuration of this program. - BMD
  67. dwShaderFlags |= D3D10_SHADER_DEBUG;
  68. #endif
  69. //Create our effect from Memory
  70. if (FAILED(D3DX10CreateEffectFromMemory((void*)pEffectData,strlen(pEffectData),"DefaultEffect",NULL,NULL,"fx_4_0",dwShaderFlags,0,m_pD3D10Device,NULL,NULL,&m_pEffect,NULL,NULL)))
  71. {
  72. //Log error
  73. Rocket::Core::Log::Message(Rocket::Core::Log::LT_ERROR, "Can't create default effect for rendering, graphics card may not support Shader Model 4");
  74. }
  75. //Number of elements in the layout - BMD
  76. UINT numElements = sizeof(layout)/sizeof(D3D10_INPUT_ELEMENT_DESC);
  77. //Get the pass description so we can get some info about the input signature of the vertices
  78. D3D10_PASS_DESC passDesc;
  79. m_pTechnique=m_pEffect->GetTechniqueByName("Render");
  80. pass=m_pTechnique->GetPassByName("P0");
  81. pass->GetDesc(&passDesc);
  82. //create input layout, to allow us to map our vertex structure to the one held in the effect
  83. m_pD3D10Device->CreateInputLayout(layout,numElements,passDesc.pIAInputSignature,passDesc.IAInputSignatureSize,&m_pVertexLayout);
  84. //grab effect variables
  85. m_pWorldMatrixVariable=m_pEffect->GetVariableByName("matWorld")->AsMatrix();
  86. m_pProjectionMatrixVariable=m_pEffect->GetVariableByName("matProjection")->AsMatrix();
  87. //grab texture variable
  88. m_pDiffuseTextureVariable=m_pEffect->GetVariableByName("diffuseMap")->AsShaderResource();
  89. }
  90. RenderInterfaceDirectX10::~RenderInterfaceDirectX10()
  91. {
  92. m_pScissorTestDisable->Release();
  93. m_pScissorTestEnable->Release();
  94. }
  95. // Called by Rocket when it wants to render geometry that it does not wish to optimise.
  96. void RenderInterfaceDirectX10::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))
  97. {
  98. // We've chosen to not support non-compiled geometry in the DirectX renderer. If you wanted to render non-compiled
  99. // geometry, for example for very small sections of geometry, you could use DrawIndexedPrimitiveUP or write to a
  100. // dynamic vertex buffer which is flushed when either the texture changes or compiled geometry is drawn.
  101. }
  102. // Called by Rocket when it wants to compile geometry it believes will be static for the forseeable future.
  103. Rocket::Core::CompiledGeometryHandle RenderInterfaceDirectX10::CompileGeometry(Rocket::Core::Vertex* vertices, int num_vertices, int* indices, int num_indices, Rocket::Core::TextureHandle texture)
  104. {
  105. //Create instance of geometry
  106. RocketD310DCompiledGeometry * geometry =new RocketD310DCompiledGeometry();
  107. //Vertex Buffer description
  108. D3D10_BUFFER_DESC bd;
  109. bd.Usage = D3D10_USAGE_DEFAULT;
  110. //Set the size of the buffer
  111. bd.ByteWidth = sizeof(RocketD3D10Vertex) * num_vertices;
  112. //This is a vertex buffer
  113. bd.BindFlags = D3D10_BIND_VERTEX_BUFFER;
  114. bd.CPUAccessFlags = 0;
  115. bd.MiscFlags = 0;
  116. //copy vertices into buffer
  117. RocketD3D10Vertex * pD3D10Vertices=new RocketD3D10Vertex[num_vertices];
  118. for (int i=0;i<num_vertices;++i)
  119. {
  120. pD3D10Vertices[i].x = vertices[i].position.x;
  121. pD3D10Vertices[i].y = vertices[i].position.y;
  122. pD3D10Vertices[i].z = 0;
  123. pD3D10Vertices[i].colour=D3DXCOLOR((float)(vertices[i].colour.red/255), (float)(vertices[i].colour.green/255), (float)(vertices[i].colour.blue/255),
  124. (float)(vertices[i].colour.alpha/255));
  125. pD3D10Vertices[i].u = vertices[i].tex_coord[0];
  126. pD3D10Vertices[i].v = vertices[i].tex_coord[1];
  127. }
  128. D3D10_SUBRESOURCE_DATA InitData;
  129. InitData.pSysMem = pD3D10Vertices;
  130. //Create VB
  131. if (FAILED(m_pD3D10Device->CreateBuffer(
  132. &bd,
  133. &InitData,
  134. &geometry->vertices )))
  135. return false;
  136. delete pD3D10Vertices;
  137. //Index buffer desc
  138. bd.Usage = D3D10_USAGE_DEFAULT;
  139. bd.ByteWidth = sizeof( UINT ) * num_indices;
  140. bd.BindFlags = D3D10_BIND_INDEX_BUFFER;
  141. bd.CPUAccessFlags = 0;
  142. bd.MiscFlags = 0;
  143. //Index values
  144. InitData.pSysMem = indices;
  145. //Fill and create buffer
  146. if (FAILED(m_pD3D10Device->CreateBuffer(
  147. &bd,
  148. &InitData,
  149. &geometry->indices )))
  150. return false;
  151. //save some info in the instance of the structure
  152. geometry->num_vertices = (DWORD) num_vertices;
  153. geometry->num_primitives = (DWORD) num_indices / 3;
  154. geometry->texture = texture == NULL ? NULL : (RocketD3D10Texture *) texture;
  155. return (Rocket::Core::CompiledGeometryHandle)geometry;
  156. }
  157. // Called by Rocket when it wants to render application-compiled geometry.
  158. void RenderInterfaceDirectX10::RenderCompiledGeometry(Rocket::Core::CompiledGeometryHandle geometry, const Rocket::Core::Vector2f& translation)
  159. {
  160. //Cast to D3D10 geometry
  161. RocketD310DCompiledGeometry* d3d10_geometry = (RocketD310DCompiledGeometry*) geometry;
  162. //if we have a texture then send it, notice we are sending the view(Shader resource) to the
  163. //effect
  164. if (d3d10_geometry->texture)
  165. m_pDiffuseTextureVariable->SetResource(d3d10_geometry->texture->textureView);
  166. else
  167. m_pDiffuseTextureVariable->SetResource(NULL);
  168. //build and send the world matrix
  169. D3DXMatrixTranslation(&m_matWorld, translation.x, translation.y, 0);
  170. m_pWorldMatrixVariable->SetMatrix((float*)m_matWorld);
  171. //Set the layout of the vertices that are held in the VB
  172. m_pD3D10Device->IASetInputLayout(m_pVertexLayout);
  173. //Get the stride(size) of the a vertex, we need this to tell the pipeline the size of one vertex
  174. UINT stride = sizeof(RocketD3D10Vertex);
  175. //The offset from start of the buffer to where our vertices are located
  176. UINT offset = 0;
  177. //Set the VB we are using
  178. m_pD3D10Device->IASetVertexBuffers(
  179. 0,
  180. 1,
  181. &d3d10_geometry->vertices,
  182. &stride,
  183. &offset );
  184. //Set the IB we are using
  185. m_pD3D10Device->IASetIndexBuffer(d3d10_geometry->indices,DXGI_FORMAT_R32_UINT,0);
  186. D3D10_TECHNIQUE_DESC techDesc;
  187. m_pTechnique->GetDesc( &techDesc );
  188. //Loop through the passes in the technique
  189. for( UINT p = 0; p < techDesc.Passes; ++p )
  190. {
  191. //Get a pass at current index and apply it
  192. m_pTechnique->GetPassByIndex( p )->Apply( 0 );
  193. //We are drawing trangle lists
  194. m_pD3D10Device->IASetPrimitiveTopology( D3D10_PRIMITIVE_TOPOLOGY_TRIANGLELIST );
  195. //Draw call
  196. m_pD3D10Device->DrawIndexed(d3d10_geometry->num_primitives*3,0,0);
  197. }
  198. }
  199. // Called by Rocket when it wants to release application-compiled geometry.
  200. void RenderInterfaceDirectX10::ReleaseCompiledGeometry(Rocket::Core::CompiledGeometryHandle geometry)
  201. {
  202. //Clean up after ourselves
  203. RocketD310DCompiledGeometry* d3d10_geometry=(RocketD310DCompiledGeometry*)geometry;
  204. d3d10_geometry->vertices->Release();
  205. d3d10_geometry->indices->Release();
  206. delete d3d10_geometry;
  207. }
  208. // Called by Rocket when it wants to enable or disable scissoring to clip content.
  209. void RenderInterfaceDirectX10::EnableScissorRegion(bool enable)
  210. {
  211. //Is the scissor test enabled?
  212. enable ? m_pD3D10Device->RSSetState(m_pScissorTestEnable) : m_pD3D10Device->RSSetState(m_pScissorTestDisable);
  213. }
  214. // Called by Rocket when it wants to change the scissor region.
  215. void RenderInterfaceDirectX10::SetScissorRegion(int x, int y, int width, int height)
  216. {
  217. //The scissor rect
  218. D3D10_RECT rect;
  219. rect.left=x;
  220. rect.right=x+width;
  221. rect.top=y;
  222. rect.bottom=y+height;
  223. m_pD3D10Device->RSSetScissorRects(1,&rect);
  224. }
  225. // Set to byte packing, or the compiler will expand our struct, which means it won't read correctly from file
  226. #pragma pack(1)
  227. struct TGAHeader
  228. {
  229. char idLength;
  230. char colourMapType;
  231. char dataType;
  232. short int colourMapOrigin;
  233. short int colourMapLength;
  234. char colourMapDepth;
  235. short int xOrigin;
  236. short int yOrigin;
  237. short int width;
  238. short int height;
  239. char bitsPerPixel;
  240. char imageDescriptor;
  241. };
  242. // Restore packing
  243. #pragma pack()
  244. // Called by Rocket when a texture is required by the library.
  245. bool RenderInterfaceDirectX10::LoadTexture(Rocket::Core::TextureHandle& texture_handle, Rocket::Core::Vector2i& texture_dimensions, const Rocket::Core::String& source)
  246. {
  247. Rocket::Core::FileInterface* file_interface = Rocket::Core::GetFileInterface();
  248. Rocket::Core::FileHandle file_handle = file_interface->Open(source);
  249. if (file_handle == NULL)
  250. return false;
  251. file_interface->Seek(file_handle, 0, SEEK_END);
  252. size_t buffer_size = file_interface->Tell(file_handle);
  253. file_interface->Seek(file_handle, 0, SEEK_SET);
  254. char* buffer = new char[buffer_size];
  255. file_interface->Read(buffer, buffer_size, file_handle);
  256. file_interface->Close(file_handle);
  257. TGAHeader header;
  258. memcpy(&header, buffer, sizeof(TGAHeader));
  259. int color_mode = header.bitsPerPixel / 8;
  260. int image_size = header.width * header.height * 4; // We always make 32bit textures
  261. if (header.dataType != 2)
  262. {
  263. Rocket::Core::Log::Message(Rocket::Core::Log::LT_ERROR, "Only 24/32bit uncompressed TGAs are supported.");
  264. return false;
  265. }
  266. // Ensure we have at least 3 colors
  267. if (color_mode < 3)
  268. {
  269. Rocket::Core::Log::Message(Rocket::Core::Log::LT_ERROR, "Only 24 and 32bit textures are supported");
  270. return false;
  271. }
  272. const char* image_src = buffer + sizeof(TGAHeader);
  273. unsigned char* image_dest = new unsigned char[image_size];
  274. // Targa is BGR, swap to RGB and flip Y axis
  275. for (long y = 0; y < header.height; y++)
  276. {
  277. long read_index = y * header.width * color_mode;
  278. long write_index = ((header.imageDescriptor & 32) != 0) ? read_index : (header.height - y - 1) * header.width * color_mode;
  279. for (long x = 0; x < header.width; x++)
  280. {
  281. image_dest[write_index] = image_src[read_index+2];
  282. image_dest[write_index+1] = image_src[read_index+1];
  283. image_dest[write_index+2] = image_src[read_index];
  284. if (color_mode == 4)
  285. image_dest[write_index+3] = image_src[read_index+3];
  286. else
  287. image_dest[write_index+3] = 255;
  288. write_index += 4;
  289. read_index += color_mode;
  290. }
  291. }
  292. texture_dimensions.x = header.width;
  293. texture_dimensions.y = header.height;
  294. bool success = GenerateTexture(texture_handle, image_dest, texture_dimensions);
  295. delete [] image_dest;
  296. delete [] buffer;
  297. return success;
  298. }
  299. // Called by Rocket when a texture is required to be built from an internally-generated sequence of pixels.
  300. bool RenderInterfaceDirectX10::GenerateTexture(Rocket::Core::TextureHandle& texture_handle, const byte* source, const Rocket::Core::Vector2i& source_dimensions)
  301. {
  302. //Create the instance of our texture
  303. RocketD3D10Texture * pTexture=new RocketD3D10Texture();
  304. //Texture description
  305. D3D10_TEXTURE2D_DESC textureDesc;
  306. //Width and height of the texture
  307. textureDesc.Width=source_dimensions.x;
  308. textureDesc.Height=source_dimensions.y;
  309. //Mip levels
  310. textureDesc.MipLevels=1;
  311. textureDesc.ArraySize = 1;
  312. //the format of the texture
  313. textureDesc.Format=DXGI_FORMAT_R8G8B8A8_UNORM;
  314. //The access and usage of the texture
  315. textureDesc.CPUAccessFlags=D3D10_CPU_ACCESS_WRITE;
  316. textureDesc.Usage=D3D10_USAGE_DYNAMIC;
  317. //Our are we going to bind this texture to the pipeline
  318. textureDesc.BindFlags= D3D10_BIND_SHADER_RESOURCE;
  319. textureDesc.MiscFlags=D3D10_RESOURCE_MISC_SHARED;
  320. textureDesc.SampleDesc.Count=1;
  321. textureDesc.SampleDesc.Quality=0;
  322. //create our texture
  323. if (FAILED(m_pD3D10Device->CreateTexture2D(&textureDesc,NULL,&pTexture->texture2D))){
  324. Rocket::Core::Log::Message(Rocket::Core::Log::LT_ERROR, "Unable to create texture");
  325. return false;
  326. }
  327. //now lets fill it
  328. D3D10_MAPPED_TEXTURE2D mappedTex;
  329. pTexture->texture2D->Map(D3D10CalcSubresource(0,0,1),D3D10_MAP_WRITE_DISCARD,0,&mappedTex);
  330. for (int y = 0; y < source_dimensions.y; ++y)
  331. {
  332. for (int x = 0; x < source_dimensions.x; ++x)
  333. {
  334. const byte* source_pixel = source + (source_dimensions.x * 4 * y) + (x * 4);
  335. byte* destination_pixel = ((byte*) mappedTex.pData) + mappedTex.RowPitch * y + x * 4;
  336. destination_pixel[0] = source_pixel[0];
  337. destination_pixel[1] = source_pixel[1];
  338. destination_pixel[2] = source_pixel[2];
  339. destination_pixel[3] = source_pixel[3];
  340. }
  341. }
  342. pTexture->texture2D->Unmap(D3D10CalcSubresource(0,0,1));
  343. //Create the shader resoure view for our texture, we need this
  344. //to send the texture to the effect
  345. D3D10_SHADER_RESOURCE_VIEW_DESC srvDesc;
  346. srvDesc.Format=textureDesc.Format;
  347. srvDesc.ViewDimension=D3D10_SRV_DIMENSION_TEXTURE2D;
  348. srvDesc.Texture2D.MipLevels=textureDesc.MipLevels;
  349. srvDesc.Texture2D.MostDetailedMip=0;
  350. if (FAILED(m_pD3D10Device->CreateShaderResourceView(pTexture->texture2D,&srvDesc,&pTexture->textureView)))
  351. return false;
  352. texture_handle = (Rocket::Core::TextureHandle)pTexture;
  353. return true;
  354. }
  355. // Called by Rocket when a loaded texture is no longer required.
  356. void RenderInterfaceDirectX10::ReleaseTexture(Rocket::Core::TextureHandle texture_handle)
  357. {
  358. //clean up after ourselves
  359. RocketD3D10Texture * pTexture=(RocketD3D10Texture*)texture_handle;
  360. pTexture->texture2D->Release();
  361. pTexture->textureView->Release();
  362. delete pTexture;
  363. }
  364. // Returns the native horizontal texel offset for the renderer.
  365. float RenderInterfaceDirectX10::GetHorizontalTexelOffset()
  366. {
  367. return 0.0f;
  368. }
  369. // Returns the native vertical texel offset for the renderer.
  370. float RenderInterfaceDirectX10::GetVerticalTexelOffset()
  371. {
  372. return 0.0f;
  373. }