MeshResourceData* MeshLoader_dfmesh(ResourceDataSource &dataSource, Allocator &alloc) { DFMeshHeader header; dataSource.getObjects(&header, 1); if (strncmp((const char *)&header.magic, "DFME", 4) != 0) { DFLOG_WARN("Invalid dfmesh magic"); return nullptr; } if (header.version != DFMESH_VERSION) { DFLOG_WARN("Unsupported dfmesh version %d", header.version); return nullptr; } // FIXME: quad.mesh HACK if (header.vertexFormat == 0) DF3D_ASSERT(header.indexSize == sizeof(uint16_t)); // TODO: vertex format is hardcoded. auto vf = VertexFormat_dfmesh(header.vertexFormat); auto result = MAKE_NEW(alloc, MeshResourceData)(); dataSource.seek(header.submeshesOffset, SeekDir::BEGIN); for (int i = 0; i < header.submeshesCount; i++) { DFMeshSubmeshHeader smHeader; dataSource.getObjects(&smHeader, 1); const size_t verticesCount = smHeader.vertexDataSizeInBytes / vf.getVertexSize(); const size_t indicesCount = smHeader.indexDataSizeInBytes / header.indexSize; auto meshPart = MAKE_NEW(alloc, MeshResourceData::Part)(vf, alloc); meshPart->vertexData.addVertices(verticesCount); dataSource.getObjects((uint8_t*)meshPart->vertexData.getRawData(), meshPart->vertexData.getSizeInBytes()); meshPart->indexData.resize(indicesCount); dataSource.getObjects(meshPart->indexData.data(), indicesCount); meshPart->materialName = smHeader.materialId; result->parts.push_back(meshPart); } return result; }
void InitContent() { g_mesh = theResourceFactory.LoadMesh("Models/sphere.obj"); g_object = MAKE_NEW(MeshObject); g_object->Init(g_mesh, theResourceFactory.GetDefaultMaterial()); Gaag.RegisterObject(g_object); //g_cubemap = theResourceFactory.LoadTexture("Textures/cubemap.dds"); g_directional_light = theResourceFactory.MakeDirectionalLight(float3(1, -1, 1), float4(1, 1, 1, 1), 0); Gaag.RegisterLight(g_directional_light); g_camera_controller = MAKE_NEW(CameraController); g_camera_controller->SetTargetCamera(Gaag.GetCamera()); }
*/ DEVICE_CMD Accept_Socket(REBREQ *sock) /* ** Accept an inbound connection on a TCP listen socket. ** ** The function will return: ** =0: succeeded ** >0: in-progress, still trying ** <0: error occurred, no longer trying ** ** Before usage: ** Open_Socket(); ** Set local_port to desired port number. ** Listen_Socket(); ** ***********************************************************************/ { SOCKAI sa; REBREQ *news; int len = sizeof(sa); int result; extern void Attach_Request(REBREQ **prior, REBREQ *req); // Accept a new socket, if there is one: result = accept(sock->socket, (struct sockaddr *)&sa, &len); if (result == BAD_SOCKET) { result = GET_ERROR; if (result == NE_WOULDBLOCK) return DR_PEND; sock->error = result; //Signal_Device(sock, EVT_ERROR); return DR_ERROR; } // To report the new socket, the code here creates a temporary // request and copies the listen request to it. Then, it stores // the new values for IP and ports and links this request to the // original via the sock->data. news = MAKE_NEW(*news); // Be sure to deallocate it CLEARS(news); // *news = *sock; news->device = sock->device; SET_OPEN(news); SET_FLAG(news->state, RSM_OPEN); SET_FLAG(news->state, RSM_CONNECT); news->socket = result; news->net.remote_ip = sa.sin_addr.s_addr; //htonl(ip); NOTE: REBOL stays in network byte order news->net.remote_port = ntohs(sa.sin_port); Get_Local_IP(news); //Nonblocking_Mode(news->socket); ???Needed? Attach_Request((REBREQ**)&sock->data, news); Signal_Device(sock, EVT_ACCEPT); // Even though we signalled, we keep the listen pending to // accept additional connections. return DR_PEND; }
REBCNT Test_Async_Callback(REBSER *obj, REBCNT word) { RXICBI *cbi; RXIARG *args; REBCNT n; // These cannot be on the stack, because they are used // when the callback happens later. cbi = MAKE_NEW(*cbi); CLEAR(cbi, sizeof(cbi)); args = MAKE_MEM(sizeof(RXIARG) * 4); CLEAR(args, sizeof(RXIARG) * 4); cbi->obj = obj; cbi->word = word; cbi->args = args; SET_FLAG(cbi->flags, RXC_ASYNC); // Pass a single integer arg to the callback function: RXI_COUNT(args) = 1; RXI_TYPE(args, 1) = RXT_INTEGER; args[1].int64 = 123; n = RL_CALLBACK(cbi); // result is in cbi struct, if wanted return n; }
TextureResourceData* TextureLoader_stbi(ResourceDataSource &dataSource, Allocator &alloc, bool forceRGBA) { stbi_io_callbacks callbacks; callbacks.read = read; callbacks.skip = skip; callbacks.eof = eof; int x, y, bpp; auto pixels = stbi_load_from_callbacks(&callbacks, &dataSource, &x, &y, &bpp, forceRGBA ? 4 : 0); if (!pixels) { #ifdef STB_DO_ERROR_PRINT DFLOG_WARN(stbi_failure_reason()); #endif return nullptr; } auto fmt = PixelFormat::INVALID; if (bpp == STBI_rgb) { fmt = PixelFormat::RGB; } else if (bpp == STBI_rgb_alpha) { fmt = PixelFormat::RGBA; } else { DFLOG_WARN("Parsed image with an invalid bpp"); stbi_image_free(pixels); return nullptr; } if (forceRGBA) fmt = PixelFormat::RGBA; auto resource = MAKE_NEW(alloc, TextureResourceData); resource->format = fmt; resource->mipLevels.resize(1); resource->mipLevels[0].width = x; resource->mipLevels[0].height = y; int compCount = forceRGBA ? 4 : bpp; resource->mipLevels[0].pixels.resize(compCount * x * y); memcpy(resource->mipLevels[0].pixels.data(), pixels, compCount * x * y); stbi_image_free(pixels); return resource; }
void RenderContext::Init(pWindow inWindow) { CleanUp(); HWND hWnd = inWindow->mHWnd; D3D_DRIVER_TYPE driver_type = D3D_DRIVER_TYPE_NULL; D3D_FEATURE_LEVEL feature_level = D3D_FEATURE_LEVEL_11_0; HRESULT hr = S_OK; RECT rc; GetClientRect(hWnd, &rc); mWidth = rc.right - rc.left; mHeight = rc.bottom - rc.top; UINT createDeviceFlags = 0; #ifdef _DEBUG createDeviceFlags |= D3D11_CREATE_DEVICE_DEBUG; createDeviceFlags |= D3D11_CREATE_DEVICE_DISABLE_GPU_TIMEOUT; #endif D3D_DRIVER_TYPE driverTypes[] = { D3D_DRIVER_TYPE_HARDWARE, D3D_DRIVER_TYPE_WARP, D3D_DRIVER_TYPE_REFERENCE, }; UINT numDriverTypes = ARRAYSIZE(driverTypes); D3D_FEATURE_LEVEL featureLevels[] = { D3D_FEATURE_LEVEL_11_1, D3D_FEATURE_LEVEL_11_0, D3D_FEATURE_LEVEL_10_1, D3D_FEATURE_LEVEL_10_0, }; UINT numFeatureLevels = ARRAYSIZE(featureLevels); for (UINT driverTypeIndex = 0; driverTypeIndex < numDriverTypes; driverTypeIndex++) { driver_type = driverTypes[driverTypeIndex]; hr = D3D11CreateDevice(nullptr, driver_type, nullptr, createDeviceFlags, featureLevels, numFeatureLevels, D3D11_SDK_VERSION, &mD3DDevice, &feature_level, &mImmediateContext); if (hr == E_INVALIDARG) { hr = D3D11CreateDevice(nullptr, driver_type, nullptr, createDeviceFlags, &featureLevels[1], numFeatureLevels - 1, D3D11_SDK_VERSION, &mD3DDevice, &feature_level, &mImmediateContext); } if (SUCCEEDED(hr)) break; } if (FAILED(hr)) { exit(hr); } // Obtain DXGI factory from device (since we used nullptr for pAdapter above) IDXGIFactory1* dxgiFactory = nullptr; { IDXGIDevice* dxgiDevice = nullptr; hr = mD3DDevice->QueryInterface(__uuidof(IDXGIDevice), reinterpret_cast<void**>(&dxgiDevice)); if (SUCCEEDED(hr)) { IDXGIAdapter* adapter = nullptr; hr = dxgiDevice->GetAdapter(&adapter); if (SUCCEEDED(hr)) { hr = adapter->GetParent(__uuidof(IDXGIFactory1), reinterpret_cast<void**>(&dxgiFactory)); adapter->Release(); } dxgiDevice->Release(); } } if (FAILED(hr)) { exit(hr); } // Create swap chain IDXGIFactory2* dxgiFactory2 = nullptr; hr = dxgiFactory->QueryInterface(__uuidof(IDXGIFactory2), reinterpret_cast<void**>(&dxgiFactory2)); if (dxgiFactory2) { // DirectX 11.1 or later hr = mD3DDevice->QueryInterface(__uuidof(ID3D11Device1), reinterpret_cast<void**>(&mD3DDevice1)); if (SUCCEEDED(hr)) { (void)mImmediateContext->QueryInterface(__uuidof(ID3D11DeviceContext1), reinterpret_cast<void**>(&mImmediateContext1)); } DXGI_SWAP_CHAIN_DESC1 sd; ZeroMemory(&sd, sizeof(sd)); sd.Width = mWidth; sd.Height = mHeight; sd.Format = DXGI_FORMAT_R8G8B8A8_UNORM; sd.SampleDesc.Count = 1; sd.SampleDesc.Quality = 0; sd.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT | DXGI_USAGE_SHADER_INPUT | DXGI_USAGE_UNORDERED_ACCESS; sd.BufferCount = 1; hr = dxgiFactory2->CreateSwapChainForHwnd(mD3DDevice, hWnd, &sd, nullptr, nullptr, &mSwapChain1); if (SUCCEEDED(hr)) { hr = mSwapChain1->QueryInterface(__uuidof(IDXGISwapChain), reinterpret_cast<void**>(&mSwapChain)); } dxgiFactory2->Release(); } else { // DirectX 11.0 systems DXGI_SWAP_CHAIN_DESC sd; ZeroMemory(&sd, sizeof(sd)); sd.BufferCount = 1; sd.BufferDesc.Width = mWidth; sd.BufferDesc.Height = mHeight; sd.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM; sd.BufferDesc.RefreshRate.Numerator = 60; sd.BufferDesc.RefreshRate.Denominator = 1; sd.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT | DXGI_USAGE_SHADER_INPUT | DXGI_USAGE_UNORDERED_ACCESS; sd.OutputWindow = hWnd; sd.SampleDesc.Count = 1; sd.SampleDesc.Quality = 0; sd.Windowed = TRUE; hr = dxgiFactory->CreateSwapChain(mD3DDevice, &sd, &mSwapChain); } // Note this tutorial doesn't handle full-screen swapchains so we block the ALT+ENTER shortcut dxgiFactory->MakeWindowAssociation(hWnd, DXGI_MWA_NO_ALT_ENTER); dxgiFactory->Release(); if (FAILED(hr)) { exit(hr); } // Create a render target view ID3D11Texture2D* pBackBuffer = nullptr; hr = mSwapChain->GetBuffer(0, __uuidof(ID3D11Texture2D), reinterpret_cast<void**>(&pBackBuffer)); if (FAILED(hr)) { exit(hr); } mBackBuffer = MAKE_NEW(Texture); mBackBuffer->Init(pBackBuffer); mOutputRenderTarget = MAKE_NEW(RenderTarget); mOutputRenderTarget->Init(mBackBuffer); // setup rasterizer SetRasterizerState(FILL_SOLID, CULL_BACK, true, 0, 0.0f, 0.0f, true, false, false, false); D3D11_DEPTH_STENCIL_DESC dsDesc; dsDesc.DepthEnable = true; dsDesc.DepthWriteMask = D3D11_DEPTH_WRITE_MASK_ALL; dsDesc.DepthFunc = D3D11_COMPARISON_LESS; dsDesc.StencilEnable = false; dsDesc.StencilReadMask = 0xFF; dsDesc.StencilWriteMask = 0xFF; dsDesc.FrontFace.StencilFailOp = D3D11_STENCIL_OP_KEEP; dsDesc.FrontFace.StencilDepthFailOp = D3D11_STENCIL_OP_INCR; dsDesc.FrontFace.StencilPassOp = D3D11_STENCIL_OP_KEEP; dsDesc.FrontFace.StencilFunc = D3D11_COMPARISON_ALWAYS; // Stencil operations if pixel is back-facing dsDesc.BackFace.StencilFailOp = D3D11_STENCIL_OP_KEEP; dsDesc.BackFace.StencilDepthFailOp = D3D11_STENCIL_OP_DECR; dsDesc.BackFace.StencilPassOp = D3D11_STENCIL_OP_KEEP; dsDesc.BackFace.StencilFunc = D3D11_COMPARISON_ALWAYS; // Create depth stencil state ID3D11DepthStencilState * pDSState; mD3DDevice->CreateDepthStencilState(&dsDesc, &pDSState); mImmediateContext->OMSetDepthStencilState(pDSState, 1); // Setup the viewport SetViewport(int2(mWidth, mHeight), 0.0f, 1.0f, int2(0, 0)); hr = mImmediateContext->QueryInterface(__uuidof(mAnnotation), reinterpret_cast<void**>(&mAnnotation)); if (FAILED(hr)) { exit(hr); } mInitialized = true; }