Пример #1
0
DXGI_FORMAT PixelBuffer::GetUAVFormat( DXGI_FORMAT defaultFormat )
{
	switch (defaultFormat)
	{
	case DXGI_FORMAT_R8G8B8A8_TYPELESS:
	case DXGI_FORMAT_R8G8B8A8_UNORM:
	case DXGI_FORMAT_R8G8B8A8_UNORM_SRGB:
		return DXGI_FORMAT_R8G8B8A8_UNORM;

	case DXGI_FORMAT_B8G8R8A8_TYPELESS:
	case DXGI_FORMAT_B8G8R8A8_UNORM:
	case DXGI_FORMAT_B8G8R8A8_UNORM_SRGB:
		return DXGI_FORMAT_B8G8R8A8_UNORM;

	case DXGI_FORMAT_B8G8R8X8_TYPELESS:
	case DXGI_FORMAT_B8G8R8X8_UNORM:
	case DXGI_FORMAT_B8G8R8X8_UNORM_SRGB:
		return DXGI_FORMAT_B8G8R8X8_UNORM;

	case DXGI_FORMAT_R32_TYPELESS:
	case DXGI_FORMAT_R32_FLOAT:
		return DXGI_FORMAT_R32_FLOAT;

#ifdef _DEBUG
	case DXGI_FORMAT_R32G8X24_TYPELESS:
	case DXGI_FORMAT_D32_FLOAT_S8X24_UINT:
	case DXGI_FORMAT_R32_FLOAT_X8X24_TYPELESS:
	case DXGI_FORMAT_X32_TYPELESS_G8X24_UINT:
	case DXGI_FORMAT_D32_FLOAT:
	case DXGI_FORMAT_R24G8_TYPELESS:
	case DXGI_FORMAT_D24_UNORM_S8_UINT:
	case DXGI_FORMAT_R24_UNORM_X8_TYPELESS:
	case DXGI_FORMAT_X24_TYPELESS_G8_UINT:
	case DXGI_FORMAT_D16_UNORM:

		PRINTWARN( "Requested a UAV format for a depth stencil format." );
#endif

	default:
		return defaultFormat;
	}
}
bool loadParams(int argc, char ** argv, std::string& worldFilename, std::string& robotFilename,
                std::string& objectFilename, std::string& outputDirectory, bool& saveSeparate, Eigen::Vector3d& objPos,
                int& maxIterations)
{
    saveSeparate = false;
    worldFilename.clear();
    robotFilename.clear();
    objectFilename.clear();
    outputDirectory.clear();

    boost::program_options::variables_map vm;
    try
    {
        vm = loadParams(argc, argv);
    }
    catch (std::exception const& e)
    {
        PRINTERROR("Exception caught: " << e.what());
        return false;
    }
    catch (...)
    {
        PRINTERROR("Exception caught");
        return false;
    }

    boost::program_options::options_description desc = getOptions();
    // desc=getOptions();

    if (vm.count("help"))
    {
        PRINTMSG(desc);
        return false;
    }

    if (vm.count("dir") < 1)
    {
        PRINTERROR("Must specify an output directory");
        PRINTMSG(desc);
        return false;
    }

    if (vm.count("wld") && (vm.count("rob") || vm.count("obj")))
    {
        PRINTERROR("Cannot specify a world and a robot and/or object at the same time.");
        PRINTMSG(desc);
        return false;
    }

    if (!vm.count("wld") && !vm.count("rob"))
    {
        PRINTERROR("Have to specify either a robot or a world.");
        PRINTMSG(desc);
        return false;
    }

    if (vm.count("rob") != vm.count("obj"))
    {
        PRINTERROR("If you specify a robot, you also have to specify an object, and vice versa.");
        PRINTMSG(desc);
        return false;
    }


    if (vm.count("rob") > 1)
    {
        PRINTERROR("You can only specify one robot at this stage.");
        PRINTMSG(desc);
        return false;
    }

    if (vm.count("obj") > 1)
    {
        PRINTERROR("You can only specify one object at this stage.");
        PRINTMSG(desc);
        return false;
    }

    if (vm.count("obj") != vm.count("rob"))
    {
        PRINTERROR("If you specify a robot, you should also specify an object.");
        PRINTMSG(desc);
        return false;
    }

    if (vm.count("wld"))
    {
        worldFilename = vm["wld"].as<std::string>();
        PRINTMSG("World file is " << worldFilename);
    }
    if (vm.count("rob"))
    {
        robotFilename = vm["rob"].as<std::string>();
        PRINTMSG("Robot file is " << robotFilename);
    }
    if (vm.count("obj"))
    {
        objectFilename = vm["obj"].as<std::string>();
        PRINTMSG("Object file is " << objectFilename);
    }
    if (vm.count("dir"))
    {
        outputDirectory = vm["dir"].as<std::string>();
        PRINTMSG("Output dir is " << outputDirectory);
    }

    if (vm.count("iter"))
    {
        maxIterations = vm["iter"].as<int>();
        PRINTMSG("Number of iterations: " << maxIterations);
        if (maxIterations < 35000)
        {
            PRINTWARN("Planning is not working well with max iterations < 35000");
        }
    }


    if (vm.count("obj-pos"))
    {
        std::vector<float> vals=vm["obj-pos"].as<std::vector<float> >();
        if (vals.size()!=3)
        {
            PRINTERROR("Must specify 3 values for --obj-pos: x, y and z (specified "<<vals.size()<<")");
            PRINTMSG(desc);
        }
        PRINTMSG("Using initial object pose "<<vals[0]<<", "<<vals[1]<<", "<<vals[2]);
        objPos=Eigen::Vector3d(vals[0],vals[1],vals[2]);
    }

    if (vm.count("save-separate"))
    {
        saveSeparate=true;
    }

    return true;
}
// Load the rendering pipeline dependencies.
HRESULT VolumetricAnimation::LoadPipeline()
{
	HRESULT hr;

	// [TODO]: Move to project independent framework
#ifdef _DEBUG
	// Enable the D3D12 debug layer.
	ComPtr<ID3D12Debug> debugController;
	if ( SUCCEEDED( D3D12GetDebugInterface( IID_PPV_ARGS( &debugController ) ) ) )
	{
		debugController->EnableDebugLayer();
	}else
		PRINTWARN(L"Unable to enable D3D12 debug validation layer.")
#endif

	ComPtr<IDXGIFactory4> factory;
	VRET( CreateDXGIFactory1( IID_PPV_ARGS( &factory ) ) );

	if ( m_useWarpDevice )
	{
		ComPtr<IDXGIAdapter> warpAdapter;
		VRET( factory->EnumWarpAdapter( IID_PPV_ARGS( &warpAdapter ) ) );
		VRET( D3D12CreateDevice( warpAdapter.Get(), D3D_FEATURE_LEVEL_11_0, IID_PPV_ARGS( &m_device ) ) );
	}
	else
	{
		ComPtr<IDXGIAdapter1> hardwareAdapter;
		GetHardwareAdapter( factory.Get(), &hardwareAdapter );
		VRET( D3D12CreateDevice( hardwareAdapter.Get(), D3D_FEATURE_LEVEL_11_0, IID_PPV_ARGS( &m_device ) ) );
	}

	// [TODO]: Move to project independent framework
	// Check Direct3D 12 feature hardware support (more usage refer Direct3D 12 sdk Capability Querying)
	D3D12_FEATURE_DATA_D3D12_OPTIONS options;
	m_device->CheckFeatureSupport( D3D12_FEATURE_D3D12_OPTIONS, &options, sizeof( options ) );
	switch ( options.ResourceBindingTier )
	{
	case D3D12_RESOURCE_BINDING_TIER_1:
		PRINTWARN( L"Tier 1 is supported." );
		break;
	case D3D12_RESOURCE_BINDING_TIER_2:
		PRINTWARN( L"Tier 1 and 2 are supported." );
		break;
	case D3D12_RESOURCE_BINDING_TIER_3:
		PRINTWARN( L"Tier 1, 2 and 3 are supported." );
		break;
	default:
		break;
	}

	// Describe and create the graphics command queue.
	D3D12_COMMAND_QUEUE_DESC queueDesc = {};
	queueDesc.Flags = D3D12_COMMAND_QUEUE_FLAG_NONE;
	queueDesc.Type = D3D12_COMMAND_LIST_TYPE_DIRECT;
	VRET( m_device->CreateCommandQueue( &queueDesc, IID_PPV_ARGS( &m_graphicCmdQueue ) ) );
	DXDebugName( m_graphicCmdQueue );

	// Describe and create the compute command queue;
	queueDesc.Type = D3D12_COMMAND_LIST_TYPE_COMPUTE;
	VRET( m_device->CreateCommandQueue( &queueDesc, IID_PPV_ARGS( &m_computeCmdQueue ) ) );
	DXDebugName( m_computeCmdQueue );

	// Describe and create the swap chain.
	DXGI_SWAP_CHAIN_DESC swapChainDesc = {};
	swapChainDesc.BufferCount = FrameCount;
	swapChainDesc.BufferDesc.Width = m_width;
	swapChainDesc.BufferDesc.Height = m_height;
	swapChainDesc.BufferDesc.Format = DXGI_FORMAT_R16G16B16A16_FLOAT;
	swapChainDesc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
	swapChainDesc.SwapEffect = DXGI_SWAP_EFFECT_FLIP_DISCARD;
	swapChainDesc.OutputWindow = m_hwnd;
	swapChainDesc.SampleDesc.Count = 1;
	swapChainDesc.Windowed = TRUE;

	ComPtr<IDXGISwapChain> swapChain;
	// Swap chain needs the queue so that it can force a flush on it.
	VRET( factory->CreateSwapChain( m_graphicCmdQueue.Get(), &swapChainDesc, &swapChain ) );
	VRET( swapChain.As( &m_swapChain ) );
	DXDebugName( m_swapChain );

	// This sample does not support fullscreen transitions.
	VRET( factory->MakeWindowAssociation( m_hwnd, DXGI_MWA_NO_ALT_ENTER ) );

	m_frameIndex = m_swapChain->GetCurrentBackBufferIndex();

	// Create descriptor heaps.
	{
		// Describe and create a render target view (RTV) descriptor heap.
		D3D12_DESCRIPTOR_HEAP_DESC rtvHeapDesc = {};
		rtvHeapDesc.NumDescriptors = FrameCount;
		rtvHeapDesc.Type = D3D12_DESCRIPTOR_HEAP_TYPE_RTV;
		rtvHeapDesc.Flags = D3D12_DESCRIPTOR_HEAP_FLAG_NONE;
		VRET( m_device->CreateDescriptorHeap( &rtvHeapDesc, IID_PPV_ARGS( &m_rtvHeap ) ) );
		DXDebugName( m_rtvHeap );

		m_rtvDescriptorSize = m_device->GetDescriptorHandleIncrementSize( D3D12_DESCRIPTOR_HEAP_TYPE_RTV );

		// Describe and create a shader resource view (SRV) and constant buffer view (CBV) descriptor heap.
		// Flags indicate that this descriptor heap can be bound to the pipeline
		// and that descriptors contained in it can be reference by a root table
		D3D12_DESCRIPTOR_HEAP_DESC cbvsrvuavHeapDesc = {};
		cbvsrvuavHeapDesc.NumDescriptors = 3; // One for SRV two for CBV (gfx and compute)
		cbvsrvuavHeapDesc.Flags = D3D12_DESCRIPTOR_HEAP_FLAG_SHADER_VISIBLE;
		cbvsrvuavHeapDesc.Type = D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV;
		VRET( m_device->CreateDescriptorHeap( &cbvsrvuavHeapDesc, IID_PPV_ARGS( &m_cbvsrvuavHeap ) ) );
		DXDebugName( m_cbvsrvuavHeap );
		
		m_cbvsrvuavDescriptorSize = m_device->GetDescriptorHandleIncrementSize( D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV );

		// Describe and create a depth stencil view (DSV) descriptor heap.
		D3D12_DESCRIPTOR_HEAP_DESC dsvHeapDesc = {};
		dsvHeapDesc.NumDescriptors = 1;
		dsvHeapDesc.Type = D3D12_DESCRIPTOR_HEAP_TYPE_DSV;
		dsvHeapDesc.Flags = D3D12_DESCRIPTOR_HEAP_FLAG_NONE;
		VRET( m_device->CreateDescriptorHeap( &dsvHeapDesc, IID_PPV_ARGS( &m_dsvHeap ) ) );
		DXDebugName( m_dsvHeap );
	}

	VRET( m_device->CreateCommandAllocator( D3D12_COMMAND_LIST_TYPE_DIRECT, IID_PPV_ARGS( &m_graphicCmdAllocator ) ) );
	DXDebugName( m_graphicCmdAllocator );
	
	VRET( m_device->CreateCommandAllocator( D3D12_COMMAND_LIST_TYPE_COMPUTE, IID_PPV_ARGS( &m_computeCmdAllocator ) ) );
	DXDebugName( m_computeCmdAllocator );

	return S_OK;
}
int run(int argc, char **argv)
{
    if (argc < 3)
    {
        printHelp(argv[0]);
        return 0;
    }

    std::string output_path;
    if (argc >= 4)
    {
        output_path=std::string(argv[3]);    
        if (!makeDirectoryIfNeeded(output_path))
        {
            PRINTERROR("Could not create directory "<<output_path);
            return 0;
        }
    }
        
    if (output_path.empty())
    {
        PRINTWARN("No output path configured, will print results on screen only.");
        printHelp(argv[0]);
    }

    const std::string robotArg(argv[1]);
    const std::string objectArg(argv[2]);

    PRINTMSG("Planning for robot ID=" << robotArg << " to grasp object ID=" << objectArg);

    // TODO parameterize this
    std::string egPlanningTopic = "graspit_eg_planning";

    ros::NodeHandle n;
    ros::ServiceClient client = n.serviceClient<manipulation_msgs::GraspPlanning>(egPlanningTopic);

    // Should use a client here to query the database for information about the
    // object type. For now, object type information is not used in the planning request,
    // as the service looks up the object type itself. So we can leave these on arbitrary values.
    object_recognition_msgs::ObjectType dbModelType;
    dbModelType.key =  "NotAvailabeYet";
    dbModelType.db = "SimpleGraspItDatabase";

    // Here we can set a different pose to put the object at in the current
    // graspit world. If this the reference frame is "0",
    // it uses the object's current pose in the world. If it is "1",
    // we will use the pose specified in the following field.
    geometry_msgs::PoseStamped modelPose;
    modelPose.header.frame_id = "0";

    /*    modelPose.header.frame_id="1";
          modelPose.pose.orientation.w=1;
          modelPose.pose.position.x=100;
     */

    household_objects_database_msgs::DatabaseModelPose dbModel;
    dbModel.model_id = atoi(objectArg.c_str());  // TODO move away from atoi at some stage
    dbModel.type = dbModelType;
    dbModel.pose = modelPose;
    dbModel.confidence = 1;
    dbModel.detector_name = "manual_detection";


    manipulation_msgs::GraspableObject obj;
    // the reference frame could be one that is relative to all fields (e.g. cluster and
    // all potential models). However at the moment, the graspit planner only supports
    // the global frame (the graspit origin). No tf transforms are considered in the
    // GraspIt planner service yet.
    obj.reference_frame_id = dbModel.pose.header.frame_id;
    obj.potential_models.push_back(dbModel);
    // obj.cluster = we will not provide a point cloud
    // obj.region = and not the SceneRegion along with it either.
    // obj.collision_name = could think about whether providing this as parameter too

    manipulation_msgs::GraspPlanning srv;
    srv.request.arm_name = robotArg;
    srv.request.target = obj;
    srv.request.collision_object_name = obj.collision_name;
    // srv.request.collision_support_surface_name = will not provide this here
    // srv.request.grasps_to_evaluate = no grasps to evaluate with this client
    // srv.request.movable_obstacles = this is not supported by this client

    if (!client.call(srv))
    {
        PRINTERROR("Failed to call service");
        return 1;
    }

    if (srv.response.error_code.value != manipulation_msgs::GraspPlanningErrorCode::SUCCESS)
    {
        PRINTERROR("Could do the grasp planning. Error code " << srv.response.error_code.value);
        return 1;
    }

    PRINTMSG("Successfully finished grasp planning. Have " << srv.response.grasps.size() << " resulting grasps.");
    std::vector<manipulation_msgs::Grasp>::iterator it;
    int i=1;
    for (it = srv.response.grasps.begin(); it != srv.response.grasps.end(); ++it)
    {
        if (!output_path.empty())
        {
            std::stringstream filename;
            filename<<output_path<<"/Grasp_"<<i<<".msg";
            std::stringstream filename_txt;
            filename_txt<<output_path<<"/Grasp_"<<i<<"_string.msg";
            ++i;
            if (!saveToFile(*it, filename.str(), true))
            {
                PRINTERROR("Could not save to file "<<filename.str());
                continue;
            }
            saveToFile(*it, filename_txt.str(), false);
        }
        else
        {
            PRINTMSG(*it);
        }
    }
    return 0;
}